test/tools/javac/processing/errors/TestSuppression.java

changeset 664
4124840b35fe
child 1073
f85d980faaf8
equal deleted inserted replaced
663:eb7c263aab73 664:4124840b35fe
1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 6403465
27 * @summary javac should defer diagnostics until it can be determined they are persistent
28 */
29
30 import java.io.*;
31 import java.util.*;
32 import javax.annotation.processing.*;
33 import javax.lang.model.*;
34 import javax.lang.model.element.TypeElement;
35 import javax.tools.*;
36
37 import com.sun.source.util.JavacTask;
38 import com.sun.tools.javac.api.JavacTool;
39 import com.sun.tools.javac.util.JCDiagnostic;
40
41 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
42
43
44 public class TestSuppression {
45 public static void main(String... args) throws Exception {
46 new TestSuppression().run(args);
47 }
48
49 enum WarningKind { NO, YES };
50
51 String[] cases = {
52 // missing class C
53 "class X { C c; }",
54 "class X { C foo() { return null; } }",
55 "class X { void foo(C c) { } }",
56 "class X extends C { }",
57 "class X<T extends C> { }",
58 // missing interface I
59 "class X implements I { }",
60 "interface X extends I { }",
61 // missing exception E
62 "class X { void m() throws E { } }",
63 // missing method m
64 "class X extends C { int i = m(); }",
65 // missing field f
66 "class X extends C { int i = f; }"
67 };
68
69 void run(String... args) throws Exception {
70 for (String c: cases) {
71 for (WarningKind wk: WarningKind.values()) {
72 for (int g = 1; g <= 3; g++) {
73 try {
74 test(c, wk, g);
75 } catch (Throwable t) {
76 error("caught: " + t);
77 }
78 if (errors > 0) throw new AssertionError();
79 }
80 }
81 }
82
83 System.err.println(count + " test cases");
84
85 if (errors > 0)
86 throw new Exception(errors + " errors occurred");
87 }
88
89 void test(String src, WarningKind wk, int gen) throws Exception {
90 count++;
91 System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" +src);
92
93 File testDir = new File("test" + count);
94 File srcDir = createDir(testDir, "src");
95 File gensrcDir = createDir(testDir, "gensrc");
96 File classesDir = createDir(testDir, "classes");
97
98 File x = writeFile(new File(srcDir, "X.java"), src);
99
100 DiagListener dl = new DiagListener();
101 JavacTool tool = JavacTool.create();
102 StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
103 fm.setLocation(StandardLocation.CLASS_PATH,
104 Arrays.asList(classesDir, new File(System.getProperty("test.classes"))));
105 fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir));
106 fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir));
107 List<String> args = new ArrayList<String>();
108 // args.add("-XprintProcessorInfo");
109 args.add("-XprintRounds");
110 args.add("-Agen=" + gen);
111 if (wk == WarningKind.YES)
112 args.add("-Xlint:serial");
113 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x);
114
115 StringWriter sw = new StringWriter();
116 PrintWriter pw = new PrintWriter(sw);
117 JavacTask task = tool.getTask(pw, fm, dl, args, null, files);
118 task.setProcessors(Arrays.asList(new AnnoProc()));
119 boolean ok = task.call();
120 pw.close();
121
122 System.err.println("ok:" + ok + " diags:" + dl.counts);
123 if (sw.toString().length() > 0) {
124 System.err.println("output:\n" + sw.toString());
125 }
126
127 for (Diagnostic.Kind dk: Diagnostic.Kind.values()) {
128 Integer v = dl.counts.get(dk);
129 int found = (v == null) ? 0 : v;
130 int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0;
131 if (found != expect) {
132 error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found);
133 }
134 }
135
136 System.err.println();
137 }
138
139 File createDir(File parent, String name) {
140 File dir = new File(parent, name);
141 dir.mkdirs();
142 return dir;
143 }
144
145 File writeFile(File f, String content) throws IOException {
146 FileWriter out = new FileWriter(f);
147 try {
148 out.write(content);
149 } finally {
150 out.close();
151 }
152 return f;
153 }
154
155 <T> void add(List<T> list, T... values) {
156 for (T v: values)
157 list.add(v);
158 }
159
160 void error(String msg) {
161 System.err.println("Error: " + msg);
162 errors++;
163 }
164
165 int count;
166 int errors;
167
168 static class DiagListener implements DiagnosticListener<JavaFileObject> {
169 int total;
170 Map<Diagnostic.Kind,Integer> counts = new TreeMap<Diagnostic.Kind,Integer>();
171
172 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
173 System.err.println((++total) + ": "
174 + "resolveError:" + isResolveError((JCDiagnostic) diagnostic) + "\n"
175 + diagnostic);
176 Diagnostic.Kind dk = diagnostic.getKind();
177 Integer c = counts.get(dk);
178 counts.put(dk, (c == null ? 1 : c + 1));
179 }
180
181 private static boolean isResolveError(JCDiagnostic d) {
182 return d.isFlagSet(RESOLVE_ERROR);
183 }
184 }
185
186 @SupportedAnnotationTypes("*")
187 @SupportedOptions("gen")
188 public static class AnnoProc extends AbstractProcessor {
189 Filer f;
190 Messager m;
191 int gen;
192
193 @Override
194 public void init(ProcessingEnvironment processingEnv) {
195 f = processingEnv.getFiler();
196 m = processingEnv.getMessager();
197 Map<String,String> options = processingEnv.getOptions();
198 gen = Integer.parseInt(options.get("gen"));
199 }
200
201 @Override
202 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
203 round++;
204 if (round < gen)
205 writeSource("Dummy" + round, "class Dummy" + round + " extends java.util.ArrayList{ }");
206 else if (round == gen) {
207 writeSource("C", "class C { int f; int m() { return 0; } }");
208 writeSource("I", "interface I { }");
209 writeSource("E", "class E extends Exception { }");
210 }
211 return true;
212 }
213
214 @Override
215 public SourceVersion getSupportedSourceVersion() {
216 return SourceVersion.latest();
217 }
218
219 private void writeSource(String name, String text) {
220 try {
221 JavaFileObject fo = f.createSourceFile(name);
222 Writer out = fo.openWriter();
223 out.write(text);
224 out.close();
225 } catch (IOException e) {
226 m.printMessage(Diagnostic.Kind.ERROR, e.toString());
227 }
228 }
229
230 int round = 0;
231 }
232 }

mercurial