author | lmesnik |
Wed, 11 Sep 2019 16:25:12 -0700 | |
changeset 58093 | 50e1d346a126 |
parent 49158 | f62d1d1c2d9c |
permissions | -rw-r--r-- |
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 |
49158
f62d1d1c2d9c
8194296: Check copyright of files in make/langtools/tools
jjg
parents:
47216
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
f62d1d1c2d9c
8194296: Check copyright of files in make/langtools/tools
jjg
parents:
47216
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
f62d1d1c2d9c
8194296: Check copyright of files in make/langtools/tools
jjg
parents:
47216
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
10 |
* |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
11 |
* 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
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
13 |
* 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
|
14 |
* 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
|
15 |
* accompanied this code). |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
16 |
* |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
17 |
* 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
|
18 |
* 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
|
19 |
* 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
|
20 |
* |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
21 |
* 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
|
22 |
* 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
|
23 |
* questions. |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
24 |
*/ |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
25 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
26 |
package crules; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
27 |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
28 |
import java.util.Arrays; |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
29 |
import java.util.HashSet; |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
30 |
import java.util.Set; |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
31 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
32 |
import com.sun.source.util.JavacTask; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
33 |
import com.sun.source.util.TaskEvent.Kind; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
34 |
import com.sun.tools.javac.code.Symbol; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
35 |
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
|
36 |
import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
37 |
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
38 |
import com.sun.tools.javac.tree.TreeScanner; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
39 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
40 |
import com.sun.tools.javac.util.DefinedBy.Api; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
41 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
42 |
/**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
|
43 |
* {@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
|
44 |
* using the annotation. |
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 class DefinedByAnalyzer extends AbstractCodingRulesAnalyzer { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
47 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
48 |
public DefinedByAnalyzer(JavacTask task) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
49 |
super(task); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
50 |
treeVisitor = new DefinedByVisitor(); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
51 |
eventKind = Kind.ANALYZE; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
52 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
53 |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
54 |
//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
|
55 |
//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
|
56 |
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
|
57 |
"java.compiler", |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
58 |
"jdk.compiler" |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
59 |
)); |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
60 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
61 |
class DefinedByVisitor extends TreeScanner { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
62 |
@Override |
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
63 |
public void visitClassDef(JCClassDecl tree) { |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
64 |
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
|
65 |
super.visitClassDef(tree); |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
66 |
} |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
67 |
} |
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
26266
diff
changeset
|
68 |
@Override |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
69 |
public void visitMethodDef(JCMethodDecl tree) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
70 |
if (!isAPIPackage(packageName(tree.sym))) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
71 |
boolean seenAPIPackage = false; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
72 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
73 |
for (MethodSymbol overridden : types.getOverriddenMethods(tree.sym)) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
74 |
String overriddenPackage = packageName(overridden); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
75 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
76 |
if (!isAPIPackage(overriddenPackage)) |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
77 |
continue; |
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 |
seenAPIPackage = true; |
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 |
DefinedBy definedBy = tree.sym.getAnnotation(DefinedBy.class); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
82 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
83 |
if (definedBy != null) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
84 |
String packageRoot = definedBy.value().packageRoot; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
85 |
if (!overriddenPackage.startsWith(packageRoot)) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
86 |
messages.error(tree, "crules.wrong.defined.by"); |
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 |
continue; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
89 |
} |
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 |
messages.error(tree, "crules.no.defined.by"); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
92 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
93 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
94 |
if (!seenAPIPackage && tree.sym.getAnnotation(DefinedBy.class) != null) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
95 |
messages.error(tree, "crules.defined.by.no.api"); |
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 |
} |
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 |
super.visitMethodDef(tree); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
100 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
101 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
102 |
private boolean isAPIPackage(String pack) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
103 |
for (Api api : Api.values()) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
104 |
if (pack.startsWith(api.packageRoot)) |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
105 |
return true; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
106 |
} |
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 |
return false; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
109 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
110 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
111 |
private String packageName(Symbol sym) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
112 |
return elements.getPackageOf(sym).getQualifiedName().toString(); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
113 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
114 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
115 |
} |