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

Fri, 27 May 2011 15:02:39 -0700

author
jeff
date
Fri, 27 May 2011 15:02:39 -0700
changeset 1016
6211df69f7e0
parent 664
4124840b35fe
child 1073
f85d980faaf8
permissions
-rw-r--r--

7045697: JDK7 THIRD PARTY README update
Reviewed-by: lana

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

mercurial