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

changeset 664
4124840b35fe
child 1073
f85d980faaf8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/errors/TestSuppression.java	Mon Aug 30 18:03:35 2010 -0700
     1.3 @@ -0,0 +1,232 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6403465
    1.30 + * @summary javac should defer diagnostics until it can be determined they are persistent
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +import javax.annotation.processing.*;
    1.36 +import javax.lang.model.*;
    1.37 +import javax.lang.model.element.TypeElement;
    1.38 +import javax.tools.*;
    1.39 +
    1.40 +import com.sun.source.util.JavacTask;
    1.41 +import com.sun.tools.javac.api.JavacTool;
    1.42 +import com.sun.tools.javac.util.JCDiagnostic;
    1.43 +
    1.44 +import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
    1.45 +
    1.46 +
    1.47 +public class TestSuppression {
    1.48 +    public static void main(String... args) throws Exception {
    1.49 +        new TestSuppression().run(args);
    1.50 +    }
    1.51 +
    1.52 +    enum WarningKind { NO, YES };
    1.53 +
    1.54 +    String[] cases = {
    1.55 +        // missing class C
    1.56 +        "class X { C c; }",
    1.57 +        "class X { C foo() { return null; } }",
    1.58 +        "class X { void foo(C c) { } }",
    1.59 +        "class X extends C { }",
    1.60 +        "class X<T extends C> { }",
    1.61 +        // missing interface I
    1.62 +        "class X implements I { }",
    1.63 +        "interface X extends I { }",
    1.64 +        // missing exception E
    1.65 +        "class X { void m() throws E { } }",
    1.66 +        // missing method m
    1.67 +        "class X extends C { int i = m(); }",
    1.68 +        // missing field f
    1.69 +        "class X extends C { int i = f; }"
    1.70 +    };
    1.71 +
    1.72 +    void run(String... args) throws Exception {
    1.73 +        for (String c: cases) {
    1.74 +            for (WarningKind wk: WarningKind.values()) {
    1.75 +                for (int g = 1; g <= 3; g++) {
    1.76 +                    try {
    1.77 +                        test(c, wk, g);
    1.78 +                    } catch (Throwable t) {
    1.79 +                        error("caught: " + t);
    1.80 +                    }
    1.81 +                    if (errors > 0) throw new AssertionError();
    1.82 +                }
    1.83 +            }
    1.84 +        }
    1.85 +
    1.86 +        System.err.println(count + " test cases");
    1.87 +
    1.88 +        if (errors > 0)
    1.89 +            throw new Exception(errors + " errors occurred");
    1.90 +    }
    1.91 +
    1.92 +    void test(String src, WarningKind wk, int gen) throws Exception {
    1.93 +        count++;
    1.94 +        System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" +src);
    1.95 +
    1.96 +        File testDir = new File("test" + count);
    1.97 +        File srcDir = createDir(testDir, "src");
    1.98 +        File gensrcDir = createDir(testDir, "gensrc");
    1.99 +        File classesDir = createDir(testDir, "classes");
   1.100 +
   1.101 +        File x = writeFile(new File(srcDir, "X.java"), src);
   1.102 +
   1.103 +        DiagListener dl = new DiagListener();
   1.104 +        JavacTool tool = JavacTool.create();
   1.105 +        StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
   1.106 +        fm.setLocation(StandardLocation.CLASS_PATH,
   1.107 +                Arrays.asList(classesDir, new File(System.getProperty("test.classes"))));
   1.108 +        fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir));
   1.109 +        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir));
   1.110 +        List<String> args = new ArrayList<String>();
   1.111 +//        args.add("-XprintProcessorInfo");
   1.112 +        args.add("-XprintRounds");
   1.113 +        args.add("-Agen=" + gen);
   1.114 +        if (wk == WarningKind.YES)
   1.115 +            args.add("-Xlint:serial");
   1.116 +        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x);
   1.117 +
   1.118 +        StringWriter sw = new StringWriter();
   1.119 +        PrintWriter pw = new PrintWriter(sw);
   1.120 +        JavacTask task = tool.getTask(pw, fm, dl, args, null, files);
   1.121 +        task.setProcessors(Arrays.asList(new AnnoProc()));
   1.122 +        boolean ok = task.call();
   1.123 +        pw.close();
   1.124 +
   1.125 +        System.err.println("ok:" + ok + " diags:" + dl.counts);
   1.126 +        if (sw.toString().length() > 0) {
   1.127 +            System.err.println("output:\n" + sw.toString());
   1.128 +        }
   1.129 +
   1.130 +        for (Diagnostic.Kind dk: Diagnostic.Kind.values()) {
   1.131 +            Integer v = dl.counts.get(dk);
   1.132 +            int found = (v == null) ? 0 : v;
   1.133 +            int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0;
   1.134 +            if (found != expect) {
   1.135 +                error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found);
   1.136 +            }
   1.137 +        }
   1.138 +
   1.139 +        System.err.println();
   1.140 +    }
   1.141 +
   1.142 +    File createDir(File parent, String name) {
   1.143 +        File dir = new File(parent, name);
   1.144 +        dir.mkdirs();
   1.145 +        return dir;
   1.146 +    }
   1.147 +
   1.148 +    File writeFile(File f, String content) throws IOException {
   1.149 +        FileWriter out = new FileWriter(f);
   1.150 +        try {
   1.151 +            out.write(content);
   1.152 +        } finally {
   1.153 +            out.close();
   1.154 +        }
   1.155 +        return f;
   1.156 +    }
   1.157 +
   1.158 +    <T> void add(List<T> list, T... values) {
   1.159 +        for (T v: values)
   1.160 +            list.add(v);
   1.161 +    }
   1.162 +
   1.163 +    void error(String msg) {
   1.164 +        System.err.println("Error: " + msg);
   1.165 +        errors++;
   1.166 +    }
   1.167 +
   1.168 +    int count;
   1.169 +    int errors;
   1.170 +
   1.171 +    static class DiagListener implements DiagnosticListener<JavaFileObject> {
   1.172 +        int total;
   1.173 +        Map<Diagnostic.Kind,Integer> counts = new TreeMap<Diagnostic.Kind,Integer>();
   1.174 +
   1.175 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.176 +            System.err.println((++total) + ": "
   1.177 +                    + "resolveError:" + isResolveError((JCDiagnostic) diagnostic) + "\n"
   1.178 +                    + diagnostic);
   1.179 +            Diagnostic.Kind dk = diagnostic.getKind();
   1.180 +            Integer c = counts.get(dk);
   1.181 +            counts.put(dk, (c == null ? 1 : c + 1));
   1.182 +        }
   1.183 +
   1.184 +        private static boolean isResolveError(JCDiagnostic d) {
   1.185 +            return d.isFlagSet(RESOLVE_ERROR);
   1.186 +        }
   1.187 +    }
   1.188 +
   1.189 +    @SupportedAnnotationTypes("*")
   1.190 +    @SupportedOptions("gen")
   1.191 +    public static class AnnoProc extends AbstractProcessor {
   1.192 +        Filer f;
   1.193 +        Messager m;
   1.194 +        int gen;
   1.195 +
   1.196 +        @Override
   1.197 +        public void init(ProcessingEnvironment processingEnv) {
   1.198 +            f = processingEnv.getFiler();
   1.199 +            m = processingEnv.getMessager();
   1.200 +            Map<String,String> options = processingEnv.getOptions();
   1.201 +            gen = Integer.parseInt(options.get("gen"));
   1.202 +        }
   1.203 +
   1.204 +        @Override
   1.205 +        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   1.206 +            round++;
   1.207 +            if (round < gen)
   1.208 +                writeSource("Dummy" + round, "class Dummy" + round + " extends java.util.ArrayList{ }");
   1.209 +            else if (round == gen) {
   1.210 +                writeSource("C", "class C { int f; int m() { return 0; } }");
   1.211 +                writeSource("I", "interface I { }");
   1.212 +                writeSource("E", "class E extends Exception { }");
   1.213 +            }
   1.214 +            return true;
   1.215 +        }
   1.216 +
   1.217 +        @Override
   1.218 +        public SourceVersion getSupportedSourceVersion() {
   1.219 +            return SourceVersion.latest();
   1.220 +        }
   1.221 +
   1.222 +        private void writeSource(String name, String text) {
   1.223 +            try {
   1.224 +                JavaFileObject fo = f.createSourceFile(name);
   1.225 +                Writer out = fo.openWriter();
   1.226 +                out.write(text);
   1.227 +                out.close();
   1.228 +            } catch (IOException e) {
   1.229 +                m.printMessage(Diagnostic.Kind.ERROR, e.toString());
   1.230 +            }
   1.231 +        }
   1.232 +
   1.233 +        int round = 0;
   1.234 +    }
   1.235 +}

mercurial