001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 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 */ 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 com.puppycrawl.tools.checkstyle.api.AnnotationUtility; 029 import com.puppycrawl.tools.checkstyle.api.DetailAST; 030 import com.puppycrawl.tools.checkstyle.api.FileContents; 031 import com.puppycrawl.tools.checkstyle.api.Scope; 032 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 033 034 public class PublicApiVisitor extends JavaAstVisitor { 035 036 final static String OVERRIDE_ANNOTATION_KEYWORD = "Override"; 037 038 public 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 = peekSourceCode(); 052 if (isPublicApi(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 static boolean isEmptyDefaultConstructor(DetailAST ast) { 061 return (isConstructorWithoutParameters(ast)) && (ast.getLastChild().getChildCount() == 1); 062 } 063 064 private static boolean isConstructorWithoutParameters(DetailAST ast) { 065 return ast.getType() == TokenTypes.CTOR_DEF && ast.findFirstToken(TokenTypes.PARAMETERS).getChildCount() == 0; 066 } 067 068 private static boolean isMethodWithOverrideAnnotation(DetailAST ast) { 069 if (isMethod(ast)) { 070 return AnnotationUtility.containsAnnotation(ast, OVERRIDE_ANNOTATION_KEYWORD) 071 || AnnotationUtility.containsAnnotation(ast, "java.lang." + OVERRIDE_ANNOTATION_KEYWORD); 072 } 073 return false; 074 } 075 076 private static boolean isMethod(DetailAST ast) { 077 return ast.getType() == TokenTypes.METHOD_DEF; 078 } 079 080 private boolean isDocumentedApi(DetailAST ast) { 081 return isDocumentedApi(ast, getFileContents()); 082 } 083 084 private static boolean isPublic(DetailAST ast) { 085 return (AstUtils.isScope(AstUtils.getScope(ast), Scope.PUBLIC) || AstUtils.isType(ast, TokenTypes.ANNOTATION_FIELD_DEF)); 086 } 087 088 private static boolean isStaticFinalVariable(DetailAST ast) { 089 return (AstUtils.isClassVariable(ast) || AstUtils.isInterfaceVariable(ast)) && AstUtils.isFinal(ast) && AstUtils.isStatic(ast); 090 } 091 092 /** 093 * Also used by {@link org.sonar.java.ast.check.UndocumentedApiCheck} 094 */ 095 public static boolean isDocumentedApi(DetailAST ast, FileContents fileContents) { 096 return fileContents.getJavadocBefore(ast.getLineNo()) != null; 097 } 098 099 /** 100 * Also used by {@link org.sonar.java.ast.check.UndocumentedApiCheck} 101 */ 102 public static boolean isPublicApi(DetailAST ast) { 103 return isPublic(ast) && !isStaticFinalVariable(ast) && !isMethodWithOverrideAnnotation(ast) && !isEmptyDefaultConstructor(ast); 104 } 105 106 }