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> TOKENS = 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 TOKENS;
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 return (isConstructorWithoutParameters(ast)) && (ast.getLastChild().getChildCount() == 1);
062 }
063
064 private boolean isConstructorWithoutParameters(DetailAST ast) {
065 return ast.getType() == TokenTypes.CTOR_DEF && ast.findFirstToken(TokenTypes.PARAMETERS).getChildCount() == 0;
066 }
067
068 private boolean isMethodWithOverrideAnnotation(DetailAST ast) {
069 if (isMethod(ast)) {
070 DetailAST modifier = ast.findFirstToken(TokenTypes.MODIFIERS);
071 for (AST annotation = modifier.getFirstChild(); annotation != null; annotation = annotation.getNextSibling()) {
072 if (isAnnotation(annotation) && ((DetailAST) annotation).findFirstToken(TokenTypes.IDENT) != null) {
073 String name = ((DetailAST) annotation).findFirstToken(TokenTypes.IDENT).getText();
074 return OVERRIDE_ANNOTATION_KEYWORD.equals(name);
075 }
076 }
077 }
078 return false;
079 }
080
081 private boolean isAnnotation(AST annotation) {
082 return annotation.getType() == TokenTypes.ANNOTATION;
083 }
084
085 private boolean isMethod(DetailAST ast) {
086 return ast.getType() == TokenTypes.METHOD_DEF;
087 }
088
089 private boolean isPublic(DetailAST ast) {
090 return (AstUtils.isScope(AstUtils.getScope(ast), Scope.PUBLIC) || AstUtils.isType(ast, TokenTypes.ANNOTATION_FIELD_DEF));
091 }
092
093 private boolean isDocumentedApi(DetailAST ast) {
094 return getFileContents().getJavadocBefore(ast.getLineNo()) != null;
095 }
096
097 private boolean isStaticFinalVariable(DetailAST ast) {
098 return (AstUtils.isClassVariable(ast) || AstUtils.isInterfaceVariable(ast)) && AstUtils.isFinal(ast) && AstUtils.isStatic(ast);
099 }
100 }