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