001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.java.ast.visitor; 021 022 import java.util.Arrays; 023 import java.util.List; 024 025 import org.sonar.squid.api.SourceCode; 026 import org.sonar.squid.measures.Metric; 027 028 import antlr.collections.AST; 029 030 import com.puppycrawl.tools.checkstyle.api.DetailAST; 031 import com.puppycrawl.tools.checkstyle.api.Scope; 032 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 033 034 public class PublicApiVisitor extends AstVisitor { 035 036 final static String OVERRIDE_ANNOTATION_KEYWORD = "Override"; 037 038 private static final List<Integer> wantedTokens = Arrays.asList(TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.METHOD_DEF, 039 TokenTypes.CTOR_DEF, TokenTypes.ANNOTATION_DEF, TokenTypes.ANNOTATION_FIELD_DEF, TokenTypes.VARIABLE_DEF); 040 041 public PublicApiVisitor() { 042 } 043 044 @Override 045 public List<Integer> getWantedTokens() { 046 return wantedTokens; 047 } 048 049 @Override 050 public void visitToken(DetailAST ast) { 051 SourceCode currentResource = peekResource(); 052 if (isPublic(ast) && !isStaticFinalVariable(ast) && !isMethodWithOverrideAnnotation(ast) && !isEmptyDefaultConstructor(ast)) { 053 currentResource.add(Metric.PUBLIC_API, 1); 054 if (isDocumentedApi(ast)) { 055 currentResource.add(Metric.PUBLIC_DOC_API, 1); 056 } 057 } 058 } 059 060 private boolean isEmptyDefaultConstructor(DetailAST ast) { 061 if (isConstructorWithoutParameters(ast) && ast.getLastChild().getChildCount() == 1) { 062 return true; 063 } 064 return false; 065 } 066 067 private boolean isConstructorWithoutParameters(DetailAST ast) { 068 return ast.getType() == TokenTypes.CTOR_DEF && ast.findFirstToken(TokenTypes.PARAMETERS).getChildCount() == 0; 069 } 070 071 private boolean isMethodWithOverrideAnnotation(DetailAST ast) { 072 if (isMethod(ast)) { 073 DetailAST modifier = ast.findFirstToken(TokenTypes.MODIFIERS); 074 for (AST annotation = modifier.getFirstChild(); annotation != null; annotation = annotation.getNextSibling()) { 075 if (isAnnotation(annotation) && ((DetailAST) annotation).findFirstToken(TokenTypes.IDENT) != null) { 076 String name = ((DetailAST) annotation).findFirstToken(TokenTypes.IDENT).getText(); 077 return OVERRIDE_ANNOTATION_KEYWORD.equals(name); 078 } 079 } 080 } 081 return false; 082 } 083 084 private boolean isAnnotation(AST annotation) { 085 return annotation.getType() == TokenTypes.ANNOTATION; 086 } 087 088 private boolean isMethod(DetailAST ast) { 089 return ast.getType() == TokenTypes.METHOD_DEF; 090 } 091 092 private boolean isPublic(DetailAST ast) { 093 return (AstUtils.isScope(AstUtils.getScope(ast), Scope.PUBLIC) || AstUtils.isType(ast, TokenTypes.ANNOTATION_FIELD_DEF)); 094 } 095 096 private boolean isDocumentedApi(DetailAST ast) { 097 return getFileContents().getJavadocBefore(ast.getLineNo()) != null; 098 } 099 100 private boolean isStaticFinalVariable(DetailAST ast) { 101 return (AstUtils.isClassVariable(ast) || AstUtils.isInterfaceVariable(ast)) && AstUtils.isFinal(ast) && AstUtils.isStatic(ast); 102 } 103 }