001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.java.ast.visitor;
021
022import com.puppycrawl.tools.checkstyle.api.*;
023import org.sonar.squid.api.SourceCode;
024import org.sonar.squid.measures.Metric;
025
026import java.util.Arrays;
027import java.util.List;
028
029public class PublicApiVisitor extends JavaAstVisitor {
030
031  static final String OVERRIDE_ANNOTATION_KEYWORD = "Override";
032
033  public static final List<Integer> TOKENS = Arrays.asList(TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.METHOD_DEF,
034      TokenTypes.CTOR_DEF, TokenTypes.ANNOTATION_DEF, TokenTypes.ANNOTATION_FIELD_DEF, TokenTypes.VARIABLE_DEF);
035
036  public PublicApiVisitor() {
037  }
038
039  @Override
040  public List<Integer> getWantedTokens() {
041    return TOKENS;
042  }
043
044  @Override
045  public void visitToken(DetailAST ast) {
046    SourceCode currentResource = peekSourceCode();
047    if (isPublicApi(ast)) {
048      currentResource.add(Metric.PUBLIC_API, 1);
049      if (isDocumentedApi(ast)) {
050        currentResource.add(Metric.PUBLIC_DOC_API, 1);
051      }
052    }
053  }
054
055  private static boolean isEmptyDefaultConstructor(DetailAST ast) {
056    return (isConstructorWithoutParameters(ast)) && (ast.getLastChild().getChildCount() == 1);
057  }
058
059  private static boolean isConstructorWithoutParameters(DetailAST ast) {
060    return ast.getType() == TokenTypes.CTOR_DEF && ast.findFirstToken(TokenTypes.PARAMETERS).getChildCount() == 0;
061  }
062
063  private static boolean isMethodWithOverrideAnnotation(DetailAST ast) {
064    if (isMethod(ast)) {
065      return AnnotationUtility.containsAnnotation(ast, OVERRIDE_ANNOTATION_KEYWORD)
066          || AnnotationUtility.containsAnnotation(ast, "java.lang." + OVERRIDE_ANNOTATION_KEYWORD);
067    }
068    return false;
069  }
070
071  private static boolean isMethod(DetailAST ast) {
072    return ast.getType() == TokenTypes.METHOD_DEF;
073  }
074
075  private boolean isDocumentedApi(DetailAST ast) {
076    return isDocumentedApi(ast, getFileContents());
077  }
078
079  private static boolean isPublic(DetailAST ast) {
080    return (AstUtils.isScope(AstUtils.getScope(ast), Scope.PUBLIC) || AstUtils.isType(ast, TokenTypes.ANNOTATION_FIELD_DEF));
081  }
082
083  private static boolean isStaticFinalVariable(DetailAST ast) {
084    return (AstUtils.isClassVariable(ast) || AstUtils.isInterfaceVariable(ast)) && AstUtils.isFinal(ast) && AstUtils.isStatic(ast);
085  }
086
087  /**
088   * Also used by {@link org.sonar.java.ast.check.UndocumentedApiCheck}
089   */
090  public static boolean isDocumentedApi(DetailAST ast, FileContents fileContents) {
091    return fileContents.getJavadocBefore(ast.getLineNo()) != null;
092  }
093
094  /**
095   * Also used by {@link org.sonar.java.ast.check.UndocumentedApiCheck}
096   */
097  public static boolean isPublicApi(DetailAST ast) {
098    return isPublic(ast) && !isStaticFinalVariable(ast) && !isMethodWithOverrideAnnotation(ast) && !isEmptyDefaultConstructor(ast);
099  }
100
101}