T4459541.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2008, 2016, 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 4459541
27 * @summary "javap -l" shows line numbers as signed short; they should be unsigned.
28 * @modules jdk.jdeps/com.sun.tools.javap
29 */
30
31import java.io.*;
32
33public class T4459541 {
34    public static void main(String[] args) throws Exception {
35        new T4459541().run();
36    }
37
38    public void run() throws IOException {
39        File javaFile = writeTestFile();
40        File classFile = compileTestFile(javaFile);
41        String output = javap(classFile);
42        verify(output);
43    }
44
45    File writeTestFile() throws IOException {
46        File f = new File("Test.java");
47        out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
48        println("class Test {");
49        println("void begin(int i) {");
50        println("i++;");
51        println("i++;");
52        println("}");
53        while (line < 32750)
54            println("// " + line);
55        println("void before_32767(int i) {");
56        println("i++;");
57        println("i++;");
58        println("}");
59        while (line < 32768-4)
60            println("// " + line);
61        println("void straddle_32768(int i) {");
62        while (line < 32768+4)
63            println("i++;");
64        println("}");
65        while (line < 65520)
66            println("// " + line);
67        println("void between_32768_and_65536(int i) {");
68        println("i++;");
69        println("i++;");
70        println("}");
71        while (line < 65536-4)
72            println("// " + line);
73        println("void straddle_65536(int i) {");
74        while (line < 65536+4)
75            println("i++;");
76        println("}");
77        println("}");
78        out.close();
79        return f;
80    }
81
82    File compileTestFile(File f) {
83        int rc = com.sun.tools.javac.Main.compile(new String[] { f.getPath() });
84        if (rc != 0)
85            throw new Error("compilation failed. rc=" + rc);
86        String path = f.getPath();
87        return new File(path.substring(0, path.length() - 5) + ".class");
88    }
89
90    String javap(File f) {
91        StringWriter sw = new StringWriter();
92        PrintWriter out = new PrintWriter(sw);
93        int rc = com.sun.tools.javap.Main.run(new String[] { "-l", f.getPath() }, out);
94        if (rc != 0)
95            throw new Error("javap failed. rc=" + rc);
96        out.close();
97        return sw.toString();
98    }
99
100    void verify(String output) {
101        System.out.println(output);
102        if (output.indexOf("-") >= 0)
103            throw new Error("- found in output");
104    }
105
106    void println(String text) {
107        out.println(text);
108        line++;
109    }
110
111    PrintWriter out;
112    int line = 1;
113}
114