author | mcimadamore |
Sat, 06 Oct 2012 10:35:38 +0100 | |
changeset 14062 | b7439971a094 |
parent 13631 | dc1212c348f9 |
child 14541 | 36f9d11fc9aa |
permissions | -rw-r--r-- |
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
1 |
/* |
13631
dc1212c348f9
7191449: update copyright year to match last edit in jdk8 langtools repository
jjh
parents:
11554
diff
changeset
|
2 |
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
4 |
* |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
8 |
* |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
14 |
* |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
18 |
* |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
21 |
* questions. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
22 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
23 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
24 |
/* |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
25 |
* @test |
11554
64d41533dc9e
7131308: Three regression tests fail due to bad fix for 7127924
jjh
parents:
11551
diff
changeset
|
26 |
* @bug 6968063 7127924 |
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
27 |
* @summary provide examples of code that generate diagnostics |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
28 |
* @build Example CheckExamples |
11551
cc9505564baf
7127924: langtools regression tests sometimes fail en-masse on windows
jjh
parents:
7643
diff
changeset
|
29 |
* @run main/othervm CheckExamples |
cc9505564baf
7127924: langtools regression tests sometimes fail en-masse on windows
jjh
parents:
7643
diff
changeset
|
30 |
*/ |
cc9505564baf
7127924: langtools regression tests sometimes fail en-masse on windows
jjh
parents:
7643
diff
changeset
|
31 |
/* |
cc9505564baf
7127924: langtools regression tests sometimes fail en-masse on windows
jjh
parents:
7643
diff
changeset
|
32 |
* See CR 7127924 for info on why othervm is used. |
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
33 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
34 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
35 |
import java.io.*; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
36 |
import java.util.*; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
37 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
38 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
39 |
* Check invariants for a set of examples. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
40 |
* -- each example should exactly declare the keys that will be generated when |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
41 |
* it is run. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
42 |
* -- together, the examples should cover the set of resource keys in the |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
43 |
* compiler.properties bundle. A list of exceptions may be given in the |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
44 |
* not-yet.txt file. Entries on the not-yet.txt list should not be |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
45 |
* covered by examples. |
6926
f0beafc94c59
6990379: two examples fail under CheckExamples on Windows
jjg
parents:
6149
diff
changeset
|
46 |
* When new keys are added to the resource bundle, it is strongly recommended |
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
47 |
* that corresponding new examples be added here, if at all practical, instead |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
48 |
* of simply and lazily being added to the not-yet.txt list. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
49 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
50 |
public class CheckExamples { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
51 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
52 |
* Standard entry point. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
53 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
54 |
public static void main(String... args) throws Exception { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
55 |
new CheckExamples().run(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
56 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
57 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
58 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
59 |
* Run the test. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
60 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
61 |
void run() throws Exception { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
62 |
Set<Example> examples = getExamples(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
63 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
64 |
Set<String> notYetList = getNotYetList(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
65 |
Set<String> declaredKeys = new TreeSet<String>(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
66 |
for (Example e: examples) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
67 |
Set<String> e_decl = e.getDeclaredKeys(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
68 |
Set<String> e_actual = e.getActualKeys(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
69 |
for (String k: e_decl) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
70 |
if (!e_actual.contains(k)) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
71 |
error("Example " + e + " declares key " + k + " but does not generate it"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
72 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
73 |
for (String k: e_actual) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
74 |
if (!e_decl.contains(k)) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
75 |
error("Example " + e + " generates key " + k + " but does not declare it"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
76 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
77 |
for (String k: e.getDeclaredKeys()) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
78 |
if (notYetList.contains(k)) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
79 |
error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
80 |
declaredKeys.add(k); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
81 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
82 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
83 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
84 |
ResourceBundle b = |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
85 |
ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
86 |
Set<String> resourceKeys = new TreeSet<String>(b.keySet()); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
87 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
88 |
for (String dk: declaredKeys) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
89 |
if (!resourceKeys.contains(dk)) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
90 |
error("Key " + dk + " is declared in tests but is not a valid key in resource bundle"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
91 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
92 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
93 |
for (String nk: notYetList) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
94 |
if (!resourceKeys.contains(nk)) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
95 |
error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
96 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
97 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
98 |
for (String rk: resourceKeys) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
99 |
if (!declaredKeys.contains(rk) && !notYetList.contains(rk)) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
100 |
error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
101 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
102 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
103 |
System.err.println(examples.size() + " examples checked"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
104 |
System.err.println(notYetList.size() + " keys on not-yet list"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
105 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
106 |
Counts declaredCounts = new Counts(declaredKeys); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
107 |
Counts resourceCounts = new Counts(resourceKeys); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
108 |
List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes)); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
109 |
rows.add("other"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
110 |
rows.add("total"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
111 |
System.err.println(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
112 |
System.err.println(String.format("%-14s %15s %15s %4s", |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
113 |
"prefix", "#keys in tests", "#keys in javac", "%")); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
114 |
for (String p: rows) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
115 |
int d = declaredCounts.get(p); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
116 |
int r = resourceCounts.get(p); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
117 |
System.err.print(String.format("%-14s %15d %15d", p, d, r)); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
118 |
if (r != 0) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
119 |
System.err.print(String.format(" %3d%%", (d * 100) / r)); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
120 |
System.err.println(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
121 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
122 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
123 |
if (errors > 0) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
124 |
throw new Exception(errors + " errors occurred."); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
125 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
126 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
127 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
128 |
* Get the complete set of examples to be checked. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
129 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
130 |
Set<Example> getExamples() { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
131 |
Set<Example> results = new TreeSet<Example>(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
132 |
File testSrc = new File(System.getProperty("test.src")); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
133 |
File examples = new File(testSrc, "examples"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
134 |
for (File f: examples.listFiles()) { |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
6926
diff
changeset
|
135 |
if (isValidExample(f)) |
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
136 |
results.add(new Example(f)); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
137 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
138 |
return results; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
139 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
140 |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
6926
diff
changeset
|
141 |
boolean isValidExample(File f) { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
6926
diff
changeset
|
142 |
return (f.isDirectory() && f.list().length > 0) || |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
6926
diff
changeset
|
143 |
(f.isFile() && f.getName().endsWith(".java")); |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
6926
diff
changeset
|
144 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
6926
diff
changeset
|
145 |
|
6149
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
146 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
147 |
* Get the contents of the "not-yet" list. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
148 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
149 |
Set<String> getNotYetList() { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
150 |
Set<String> results = new TreeSet<String>(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
151 |
File testSrc = new File(System.getProperty("test.src")); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
152 |
File notYetList = new File(testSrc, "examples.not-yet.txt"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
153 |
try { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
154 |
String[] lines = read(notYetList).split("[\r\n]"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
155 |
for (String line: lines) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
156 |
int hash = line.indexOf("#"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
157 |
if (hash != -1) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
158 |
line = line.substring(0, hash).trim(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
159 |
if (line.matches("[A-Za-z0-9-_.]+")) |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
160 |
results.add(line); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
161 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
162 |
} catch (IOException e) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
163 |
throw new Error(e); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
164 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
165 |
return results; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
166 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
167 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
168 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
169 |
* Read the contents of a file. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
170 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
171 |
String read(File f) throws IOException { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
172 |
byte[] bytes = new byte[(int) f.length()]; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
173 |
DataInputStream in = new DataInputStream(new FileInputStream(f)); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
174 |
try { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
175 |
in.readFully(bytes); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
176 |
} finally { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
177 |
in.close(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
178 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
179 |
return new String(bytes); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
180 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
181 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
182 |
/** |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
183 |
* Report an error. |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
184 |
*/ |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
185 |
void error(String msg) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
186 |
System.err.println("Error: " + msg); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
187 |
errors++; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
188 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
189 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
190 |
int errors; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
191 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
192 |
static class Counts { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
193 |
static String[] prefixes = { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
194 |
"compiler.err.", |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
195 |
"compiler.warn.", |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
196 |
"compiler.note.", |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
197 |
"compiler.misc." |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
198 |
}; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
199 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
200 |
Counts(Set<String> keys) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
201 |
nextKey: |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
202 |
for (String k: keys) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
203 |
for (String p: prefixes) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
204 |
if (k.startsWith(p)) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
205 |
inc(p); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
206 |
continue nextKey; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
207 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
208 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
209 |
inc("other"); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
210 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
211 |
table.put("total", keys.size()); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
212 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
213 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
214 |
int get(String p) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
215 |
Integer i = table.get(p); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
216 |
return (i == null ? 0 : i); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
217 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
218 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
219 |
void inc(String p) { |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
220 |
Integer i = table.get(p); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
221 |
table.put(p, (i == null ? 1 : i + 1)); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
222 |
} |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
223 |
|
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
224 |
Map<String,Integer> table = new HashMap<String,Integer>(); |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
225 |
}; |
48de3564aa13
6968063: provide examples of code that generate diagnostics
jjg
parents:
diff
changeset
|
226 |
} |