langtools/make/tools/crules/DefinedByAnalyzer.java
changeset 26266 2d24bda701dc
child 40606 eb2c81860c86
equal deleted inserted replaced
26265:46aacfffd3b5 26266:2d24bda701dc
       
     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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package crules;
       
    25 
       
    26 import com.sun.source.util.JavacTask;
       
    27 import com.sun.source.util.TaskEvent.Kind;
       
    28 import com.sun.tools.javac.code.Symbol;
       
    29 import com.sun.tools.javac.code.Symbol.MethodSymbol;
       
    30 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
       
    31 import com.sun.tools.javac.tree.TreeScanner;
       
    32 import com.sun.tools.javac.util.DefinedBy;
       
    33 import com.sun.tools.javac.util.DefinedBy.Api;
       
    34 
       
    35 /**This analyzer ensures that all method that implement a public supported API method are marked with
       
    36  * {@link DefinedBy} annotation, and that methods that don't implement a public API are not marked
       
    37  * using the annotation.
       
    38  */
       
    39 public class DefinedByAnalyzer extends AbstractCodingRulesAnalyzer {
       
    40 
       
    41     public DefinedByAnalyzer(JavacTask task) {
       
    42         super(task);
       
    43         treeVisitor = new DefinedByVisitor();
       
    44         eventKind = Kind.ANALYZE;
       
    45     }
       
    46 
       
    47     class DefinedByVisitor extends TreeScanner {
       
    48         @Override
       
    49         public void visitMethodDef(JCMethodDecl tree) {
       
    50             if (!isAPIPackage(packageName(tree.sym))) {
       
    51                 boolean seenAPIPackage = false;
       
    52 
       
    53                 for (MethodSymbol overridden : types.getOverriddenMethods(tree.sym)) {
       
    54                     String overriddenPackage = packageName(overridden);
       
    55 
       
    56                     if (!isAPIPackage(overriddenPackage))
       
    57                         continue;
       
    58 
       
    59                     seenAPIPackage = true;
       
    60 
       
    61                     DefinedBy definedBy = tree.sym.getAnnotation(DefinedBy.class);
       
    62 
       
    63                     if (definedBy != null) {
       
    64                         String packageRoot = definedBy.value().packageRoot;
       
    65                         if (!overriddenPackage.startsWith(packageRoot)) {
       
    66                             messages.error(tree, "crules.wrong.defined.by");
       
    67                         }
       
    68                         continue;
       
    69                     }
       
    70 
       
    71                     messages.error(tree, "crules.no.defined.by");
       
    72                 }
       
    73 
       
    74                 if (!seenAPIPackage && tree.sym.getAnnotation(DefinedBy.class) != null) {
       
    75                     messages.error(tree, "crules.defined.by.no.api");
       
    76                 }
       
    77             }
       
    78 
       
    79             super.visitMethodDef(tree);
       
    80         }
       
    81 
       
    82         private boolean isAPIPackage(String pack) {
       
    83             for (Api api : Api.values()) {
       
    84                 if (pack.startsWith(api.packageRoot))
       
    85                     return true;
       
    86             }
       
    87 
       
    88             return false;
       
    89         }
       
    90 
       
    91         private String packageName(Symbol sym) {
       
    92             return elements.getPackageOf(sym).getQualifiedName().toString();
       
    93         }
       
    94     }
       
    95 }