1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package crules; 27 28 import java.util.Arrays; 29 import java.util.HashSet; 30 import java.util.Set; 31 32 import com.sun.source.util.JavacTask; 33 import com.sun.source.util.TaskEvent.Kind; 34 import com.sun.tools.javac.code.Symbol; 35 import com.sun.tools.javac.code.Symbol.MethodSymbol; 36 import com.sun.tools.javac.tree.JCTree.JCClassDecl; 37 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; 38 import com.sun.tools.javac.tree.TreeScanner; 39 import com.sun.tools.javac.util.DefinedBy; 40 import com.sun.tools.javac.util.DefinedBy.Api; 41 42 /**This analyzer ensures that all method that implement a public supported API method are marked with 43 * {@link DefinedBy} annotation, and that methods that don't implement a public API are not marked 44 * using the annotation. 45 */ 46 public class DefinedByAnalyzer extends AbstractCodingRulesAnalyzer { 47 48 public DefinedByAnalyzer(JavacTask task) { 49 super(task); 50 treeVisitor = new DefinedByVisitor(); 51 eventKind = Kind.ANALYZE; 52 } 53 54 //only java.compiler and jdk.compiler modules implement the APIs, 55 //so only these need the @DefinedBy annotation: 56 private static final Set<String> MODULE = new HashSet<>(Arrays.asList( 57 "java.compiler", 58 "jdk.compiler" 59 )); 60 61 class DefinedByVisitor extends TreeScanner { 62 @Override 63 public void visitClassDef(JCClassDecl tree) { 64 if (MODULE.contains(tree.sym.packge().modle.name.toString())) { 65 super.visitClassDef(tree); 66 } 67 } 68 @Override 69 public void visitMethodDef(JCMethodDecl tree) { 70 if (!isAPIPackage(packageName(tree.sym))) { 71 boolean seenAPIPackage = false; 72 73 for (MethodSymbol overridden : types.getOverriddenMethods(tree.sym)) { 74 String overriddenPackage = packageName(overridden); 75 76 if (!isAPIPackage(overriddenPackage)) 77 continue; 78 79 seenAPIPackage = true; 80 81 DefinedBy definedBy = tree.sym.getAnnotation(DefinedBy.class); 82 83 if (definedBy != null) { 84 String packageRoot = definedBy.value().packageRoot; 85 if (!overriddenPackage.startsWith(packageRoot)) { 86 messages.error(tree, "crules.wrong.defined.by"); 87 } 88 continue; 89 } 90 91 messages.error(tree, "crules.no.defined.by"); 92 } 93 94 if (!seenAPIPackage && tree.sym.getAnnotation(DefinedBy.class) != null) { 95 messages.error(tree, "crules.defined.by.no.api"); 96 } 97 } 98 99 super.visitMethodDef(tree); 100 } 101 102 private boolean isAPIPackage(String pack) { 103 for (Api api : Api.values()) { 104 if (pack.startsWith(api.packageRoot)) 105 return true; 106 } 107 108 return false; 109 } 110 111 private String packageName(Symbol sym) { 112 return elements.getPackageOf(sym).getQualifiedName().toString(); 113 } 114 } 115 }