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