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