langtools/test/tools/javac/T6999210.java
author jlahoda
Tue, 16 Aug 2016 16:43:00 +0200
changeset 40504 0a01f6710c84
parent 30730 d3ce7619db2c
permissions -rw-r--r--
8078561: Error message should be generated once when -source 6 is specified Summary: Code to avoid duplicated errors about features not supported in the current source level moved to Log Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7624
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 7624
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
7624
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     4
 *
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     8
 *
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    13
 * accompanied this code).
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    14
 *
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    18
 *
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    21
 * questions.
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    22
 */
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    23
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    24
/*
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    25
 * @test
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    26
 * @bug 6999210
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    27
 * @summary javac should be able to warn of anomalous conditions in classfiles
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 7624
diff changeset
    28
 * @modules jdk.compiler
7624
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    29
 */
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    30
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    31
import java.io.*;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    32
import java.util.*;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    33
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    34
public class T6999210 {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    35
    public static void main(String... args) throws Exception {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    36
        new T6999210().run();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    37
    }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    38
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    39
    void run() throws Exception {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    40
        File srcDir = new File("src");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    41
        File classesDir = new File("classes");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    42
        classesDir.mkdirs();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    43
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    44
        File c_java = writeFile(srcDir, "C.java", "class C<T> { }");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    45
        compile("-d", classesDir.getPath(), c_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    46
        File c_class = new File(classesDir, "C.class");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    47
        setMajorVersion(c_class, 48);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    48
        File d_java = writeFile(srcDir, "D.java", "class D { C c; }");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    49
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    50
        // verify no warning if -Xlint:classfile not enabled
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    51
        String out1 = compile(
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    52
            "-d", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    53
            "-classpath", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    54
            d_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    55
        if (out1.length() > 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    56
            error("unexpected output from javac");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    57
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    58
        // sanity check of warning when -XDrawDiagnostics not used
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    59
        String out2 = compile(
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    60
            "-d", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    61
            "-classpath", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    62
            "-Xlint:classfile",
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    63
            d_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    64
        if (!out2.contains("[classfile]"))
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    65
            error("expected output \"[classfile]\" not found");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    66
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    67
        // check specific details, using -XDrawDiagnostics
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    68
        String out3 = compile(
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    69
            "-d", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    70
            "-classpath", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    71
            "-Xlint:classfile", "-XDrawDiagnostics",
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    72
            d_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    73
        String expect = "C.class:-:-: compiler.warn.future.attr: Signature, 49, 0, 48, 0";
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    74
        if (!out3.contains(expect))
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    75
            error("expected output \"" + expect + "\" not found");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    76
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    77
        if (errors > 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    78
            throw new Exception(errors + " errors occurred");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    79
    }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    80
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    81
    String compile(String... args) throws Exception {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    82
        System.err.println("compile: " + Arrays.asList(args));
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    83
        StringWriter sw = new StringWriter();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    84
        PrintWriter pw = new PrintWriter(sw);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    85
        int rc = com.sun.tools.javac.Main.compile(args, pw);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    86
        pw.close();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    87
        String out = sw.toString();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    88
        if (out.length() > 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    89
            System.err.println(out);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    90
        if (rc != 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    91
            throw new Exception("compilation failed, rc=" + rc);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    92
        return out;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    93
    }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    94
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    95
    void setMajorVersion(File f, int major) throws IOException {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    96
        int len = (int) f.length();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    97
        byte[] data = new byte[len];
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    98
        try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    99
            in.readFully(data);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   100
        }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   101
        // u4 magic
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   102
        // u2 minor
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   103
        data[6] = (byte) (major >> 8);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   104
        data[7] = (byte) (major & 0xff);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   105
        try (FileOutputStream out = new FileOutputStream(f)) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   106
            out.write(data);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   107
        }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   108
    }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   109
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   110
    File writeFile(File dir, String path, String body) throws IOException {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   111
        File f = new File(dir, path);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   112
        f.getParentFile().mkdirs();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   113
        try (FileWriter out = new FileWriter(f)) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   114
            out.write(body);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   115
        }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   116
        return f;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   117
    }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   118
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   119
    void error(String msg) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   120
        System.err.println("Error: " + msg);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   121
        errors++;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   122
    }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   123
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   124
    int errors;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   125
}