langtools/test/tools/javac/processing/TestWarnErrorCount.java
author jjg
Mon, 28 Feb 2011 13:37:48 -0800
changeset 8619 15a69d0db208
child 8629 e9843bacf5f3
permissions -rw-r--r--
7022741: warning counts are wrong after anno processing Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8619
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     1
/*
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     4
 *
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     8
 *
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    13
 * accompanied this code).
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    14
 *
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    18
 *
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    21
 * questions.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    22
 */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    23
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    24
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    25
/*
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    26
 * @test
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    27
 * @bug 7022337
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    28
 * @summary repeated warnings about bootclasspath not set
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    29
 * @library ../lib
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    30
 * @build JavacTestingAbstractProcessor TestWarnErrorCount
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    31
 * @run main TestWarnErrorCount
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    32
 */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    33
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    34
import java.io.*;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    35
import java.util.*;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    36
import javax.annotation.processing.*;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    37
import javax.lang.model.element.*;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    38
import javax.tools.*;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    39
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    40
@SupportedOptions({"errKind", "msgrWarnKind", "javaWarnKind"})
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    41
public class TestWarnErrorCount extends JavacTestingAbstractProcessor {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    42
    public static void main(String... args) throws Exception {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    43
        new TestWarnErrorCount().run(args);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    44
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    45
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    46
    final int MAX_GEN = 10;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    47
    final int ERROR_ROUND = MAX_GEN / 2; // when to generate error
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    48
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    49
    /**
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    50
     * Type of errors to generate in test case.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    51
     */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    52
    enum ErrorKind {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    53
        /** No errors. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    54
        NONE,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    55
        /** Source code errors. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    56
        JAVA,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    57
        /** Errors reported to Messager. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    58
        MESSAGER,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    59
        /** Error as a result of using -Werror. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    60
        WERROR,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    61
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    62
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    63
    /**
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    64
     * Frequency of warnings in test case.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    65
     */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    66
    enum WarnKind {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    67
        /** No warnings. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    68
        NONE {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    69
            boolean warn(int round) { return false; }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    70
            int count(int start, int end) { return 0; }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    71
        },
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    72
        /** Generate a warning if round count is a multiple of 2. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    73
        EVERY_TWO {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    74
            boolean warn(int round) { return (round % 2) == 0; }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    75
            int count(int start, int end) { return (end / 2) - ((start - 1)/ 2); }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    76
        },
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    77
        /** Generate a warning if round count is a multiple of 3. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    78
        EVERY_THREE {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    79
            boolean warn(int round) { return (round % 3) == 0; }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    80
            int count(int start, int end) { return (end / 3) - ((start - 1)/ 3); }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    81
        },
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    82
        /** Generate a warning every round. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    83
        ALL {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    84
            boolean warn(int round) { return true; }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    85
            int count(int start, int end) { return (end - start + 1); }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    86
        };
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    87
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    88
        /** whether to generate a warning in round 'round'. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    89
        abstract boolean warn(int round);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    90
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    91
        /** number of warnings generated in a range of rounds, inclusive. */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    92
        abstract int count(int start, int end);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    93
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    94
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    95
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    96
    /**
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    97
     * Run test.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    98
     * @param args provide ability to specify particular test cases for debugging.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
    99
     */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   100
    void run(String... args) throws Exception {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   101
        for (String arg: args) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   102
            if (arg.matches("[0-9]+")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   103
                if (testCases == null)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   104
                    testCases = new HashSet<Integer>();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   105
                testCases.add(Integer.valueOf(arg));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   106
            } else if (arg.equals("-stopOnError")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   107
                stopOnError = true;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   108
            } else
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   109
                throw new IllegalArgumentException(arg);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   110
        }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   111
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   112
        run ();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   113
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   114
        if (errors > 0)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   115
            throw new Exception(errors + " errors found");
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   116
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   117
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   118
    /**
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   119
     * Run test.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   120
     */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   121
    void run() throws Exception {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   122
        for (ErrorKind ek: ErrorKind.values()) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   123
            for (WarnKind mwk: WarnKind.values()) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   124
                for (WarnKind jwk: WarnKind.values()) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   125
                    test(ek, mwk, jwk);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   126
                    if (stopOnError && errors > 0)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   127
                        throw new Exception(errors + " errors found");
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   128
                }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   129
            }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   130
        }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   131
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   132
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   133
    boolean stopOnError;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   134
    Set<Integer> testCases;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   135
    int testNum = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   136
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   137
    /**
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   138
     * Run a test case.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   139
     * @param ek    The type of errors to generate
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   140
     * @param mwk   The frequency of Messager warnings to generate
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   141
     * @param jwk   The frequency of Java warnings to generate
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   142
     */
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   143
    void test(ErrorKind ek, WarnKind mwk, WarnKind jwk) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   144
        testNum++;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   145
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   146
        if (testCases != null && !testCases.contains(testNum))
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   147
            return;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   148
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   149
        System.err.println("Test " + testNum + ": ek:" + ek + " mwk:" + mwk + " jwk:" + jwk);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   150
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   151
        File testDir = new File("test" + testNum);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   152
        testDir.mkdirs();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   153
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   154
        String myName = TestWarnErrorCount.class.getSimpleName();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   155
        File testSrc = new File(System.getProperty("test.src"));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   156
        File file = new File(testSrc, myName + ".java");
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   157
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   158
        List<String> args = new ArrayList<String>();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   159
        args.addAll(Arrays.asList(
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   160
            "-XDrawDiagnostics",
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   161
            "-d", testDir.getPath(),
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   162
            "-processor", myName,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   163
//            "-XprintRounds",
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   164
            "-Xlint",
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   165
            "-AerrKind=" + ek,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   166
            "-AmsgrWarnKind=" + mwk,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   167
            "-AjavaWarnKind=" + jwk));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   168
        if (ek == ErrorKind.WERROR)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   169
            args.add("-Werror");
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   170
        args.add(file.getPath());
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   171
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   172
        String out = compile(args.toArray(new String[args.size()]));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   173
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   174
        int errsFound = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   175
        int errsReported = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   176
        int warnsFound = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   177
        int warnsReported = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   178
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   179
        // Scan the output looking for messages of interest.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   180
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   181
        for (String line: out.split("[\r\n]+")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   182
            if (line.contains("compiler.err.")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   183
                errsFound++;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   184
            } else if (line.contains("compiler.warn.")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   185
                warnsFound++;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   186
            } else if (line.matches("[0-9]+ error(?:s?)")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   187
                errsReported = Integer.valueOf(line.substring(0, line.indexOf("error")).trim());
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   188
            } else if (line.matches("[0-9]+ warning(?:s?)")) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   189
                warnsReported = Integer.valueOf(line.substring(0, line.indexOf("warning")).trim());
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   190
            }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   191
        }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   192
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   193
        // Compute the expected number of errors and warnings, based on
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   194
        // the test case parameters.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   195
        // This is highly specific to the annotation processor below, and to
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   196
        // the files it generates.
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   197
        // Generally, the rules are:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   198
        // -- errors stop annotation processing, allowing for one extra "last round"
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   199
        // -- messager warnings are immediate
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   200
        // -- javac warnings are not shown before the final compilation
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   201
        //      (FIXME?  -Werror does not stop processing for java warnings)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   202
        int errsExpected;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   203
        int msgrWarnsExpected;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   204
        int javaWarnsExpected;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   205
        switch (ek) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   206
            case NONE:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   207
                errsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   208
                msgrWarnsExpected = mwk.count(1, 1 + MAX_GEN + 1);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   209
                javaWarnsExpected = jwk.count(2, 1 + MAX_GEN);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   210
                break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   211
            case MESSAGER:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   212
                errsExpected = 1;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   213
                msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   214
                javaWarnsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   215
                break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   216
            case JAVA:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   217
                errsExpected = 2;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   218
                msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   219
                javaWarnsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   220
                break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   221
            case WERROR:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   222
                errsExpected = (mwk != WarnKind.NONE || jwk != WarnKind.NONE) ? 1 : 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   223
                switch (mwk) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   224
                    case NONE:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   225
                        msgrWarnsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   226
                        javaWarnsExpected = (jwk == WarnKind.NONE)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   227
                                ? 0
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   228
                                : 1;  // this is surprising: javac only reports warning in first file
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   229
                        break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   230
                    case EVERY_TWO:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   231
                        msgrWarnsExpected = mwk.count(1, 2 + 1);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   232
                        javaWarnsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   233
                        break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   234
                    case EVERY_THREE:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   235
                        msgrWarnsExpected = mwk.count(1, 3 + 1);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   236
                        javaWarnsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   237
                        break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   238
                    case ALL:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   239
                        msgrWarnsExpected = mwk.count(1, 1 + 1);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   240
                        javaWarnsExpected = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   241
                        break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   242
                    default:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   243
                        throw new IllegalStateException();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   244
                }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   245
                break;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   246
            default:
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   247
                throw new IllegalStateException();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   248
        }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   249
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   250
        int warnsExpected = msgrWarnsExpected + javaWarnsExpected;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   251
        System.err.println("mwk: " + msgrWarnsExpected
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   252
                + ", jwk: " + javaWarnsExpected
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   253
                + ", total: " + warnsExpected);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   254
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   255
        boolean ok;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   256
        ok  = checkEqual("errors", "reported", errsFound, errsReported);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   257
        ok &= checkEqual("errors", "expected", errsFound, errsExpected);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   258
        ok &= checkEqual("warnings", "reported", warnsFound, warnsReported);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   259
        ok &= checkEqual("warnings", "expected", warnsFound, warnsExpected);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   260
        if (ok)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   261
            System.err.println("OK");
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   262
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   263
        System.err.println();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   264
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   265
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   266
    String compile(String... args) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   267
        StringWriter sw = new StringWriter();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   268
        PrintWriter pw = new PrintWriter(sw);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   269
        int rc = com.sun.tools.javac.Main.compile(args, pw);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   270
        pw.close();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   271
        String out = sw.toString();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   272
        if (!out.isEmpty())
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   273
            System.err.println(out);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   274
        if (rc != 0)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   275
            System.err.println("compilation failed: rc=" + rc);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   276
        return out;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   277
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   278
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   279
    boolean checkEqual(String l1, String l2, int i1, int i2) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   280
        if (i1 != i2)
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   281
            error("number of " + l1 + " found, " + i1 + ", does not match number " + l2 + ", " + i2);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   282
        return (i1 == i2);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   283
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   284
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   285
    void error(String msg) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   286
        System.err.println("Error: " + msg);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   287
        errors++;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   288
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   289
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   290
    int errors = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   291
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   292
    // ----- Annotation processor -----
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   293
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   294
    int round = 0;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   295
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   296
    @Override
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   297
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   298
        round++;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   299
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   300
        ErrorKind ek = ErrorKind.valueOf(options.get("errKind"));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   301
        WarnKind mwk = WarnKind.valueOf(options.get("msgrWarnKind"));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   302
        WarnKind jwk = WarnKind.valueOf(options.get("javaWarnKind"));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   303
        messager.printMessage(Diagnostic.Kind.NOTE,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   304
                "Round " + round
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   305
                + " " + roundEnv.getRootElements()
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   306
                + ", last round: " + roundEnv.processingOver());
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   307
        messager.printMessage(Diagnostic.Kind.NOTE,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   308
                "ek: " + ek + ", mwk: " + mwk + ", jwk: " + jwk);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   309
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   310
        if (round <= MAX_GEN && !roundEnv.processingOver())
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   311
            generate("Gen" + round,
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   312
                    (ek == ErrorKind.JAVA) && (round == ERROR_ROUND),
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   313
                    jwk.warn(round));
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   314
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   315
        if (mwk.warn(round))
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   316
            messager.printMessage(Diagnostic.Kind.WARNING, "round " + round);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   317
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   318
        if ((ek == ErrorKind.MESSAGER) && (round == ERROR_ROUND))
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   319
            messager.printMessage(Diagnostic.Kind.ERROR, "round " + round);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   320
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   321
        return true;
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   322
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   323
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   324
    void generate(String name, boolean error, boolean warn) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   325
        try {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   326
            JavaFileObject fo = filer.createSourceFile(name);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   327
            Writer out = fo.openWriter();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   328
            try {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   329
                out.write("class " + name + " {\n"
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   330
                        + (warn ? "    int i = (int) 0;\n" : "")
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   331
                        + (error ? "   ERROR\n" : "")
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   332
                        + "}\n");
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   333
            } finally {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   334
                out.close();
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   335
            }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   336
        } catch (IOException e) {
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   337
            throw new Error(e);
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   338
        }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   339
    }
15a69d0db208 7022741: warning counts are wrong after anno processing
jjg
parents:
diff changeset
   340
}