author | adlertz |
Fri, 24 Jan 2014 13:06:52 +0100 | |
changeset 22804 | 401135897b65 |
parent 17283 | cddfdf252f4a |
child 25848 | 3bc09f4676a9 |
permissions | -rw-r--r-- |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
1 |
/* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
4 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
8 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
13 |
* accompanied this code). |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
14 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
18 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
21 |
* questions. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
22 |
*/ |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
23 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
24 |
package crules; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
25 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
26 |
import java.text.MessageFormat; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
27 |
import java.util.Locale; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
28 |
import java.util.ResourceBundle; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
29 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
30 |
import javax.lang.model.element.TypeElement; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
31 |
import javax.tools.JavaFileObject; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
32 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
33 |
import com.sun.source.tree.Tree; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
34 |
import com.sun.source.util.JavacTask; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
35 |
import com.sun.source.util.Plugin; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
36 |
import com.sun.source.util.TaskEvent; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
37 |
import com.sun.source.util.TaskListener; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
38 |
import com.sun.source.util.Trees; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
39 |
import com.sun.tools.javac.api.BasicJavacTask; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
40 |
import com.sun.tools.javac.tree.JCTree; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
41 |
import com.sun.tools.javac.tree.TreeScanner; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
42 |
import com.sun.tools.javac.util.Context; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
43 |
import com.sun.tools.javac.util.Log; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
44 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
45 |
import static com.sun.source.util.TaskEvent.Kind; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
46 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
47 |
public abstract class AbstractCodingRulesAnalyzer implements Plugin { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
48 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
49 |
protected Log log; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
50 |
protected Trees trees; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
51 |
protected TreeScanner treeVisitor; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
52 |
protected Kind eventKind; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
53 |
protected Messages messages; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
54 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
55 |
public void init(JavacTask task, String... args) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
56 |
BasicJavacTask impl = (BasicJavacTask)task; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
57 |
Context context = impl.getContext(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
58 |
log = Log.instance(context); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
59 |
trees = Trees.instance(task); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
60 |
messages = new Messages(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
61 |
task.addTaskListener(new PostAnalyzeTaskListener()); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
62 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
63 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
64 |
public class PostAnalyzeTaskListener implements TaskListener { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
65 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
66 |
@Override |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
67 |
public void started(TaskEvent taskEvent) {} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
68 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
69 |
@Override |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
70 |
public void finished(TaskEvent taskEvent) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
71 |
if (taskEvent.getKind().equals(eventKind)) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
72 |
TypeElement typeElem = taskEvent.getTypeElement(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
73 |
Tree tree = trees.getTree(typeElem); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
74 |
if (tree != null) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
75 |
JavaFileObject prevSource = log.currentSourceFile(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
76 |
try { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
77 |
log.useSource(taskEvent.getCompilationUnit().getSourceFile()); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
78 |
treeVisitor.scan((JCTree)tree); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
79 |
} finally { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
80 |
log.useSource(prevSource); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
81 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
82 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
83 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
84 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
85 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
86 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
87 |
class Messages { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
88 |
ResourceBundle bundle; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
89 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
90 |
Messages() { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
91 |
String name = getClass().getPackage().getName() + ".resources.crules"; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
92 |
bundle = ResourceBundle.getBundle(name, Locale.ENGLISH); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
93 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
94 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
95 |
public void error(JCTree tree, String code, Object... args) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
96 |
String msg = (code == null) ? (String) args[0] : localize(code, args); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
97 |
log.error(tree, "proc.messager", msg.toString()); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
98 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
99 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
100 |
private String localize(String code, Object... args) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
101 |
String msg = bundle.getString(code); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
102 |
if (msg == null) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
103 |
StringBuilder sb = new StringBuilder(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
104 |
sb.append("message file broken: code=").append(code); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
105 |
if (args.length > 0) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
106 |
sb.append(" arguments={0}"); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
107 |
for (int i = 1; i < args.length; i++) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
108 |
sb.append(", {").append(i).append("}"); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
109 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
110 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
111 |
msg = sb.toString(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
112 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
113 |
return MessageFormat.format(msg, args); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
114 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
115 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
116 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
117 |
} |