author | mcimadamore |
Thu, 05 Mar 2015 13:10:49 +0000 | |
changeset 29294 | 376a915b4ff0 |
parent 26266 | 2d24bda701dc |
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 |
/* |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
2 |
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
17283
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 com.sun.source.util.JavacTask; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
31 |
import com.sun.tools.javac.api.BasicJavacTask; |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
32 |
import com.sun.tools.javac.code.Symtab; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
33 |
import com.sun.tools.javac.model.JavacElements; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
34 |
import com.sun.tools.javac.model.JavacTypes; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
35 |
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
|
36 |
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
|
37 |
import com.sun.tools.javac.util.Context; |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
38 |
import com.sun.tools.javac.util.JCDiagnostic; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
39 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
17283
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.util.Log; |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
41 |
import com.sun.tools.javac.util.Options; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
42 |
import com.sun.tools.javac.util.RawDiagnosticFormatter; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
43 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
44 |
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
|
45 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
46 |
public abstract class AbstractCodingRulesAnalyzer { |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
47 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
48 |
private final Log log; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
49 |
private final boolean rawDiagnostics; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
50 |
private final JCDiagnostic.Factory diags; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
51 |
private final Options options; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
52 |
protected final Messages messages; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
53 |
protected final Symtab syms; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
54 |
protected final JavacElements elements; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
55 |
protected final JavacTypes types; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
56 |
protected TreeScanner treeVisitor; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
57 |
protected Kind eventKind; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
58 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
59 |
public AbstractCodingRulesAnalyzer(JavacTask task) { |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
60 |
BasicJavacTask impl = (BasicJavacTask)task; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
61 |
Context context = impl.getContext(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
62 |
log = Log.instance(context); |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
63 |
options = Options.instance(context); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
64 |
rawDiagnostics = options.isSet("rawDiagnostics"); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
65 |
diags = JCDiagnostic.Factory.instance(context); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
66 |
messages = new Messages(); |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
67 |
syms = Symtab.instance(context); |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
68 |
elements = JavacElements.instance(context); |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
69 |
types = JavacTypes.instance(context); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
70 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
71 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
72 |
protected class Messages { |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
73 |
ResourceBundle bundle; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
74 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
75 |
Messages() { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
79 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
80 |
public void error(JCTree tree, String code, Object... args) { |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
81 |
String msg; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
82 |
if (rawDiagnostics) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
83 |
RawDiagnosticFormatter f = new RawDiagnosticFormatter(options); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
84 |
msg = f.formatMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
85 |
tree.pos(), code, args), null); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
86 |
} else { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
87 |
msg = (code == null) ? (String) args[0] : localize(code, args); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
88 |
} |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
89 |
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
|
90 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
91 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
92 |
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
|
93 |
String msg = bundle.getString(code); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
94 |
if (msg == null) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
95 |
StringBuilder sb = new StringBuilder(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
96 |
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
|
97 |
if (args.length > 0) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
98 |
sb.append(" arguments={0}"); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
99 |
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
|
100 |
sb.append(", {").append(i).append("}"); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
101 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
102 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
103 |
msg = sb.toString(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
104 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
105 |
return MessageFormat.format(msg, args); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
106 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
107 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
108 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
109 |
} |