1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * bug 8071847
27 * @summary Verify proper termination when instance initialization method uses invalid flags
28 * @modules jdk.compiler
29 * @compile  T8071847.java
30 * @run main T8071847
31*/
32
33
34import java.io.*;
35import java.util.*;
36
37public class T8071847 {
38    String testclass="invalidFlags.class";
39    String testclassHexString =
40    "CAFEBABE00000031000D0A0003000A07000B07000C0100063C696E69743E0100" +
41    "03282956010004436F646501000F4C696E654E756D6265725461626C6501000A" +
42    "536F7572636546696C65010009546573742E6A6176610C0004000501000C696E" +
43    "76616C6964466C6167730100106A6176612F6C616E672F4F626A656374002000" +
44    "02000300000000000100A000040005000100060000001D00010001000000052A" +
45    "B70001B10000000100070000000600010000000100010008000000020009";
46
47    String testJavaFile = "testInvalidFlags.java";
48    String testJavaSource ="public class testInvalidFlags extends invalidFlags {" +
49        "invalidFlags c = null;" +
50        "public testInvalidFlags() {  c = new invalidFlags(); }" +
51        "public static void main(String... args) { " +
52        "new testInvalidFlags();}}";
53
54    public static void main(String[] args) throws Exception {
55        new  T8071847().run();
56    }
57
58    public void run() throws IOException {
59        writeHexFile(testclass,testclassHexString);
60        writeTestFile(testJavaFile, testJavaSource);
61        javac(testJavaFile);
62    }
63
64    File writeTestFile(String fname, String source) throws IOException {
65        File f = new File(fname);
66        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
67        out.println(source);
68        out.close();
69        return f;
70    }
71
72    byte[] hexToByte(String str) {
73        char[] CA = str.toCharArray();
74        byte[] byteArry = new byte[str.length()/2];
75        int bi = 0;
76        for (int i = 0; i<CA.length ; i+=2) {
77            char c1 = CA[i], c2=CA[i+1];
78            byteArry[bi++] = (byte)((Character.digit((int)c1,16)<<4) +
79                             Character.digit((int)c2,16));
80        }
81        return byteArry;
82    }
83
84    File writeHexFile(String classFileName, String hexString) throws IOException {
85        File f = new File(classFileName);
86        FileOutputStream output  = new FileOutputStream(f);
87        output.write(hexToByte(hexString));
88        output.close();
89        return f;
90    }
91
92    String javac(String className) {
93        StringWriter sw = new StringWriter();
94        PrintWriter out = new PrintWriter(sw);
95        int rc = 0;
96        List<String> javacArgs = new ArrayList<>();
97        javacArgs.addAll(Arrays.asList("-XDrawDiagnostics", "-cp", ".", "-d", ".", className));
98        rc = com.sun.tools.javac.Main.compile(
99            javacArgs.toArray(new String[javacArgs.size()]),out);
100        out.close();
101        if (rc > 1) {
102            System.out.println(sw.toString());
103            throw new Error("javac " + className + " failed. rc=" + rc);
104        }
105        if (rc != 1 || !sw.toString().contains("compiler.misc.malformed.vararg.method"))
106            throw new RuntimeException("Unexpected output" + sw.toString());
107        return sw.toString();
108    }
109}
110