test/langtools/tools/javac/lib/CompileFail.java
author darcy
Wed, 27 Jun 2018 21:01:12 -0700
changeset 50892 a5557f24b4d4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8205615: Start of release updates for JDK 12 8205621: Increment JDK version for JDK 12 8193292: Add SourceVersion.RELEASE_12 8193290: Add source 12 and target 12 to javac 8205619: Bump maximum recognized class file version to 56 for JDK 12 Reviewed-by: alanb, smarks, jjg, mr, erikj, psandoz, dholmes Contributed-by: erik.joelsson@oracle.com, mikael.vidstedt@oracle.com, joe.darcy@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10202
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     1
/*
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 10638
diff changeset
     2
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
10202
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     4
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     8
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    13
 * accompanied this code).
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    14
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    18
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    21
 * questions.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    22
 */
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    23
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    24
import java.io.*;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    25
import java.util.*;
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10202
diff changeset
    26
import com.sun.tools.javac.main.Main;
10202
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    27
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    28
/*
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    29
 * Utility class to emulate jtreg @compile/fail, but also checking the specific
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    30
 * exit code, given as the first arg.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    31
 */
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    32
public class CompileFail {
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    33
    public static void main(String... args) {
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    34
        if (args.length < 2)
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    35
            throw new IllegalArgumentException("insufficient args");
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    36
        int expected_rc = getReturnCode(args[0]);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    37
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    38
        List<String> javacArgs = new ArrayList<>();
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    39
        javacArgs.addAll(Arrays.asList(
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    40
            "-d", "."
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    41
        ));
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    42
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    43
        File testSrc = new File(System.getProperty("test.src"));
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    44
        for (int i = 1; i < args.length; i++) { // skip first arg
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    45
            String arg = args[i];
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    46
            if (arg.endsWith(".java"))
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    47
                javacArgs.add(new File(testSrc, arg).getPath());
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    48
            else
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    49
                javacArgs.add(arg);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    50
        }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    51
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    52
        int rc = com.sun.tools.javac.Main.compile(
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    53
            javacArgs.toArray(new String[javacArgs.size()]));
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    54
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    55
        if (rc != expected_rc)
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    56
            throw new Error("unexpected exit code: " + rc
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    57
                        + ", expected: " + expected_rc);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    58
    }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    59
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    60
    static int getReturnCode(String name) {
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10202
diff changeset
    61
        return Main.Result.valueOf(name).exitCode;
10202
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    62
    }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    63
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    64
}