test/tools/javap/MethodParameters.java

changeset 1473
31780dd06ec7
child 1478
a9cb93cca229
equal deleted inserted replaced
1472:0c244701188e 1473:31780dd06ec7
1 /*
2 * Copyright (c) 2010, 2012, 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 * @summary javac should generate method parameters correctly.
27 */
28
29 import java.io.*;
30 import java.util.*;
31
32 public class MethodParameters {
33
34 static final String Foo_name = "Foo";
35 static final String Foo_contents =
36 ("public class Foo {\n" +
37 " Foo() {}\n" +
38 " Foo(int i) {}\n" +
39 " void foo0() {}\n" +
40 " void foo2(int j, int k) {}\n" +
41 "}").replaceAll(" +", " ");
42 static final String Init0_expected =
43 (" Foo();\n" +
44 " flags: \n" +
45 " Code:\n" +
46 " stack=1, locals=1, args_size=1\n" +
47 " 0: aload_0 \n" +
48 " 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +
49 " 4: return \n" +
50 " LineNumberTable:\n" +
51 " line 2: 0").replaceAll(" +", " ");
52 static final String Init1_expected =
53 (" Foo(int);\n" +
54 " flags: \n" +
55 " Code:\n" +
56 " stack=1, locals=2, args_size=2\n" +
57 " 0: aload_0 \n" +
58 " 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +
59 " 4: return \n" +
60 " LineNumberTable:\n" +
61 " line 3: 0\n" +
62 " MethodParameters:\n" +
63 " Name Flags\n" +
64 " i").replaceAll(" +", " ");
65 static final String foo0_expected =
66 (" void foo0();\n" +
67 " flags: \n" +
68 " Code:\n" +
69 " stack=0, locals=1, args_size=1\n" +
70 " 0: return \n" +
71 " LineNumberTable:\n" +
72 " line 4: 0").replaceAll(" +", " ");
73 static final String foo2_expected =
74 (" void foo2(int, int);\n" +
75 " flags: \n" +
76 " Code:\n" +
77 " stack=0, locals=3, args_size=3\n" +
78 " 0: return \n" +
79 " LineNumberTable:\n" +
80 " line 5: 0\n" +
81 " MethodParameters:\n" +
82 " Name Flags\n" +
83 " j \n" +
84 " k").replaceAll(" +", " ");
85
86 static final File classesdir = new File("methodparameters");
87 static final String separator = System.getProperty("line.separator");
88
89 public static void main(String... args) throws Exception {
90 new MethodParameters().run();
91 }
92
93 void run() throws Exception {
94 classesdir.mkdir();
95 final File Foo_java =
96 writeFile(classesdir, Foo_name + ".java", Foo_contents);
97 compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath());
98 System.out.println("Run javap");
99 String output =
100 javap("-c", "-verbose", "-classpath",
101 classesdir.getPath(), Foo_name);
102 String normalized =
103 output.replaceAll(separator, "\n").replaceAll(" +", " ");
104
105 if (!normalized.contains(Init0_expected))
106 error("Bad output for zero-parameter constructor. Expected\n" +
107 Init0_expected + "\n" + "but got\n" + normalized);
108 if (!normalized.contains(Init1_expected))
109 error("Bad output for one-parameter constructor. Expected\n" +
110 Init1_expected + "\n" + "but got\n" + normalized);
111 if (!normalized.contains(foo0_expected))
112 error("Bad output for zero-parameter method. Expected\n" +
113 foo0_expected + "\n" + "but got\n" + normalized);
114 if (!normalized.contains(foo2_expected))
115 error("Bad output for two-parameter method Expected\n" +
116 foo2_expected + "\n" + "but got\n" + normalized);
117
118 if (0 != errors)
119 throw new Exception("MethodParameters test failed with " +
120 errors + " errors");
121 }
122
123 String javap(String... args) {
124 StringWriter sw = new StringWriter();
125 PrintWriter out = new PrintWriter(sw);
126 //sun.tools.javap.Main.entry(args);
127 int rc = com.sun.tools.javap.Main.run(args, out);
128 if (rc != 0)
129 throw new Error("javap failed. rc=" + rc);
130 out.close();
131 System.out.println(sw);
132 return sw.toString();
133 }
134
135 String compile(String... args) throws Exception {
136 System.err.println("compile: " + Arrays.asList(args));
137 StringWriter sw = new StringWriter();
138 PrintWriter pw = new PrintWriter(sw);
139 int rc = com.sun.tools.javac.Main.compile(args, pw);
140 pw.close();
141 String out = sw.toString();
142 if (out.length() > 0)
143 System.err.println(out);
144 if (rc != 0)
145 error("compilation failed, rc=" + rc);
146 return out;
147 }
148
149 File writeFile(File dir, String path, String body) throws IOException {
150 File f = new File(dir, path);
151 f.getParentFile().mkdirs();
152 FileWriter out = new FileWriter(f);
153 out.write(body);
154 out.close();
155 return f;
156 }
157
158 void error(String msg) {
159 System.err.println("Error: " + msg);
160 errors++;
161 }
162
163 int errors;
164
165 }

mercurial