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

Wed, 24 Sep 2014 11:38:26 -0700

author
katleman
date
Wed, 24 Sep 2014 11:38:26 -0700
changeset 2562
ed1a48bedfa8
parent 1073
f85d980faaf8
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u40-b07 for changeset 2fa3858a281f

jjg@664 1 /*
jjg@1073 2 * Copyright (c) 2010, 2011, 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@1073 38 import com.sun.tools.javac.api.ClientCodeWrapper;
jjg@664 39 import com.sun.tools.javac.api.JavacTool;
jjg@664 40 import com.sun.tools.javac.util.JCDiagnostic;
jjg@664 41
jjg@664 42 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
jjg@664 43
jjg@664 44
jjg@664 45 public class TestSuppression {
jjg@664 46 public static void main(String... args) throws Exception {
jjg@664 47 new TestSuppression().run(args);
jjg@664 48 }
jjg@664 49
jjg@664 50 enum WarningKind { NO, YES };
jjg@664 51
jjg@664 52 String[] cases = {
jjg@664 53 // missing class C
jjg@664 54 "class X { C c; }",
jjg@664 55 "class X { C foo() { return null; } }",
jjg@664 56 "class X { void foo(C c) { } }",
jjg@664 57 "class X extends C { }",
jjg@664 58 "class X<T extends C> { }",
jjg@664 59 // missing interface I
jjg@664 60 "class X implements I { }",
jjg@664 61 "interface X extends I { }",
jjg@664 62 // missing exception E
jjg@664 63 "class X { void m() throws E { } }",
jjg@664 64 // missing method m
jjg@664 65 "class X extends C { int i = m(); }",
jjg@664 66 // missing field f
jjg@664 67 "class X extends C { int i = f; }"
jjg@664 68 };
jjg@664 69
jjg@664 70 void run(String... args) throws Exception {
jjg@664 71 for (String c: cases) {
jjg@664 72 for (WarningKind wk: WarningKind.values()) {
jjg@664 73 for (int g = 1; g <= 3; g++) {
jjg@664 74 try {
jjg@664 75 test(c, wk, g);
jjg@664 76 } catch (Throwable t) {
jjg@664 77 error("caught: " + t);
jjg@664 78 }
jjg@664 79 if (errors > 0) throw new AssertionError();
jjg@664 80 }
jjg@664 81 }
jjg@664 82 }
jjg@664 83
jjg@664 84 System.err.println(count + " test cases");
jjg@664 85
jjg@664 86 if (errors > 0)
jjg@664 87 throw new Exception(errors + " errors occurred");
jjg@664 88 }
jjg@664 89
jjg@664 90 void test(String src, WarningKind wk, int gen) throws Exception {
jjg@664 91 count++;
jjg@664 92 System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" +src);
jjg@664 93
jjg@664 94 File testDir = new File("test" + count);
jjg@664 95 File srcDir = createDir(testDir, "src");
jjg@664 96 File gensrcDir = createDir(testDir, "gensrc");
jjg@664 97 File classesDir = createDir(testDir, "classes");
jjg@664 98
jjg@664 99 File x = writeFile(new File(srcDir, "X.java"), src);
jjg@664 100
jjg@664 101 DiagListener dl = new DiagListener();
jjg@664 102 JavacTool tool = JavacTool.create();
jjg@664 103 StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
jjg@664 104 fm.setLocation(StandardLocation.CLASS_PATH,
jjg@664 105 Arrays.asList(classesDir, new File(System.getProperty("test.classes"))));
jjg@664 106 fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir));
jjg@664 107 fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir));
jjg@664 108 List<String> args = new ArrayList<String>();
jjg@664 109 // args.add("-XprintProcessorInfo");
jjg@664 110 args.add("-XprintRounds");
jjg@664 111 args.add("-Agen=" + gen);
jjg@664 112 if (wk == WarningKind.YES)
jjg@664 113 args.add("-Xlint:serial");
jjg@664 114 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x);
jjg@664 115
jjg@664 116 StringWriter sw = new StringWriter();
jjg@664 117 PrintWriter pw = new PrintWriter(sw);
jjg@664 118 JavacTask task = tool.getTask(pw, fm, dl, args, null, files);
jjg@664 119 task.setProcessors(Arrays.asList(new AnnoProc()));
jjg@664 120 boolean ok = task.call();
jjg@664 121 pw.close();
jjg@664 122
jjg@664 123 System.err.println("ok:" + ok + " diags:" + dl.counts);
jjg@664 124 if (sw.toString().length() > 0) {
jjg@664 125 System.err.println("output:\n" + sw.toString());
jjg@664 126 }
jjg@664 127
jjg@664 128 for (Diagnostic.Kind dk: Diagnostic.Kind.values()) {
jjg@664 129 Integer v = dl.counts.get(dk);
jjg@664 130 int found = (v == null) ? 0 : v;
jjg@664 131 int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0;
jjg@664 132 if (found != expect) {
jjg@664 133 error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found);
jjg@664 134 }
jjg@664 135 }
jjg@664 136
jjg@664 137 System.err.println();
jjg@664 138 }
jjg@664 139
jjg@664 140 File createDir(File parent, String name) {
jjg@664 141 File dir = new File(parent, name);
jjg@664 142 dir.mkdirs();
jjg@664 143 return dir;
jjg@664 144 }
jjg@664 145
jjg@664 146 File writeFile(File f, String content) throws IOException {
jjg@664 147 FileWriter out = new FileWriter(f);
jjg@664 148 try {
jjg@664 149 out.write(content);
jjg@664 150 } finally {
jjg@664 151 out.close();
jjg@664 152 }
jjg@664 153 return f;
jjg@664 154 }
jjg@664 155
jjg@664 156 <T> void add(List<T> list, T... values) {
jjg@664 157 for (T v: values)
jjg@664 158 list.add(v);
jjg@664 159 }
jjg@664 160
jjg@664 161 void error(String msg) {
jjg@664 162 System.err.println("Error: " + msg);
jjg@664 163 errors++;
jjg@664 164 }
jjg@664 165
jjg@664 166 int count;
jjg@664 167 int errors;
jjg@664 168
jjg@664 169 static class DiagListener implements DiagnosticListener<JavaFileObject> {
jjg@664 170 int total;
jjg@664 171 Map<Diagnostic.Kind,Integer> counts = new TreeMap<Diagnostic.Kind,Integer>();
jjg@664 172
jjg@664 173 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
jjg@664 174 System.err.println((++total) + ": "
jjg@1073 175 + "resolveError:" + isResolveError(unwrap(diagnostic)) + "\n"
jjg@664 176 + diagnostic);
jjg@664 177 Diagnostic.Kind dk = diagnostic.getKind();
jjg@664 178 Integer c = counts.get(dk);
jjg@664 179 counts.put(dk, (c == null ? 1 : c + 1));
jjg@664 180 }
jjg@664 181
jjg@664 182 private static boolean isResolveError(JCDiagnostic d) {
jjg@664 183 return d.isFlagSet(RESOLVE_ERROR);
jjg@664 184 }
jjg@1073 185
jjg@1073 186 private JCDiagnostic unwrap(Diagnostic<? extends JavaFileObject> diagnostic) {
jjg@1073 187 if (diagnostic instanceof JCDiagnostic)
jjg@1073 188 return (JCDiagnostic) diagnostic;
jjg@1073 189 if (diagnostic instanceof ClientCodeWrapper.DiagnosticSourceUnwrapper)
jjg@1073 190 return ((ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic).d;
jjg@1073 191 throw new IllegalArgumentException();
jjg@1073 192 }
jjg@664 193 }
jjg@664 194
jjg@664 195 @SupportedAnnotationTypes("*")
jjg@664 196 @SupportedOptions("gen")
jjg@664 197 public static class AnnoProc extends AbstractProcessor {
jjg@664 198 Filer f;
jjg@664 199 Messager m;
jjg@664 200 int gen;
jjg@664 201
jjg@664 202 @Override
jjg@664 203 public void init(ProcessingEnvironment processingEnv) {
jjg@664 204 f = processingEnv.getFiler();
jjg@664 205 m = processingEnv.getMessager();
jjg@664 206 Map<String,String> options = processingEnv.getOptions();
jjg@664 207 gen = Integer.parseInt(options.get("gen"));
jjg@664 208 }
jjg@664 209
jjg@664 210 @Override
jjg@664 211 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@664 212 round++;
jjg@664 213 if (round < gen)
jjg@664 214 writeSource("Dummy" + round, "class Dummy" + round + " extends java.util.ArrayList{ }");
jjg@664 215 else if (round == gen) {
jjg@664 216 writeSource("C", "class C { int f; int m() { return 0; } }");
jjg@664 217 writeSource("I", "interface I { }");
jjg@664 218 writeSource("E", "class E extends Exception { }");
jjg@664 219 }
jjg@664 220 return true;
jjg@664 221 }
jjg@664 222
jjg@664 223 @Override
jjg@664 224 public SourceVersion getSupportedSourceVersion() {
jjg@664 225 return SourceVersion.latest();
jjg@664 226 }
jjg@664 227
jjg@664 228 private void writeSource(String name, String text) {
jjg@664 229 try {
jjg@664 230 JavaFileObject fo = f.createSourceFile(name);
jjg@664 231 Writer out = fo.openWriter();
jjg@664 232 out.write(text);
jjg@664 233 out.close();
jjg@664 234 } catch (IOException e) {
jjg@664 235 m.printMessage(Diagnostic.Kind.ERROR, e.toString());
jjg@664 236 }
jjg@664 237 }
jjg@664 238
jjg@664 239 int round = 0;
jjg@664 240 }
jjg@664 241 }

mercurial