langtools/test/tools/javac/T6999210.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 7624 c31b0ea95b37
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
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
/*
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    28
 */
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
import java.io.*;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    31
import java.util.*;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    32
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    33
public class T6999210 {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    34
    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
    35
        new T6999210().run();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    36
    }
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
    void run() throws Exception {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    39
        File srcDir = new File("src");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    40
        File classesDir = new File("classes");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    41
        classesDir.mkdirs();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    42
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    43
        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
    44
        compile("-d", classesDir.getPath(), c_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    45
        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
    46
        setMajorVersion(c_class, 48);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    47
        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
    48
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    49
        // 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
    50
        String out1 = compile(
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    51
            "-d", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    52
            "-classpath", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    53
            d_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    54
        if (out1.length() > 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    55
            error("unexpected output from javac");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    56
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    57
        // 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
    58
        String out2 = compile(
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    59
            "-d", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    60
            "-classpath", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    61
            "-Xlint:classfile",
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    62
            d_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    63
        if (!out2.contains("[classfile]"))
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    64
            error("expected output \"[classfile]\" not found");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    65
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    66
        // check specific details, using -XDrawDiagnostics
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    67
        String out3 = compile(
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    68
            "-d", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    69
            "-classpath", classesDir.getPath(),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    70
            "-Xlint:classfile", "-XDrawDiagnostics",
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    71
            d_java.getPath());
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    72
        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
    73
        if (!out3.contains(expect))
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    74
            error("expected output \"" + expect + "\" not found");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    75
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    76
        if (errors > 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    77
            throw new Exception(errors + " errors occurred");
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    78
    }
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
    String compile(String... args) throws Exception {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    81
        System.err.println("compile: " + Arrays.asList(args));
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    82
        StringWriter sw = new StringWriter();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    83
        PrintWriter pw = new PrintWriter(sw);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    84
        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
    85
        pw.close();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    86
        String out = sw.toString();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    87
        if (out.length() > 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    88
            System.err.println(out);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    89
        if (rc != 0)
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    90
            throw new Exception("compilation failed, rc=" + rc);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    91
        return out;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    92
    }
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
    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
    95
        int len = (int) f.length();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    96
        byte[] data = new byte[len];
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    97
        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
    98
            in.readFully(data);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
    99
        }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   100
        // u4 magic
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   101
        // u2 minor
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   102
        data[6] = (byte) (major >> 8);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   103
        data[7] = (byte) (major & 0xff);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   104
        try (FileOutputStream out = new FileOutputStream(f)) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   105
            out.write(data);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   106
        }
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
    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
   110
        File f = new File(dir, path);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   111
        f.getParentFile().mkdirs();
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   112
        try (FileWriter out = new FileWriter(f)) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   113
            out.write(body);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   114
        }
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   115
        return f;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   116
    }
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
    void error(String msg) {
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   119
        System.err.println("Error: " + msg);
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   120
        errors++;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   121
    }
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
    int errors;
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
diff changeset
   124
}