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