author | mcimadamore |
Thu, 05 Mar 2015 13:10:49 +0000 | |
changeset 29294 | 376a915b4ff0 |
parent 26266 | 2d24bda701dc |
child 40606 | eb2c81860c86 |
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 |
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 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
26 |
import com.sun.source.util.JavacTask; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
27 |
import com.sun.source.util.TaskEvent.Kind; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
28 |
import com.sun.tools.javac.code.Symbol; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
29 |
import com.sun.tools.javac.code.Symbol.MethodSymbol; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
30 |
import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
31 |
import com.sun.tools.javac.tree.TreeScanner; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
32 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
33 |
import com.sun.tools.javac.util.DefinedBy.Api; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
34 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
35 |
/**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
|
36 |
* {@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
|
37 |
* using the annotation. |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
38 |
*/ |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
39 |
public class DefinedByAnalyzer extends AbstractCodingRulesAnalyzer { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
40 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
41 |
public DefinedByAnalyzer(JavacTask task) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
42 |
super(task); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
43 |
treeVisitor = new DefinedByVisitor(); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
44 |
eventKind = Kind.ANALYZE; |
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 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
47 |
class DefinedByVisitor extends TreeScanner { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
48 |
@Override |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
49 |
public void visitMethodDef(JCMethodDecl tree) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
50 |
if (!isAPIPackage(packageName(tree.sym))) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
51 |
boolean seenAPIPackage = false; |
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 |
for (MethodSymbol overridden : types.getOverriddenMethods(tree.sym)) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
54 |
String overriddenPackage = packageName(overridden); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
55 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
56 |
if (!isAPIPackage(overriddenPackage)) |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
57 |
continue; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
58 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
59 |
seenAPIPackage = true; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
60 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
61 |
DefinedBy definedBy = tree.sym.getAnnotation(DefinedBy.class); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
62 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
63 |
if (definedBy != null) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
64 |
String packageRoot = definedBy.value().packageRoot; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
65 |
if (!overriddenPackage.startsWith(packageRoot)) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
66 |
messages.error(tree, "crules.wrong.defined.by"); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
67 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
68 |
continue; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
69 |
} |
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 |
messages.error(tree, "crules.no.defined.by"); |
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 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
74 |
if (!seenAPIPackage && tree.sym.getAnnotation(DefinedBy.class) != null) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
75 |
messages.error(tree, "crules.defined.by.no.api"); |
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 |
} |
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 |
super.visitMethodDef(tree); |
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 |
|
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
82 |
private boolean isAPIPackage(String pack) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
83 |
for (Api api : Api.values()) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
84 |
if (pack.startsWith(api.packageRoot)) |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
85 |
return true; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
86 |
} |
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 |
return false; |
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 |
private String packageName(Symbol sym) { |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
92 |
return elements.getPackageOf(sym).getQualifiedName().toString(); |
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 |
} |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
diff
changeset
|
95 |
} |