langtools/test/tools/javac/policy/test3/Test.java
author vromero
Mon, 11 Jul 2016 15:27:52 -0700
changeset 39601 5b37e511ae4b
parent 34752 9c262a013456
child 40835 6ab9ed1abc46
permissions -rw-r--r--
8161020: javac, fold stop compilation options Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2514
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     1
/*
39601
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 34752
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
2514
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     4
 *
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     8
 *
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    13
 * accompanied this code).
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    14
 *
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2514
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2514
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2514
diff changeset
    21
 * questions.
2514
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    22
 */
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    23
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    24
/* @test
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    25
 * @bug 6813059
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    26
 * @summary
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 17799
diff changeset
    27
 * @modules jdk.compiler
2514
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    28
 */
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    29
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    30
import java.io.*;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    31
import java.util.*;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    32
39601
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 34752
diff changeset
    33
// Simple test of -Xshouldstop:at.
2514
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    34
// For each of the permissable values, we compile a file with an error in it,
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    35
// then using -XDverboseCompilePolicy we check that the compilation gets as
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    36
// far as expected, but no further.
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    37
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    38
public class Test {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    39
    enum ShouldStopPolicy {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    40
        BLANK(false, null, "attr"),
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    41
        PROCESS(true, null, "attr"),
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    42
        ATTR(true, "attr", "flow"),
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    43
        FLOW(true, "flow", "desugar"),
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    44
        TRANSTYPES(true, "desugar", "generate"),
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    45
        LOWER(true, "desugar", "generate"),
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    46
        GENERATE(true, "generate", null);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    47
        ShouldStopPolicy(boolean needOption, String expect, String dontExpect) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    48
            this.needOption = needOption;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    49
            this.expect = expect;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    50
            this.dontExpect = dontExpect;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    51
        }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    52
        boolean needOption;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    53
        String expect;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    54
        String dontExpect;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    55
    }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    56
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    57
    enum CompilePolicy {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    58
        BYFILE,
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    59
        BYTODO
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    60
    }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    61
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    62
    public static void main(String... args) throws Exception {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    63
        new Test().run();
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    64
    }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    65
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    66
    public void run() throws Exception {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    67
        for (CompilePolicy cp: CompilePolicy.values()) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    68
            for (ShouldStopPolicy ssp: ShouldStopPolicy.values()) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    69
                test(cp, ssp);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    70
            }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    71
        }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    72
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    73
        if (errors > 0)
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    74
            throw new Exception(errors + " errors occurred");
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    75
    }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    76
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    77
    public void test(CompilePolicy cp, ShouldStopPolicy ssp) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    78
        System.err.println();
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    79
        System.err.println("test " + cp + " " + ssp);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    80
        List<String> args = new ArrayList<String>();
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    81
        args.add("-XDverboseCompilePolicy");
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    82
        args.add("-XDcompilePolicy=" + cp.toString().toLowerCase());
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    83
        args.add("-d");
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    84
        args.add(".");
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    85
        if (ssp.needOption)
39601
5b37e511ae4b 8161020: javac, fold stop compilation options
vromero
parents: 34752
diff changeset
    86
            args.add("-Xshouldstop:at=" + ssp);
2514
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    87
        args.add(new File(System.getProperty("test.src", "."), "A.java").getPath());
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    88
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    89
        StringWriter sw = new StringWriter();
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    90
        PrintWriter pw = new PrintWriter(sw);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    91
        System.err.println("compile " + args);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    92
        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    93
        if (rc == 0)
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    94
            throw new Error("compilation succeeded unexpectedly");
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    95
        //System.err.println(sw);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    96
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    97
        // The following is a workaround for the current javac implementation,
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    98
        // that in bytodo mode, it will still attribute files after syntax errors.
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
    99
        // Changing that behavior may surprise existing users, so for now, we
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   100
        // work around it.
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   101
        if (cp == CompilePolicy.BYTODO && ssp == ShouldStopPolicy.PROCESS)
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   102
            ssp = ShouldStopPolicy.ATTR;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   103
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   104
        boolean foundExpected = (ssp.expect == null);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   105
        String[] lines = sw.toString().split("\n");
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   106
        for (String line: lines) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   107
            if (ssp.expect != null && line.startsWith("[" + ssp.expect))
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   108
                foundExpected = true;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   109
            if (ssp.dontExpect != null && line.startsWith("[" + ssp.dontExpect)) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   110
                error("Unexpected output: " + ssp.dontExpect + "\n" + sw);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   111
                return;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   112
            }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   113
        }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   114
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   115
        if (!foundExpected)
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   116
            error("Expected output not found: " + ssp.expect + "\n" + sw);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   117
    }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   118
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   119
    void error(String message) {
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   120
        System.err.println(message);
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   121
        errors++;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   122
    }
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   123
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   124
    int errors;
f8929e3790b4 6813059: replace use of JavaCompiler.errorCount with shouldContinue
jjg
parents:
diff changeset
   125
}