author | mchung |
Wed, 27 May 2015 13:25:18 -0700 | |
changeset 30846 | 2b3f379840f0 |
parent 30730 | d3ce7619db2c |
child 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
15031 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
23110
diff
changeset
|
2 |
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
15031 | 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 |
|
15036
b60e18ab9dd7
8005647: langtools/test/tools/javap/MethodParameters.java fails on windows
jjh
parents:
15031
diff
changeset
|
26 |
* @bug 8004727 8005647 |
15031 | 27 |
* @summary javac should generate method parameters correctly. |
30846 | 28 |
* @modules jdk.jdeps |
15031 | 29 |
*/ |
30 |
||
31 |
import java.io.*; |
|
32 |
import java.util.*; |
|
33 |
||
34 |
public class MethodParameters { |
|
35 |
||
36 |
static final String Foo_name = "Foo"; |
|
37 |
static final String Foo_contents = |
|
38 |
("public class Foo {\n" + |
|
39 |
" Foo() {}\n" + |
|
40 |
" Foo(int i) {}\n" + |
|
41 |
" void foo0() {}\n" + |
|
42 |
" void foo2(int j, int k) {}\n" + |
|
43 |
"}").replaceAll(" +", " "); |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
44 |
|
15031 | 45 |
static final String Init0_expected = |
46 |
(" Foo();\n" + |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
47 |
" descriptor: ()V\n" + |
23110 | 48 |
" flags:\n" + |
15031 | 49 |
" Code:\n" + |
50 |
" stack=1, locals=1, args_size=1\n" + |
|
23110 | 51 |
" 0: aload_0\n" + |
15031 | 52 |
" 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" + |
23110 | 53 |
" 4: return\n" + |
15031 | 54 |
" LineNumberTable:\n" + |
55 |
" line 2: 0").replaceAll(" +", " "); |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
56 |
|
15031 | 57 |
static final String Init1_expected = |
58 |
(" Foo(int);\n" + |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
59 |
" descriptor: (I)V\n" + |
23110 | 60 |
" flags:\n" + |
15031 | 61 |
" Code:\n" + |
62 |
" stack=1, locals=2, args_size=2\n" + |
|
23110 | 63 |
" 0: aload_0\n" + |
15031 | 64 |
" 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" + |
23110 | 65 |
" 4: return\n" + |
15031 | 66 |
" LineNumberTable:\n" + |
67 |
" line 3: 0\n" + |
|
68 |
" MethodParameters:\n" + |
|
69 |
" Name Flags\n" + |
|
70 |
" i").replaceAll(" +", " "); |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
71 |
|
15031 | 72 |
static final String foo0_expected = |
73 |
(" void foo0();\n" + |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
74 |
" descriptor: ()V\n" + |
23110 | 75 |
" flags:\n" + |
15031 | 76 |
" Code:\n" + |
77 |
" stack=0, locals=1, args_size=1\n" + |
|
23110 | 78 |
" 0: return\n" + |
15031 | 79 |
" LineNumberTable:\n" + |
80 |
" line 4: 0").replaceAll(" +", " "); |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
81 |
|
15031 | 82 |
static final String foo2_expected = |
83 |
(" void foo2(int, int);\n" + |
|
16299
05b5bf59c9d3
8008313: 8007052 breaks test/tools/javap/MethodParameters.java
jjg
parents:
15036
diff
changeset
|
84 |
" descriptor: (II)V\n" + |
23110 | 85 |
" flags:\n" + |
15031 | 86 |
" Code:\n" + |
87 |
" stack=0, locals=3, args_size=3\n" + |
|
23110 | 88 |
" 0: return\n" + |
15031 | 89 |
" LineNumberTable:\n" + |
90 |
" line 5: 0\n" + |
|
91 |
" MethodParameters:\n" + |
|
92 |
" Name Flags\n" + |
|
23110 | 93 |
" j\n" + |
15031 | 94 |
" k").replaceAll(" +", " "); |
95 |
||
96 |
static final File classesdir = new File("methodparameters"); |
|
97 |
static final String separator = System.getProperty("line.separator"); |
|
98 |
||
99 |
public static void main(String... args) throws Exception { |
|
100 |
new MethodParameters().run(); |
|
101 |
} |
|
102 |
||
103 |
void run() throws Exception { |
|
104 |
classesdir.mkdir(); |
|
105 |
final File Foo_java = |
|
106 |
writeFile(classesdir, Foo_name + ".java", Foo_contents); |
|
107 |
compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath()); |
|
108 |
System.out.println("Run javap"); |
|
109 |
String output = |
|
110 |
javap("-c", "-verbose", "-classpath", |
|
111 |
classesdir.getPath(), Foo_name); |
|
112 |
String normalized = |
|
113 |
output.replaceAll(separator, "\n").replaceAll(" +", " "); |
|
114 |
||
115 |
if (!normalized.contains(Init0_expected)) |
|
116 |
error("Bad output for zero-parameter constructor. Expected\n" + |
|
117 |
Init0_expected + "\n" + "but got\n" + normalized); |
|
118 |
if (!normalized.contains(Init1_expected)) |
|
119 |
error("Bad output for one-parameter constructor. Expected\n" + |
|
120 |
Init1_expected + "\n" + "but got\n" + normalized); |
|
121 |
if (!normalized.contains(foo0_expected)) |
|
122 |
error("Bad output for zero-parameter method. Expected\n" + |
|
123 |
foo0_expected + "\n" + "but got\n" + normalized); |
|
124 |
if (!normalized.contains(foo2_expected)) |
|
125 |
error("Bad output for two-parameter method Expected\n" + |
|
126 |
foo2_expected + "\n" + "but got\n" + normalized); |
|
127 |
||
128 |
if (0 != errors) |
|
129 |
throw new Exception("MethodParameters test failed with " + |
|
130 |
errors + " errors"); |
|
131 |
} |
|
132 |
||
133 |
String javap(String... args) { |
|
134 |
StringWriter sw = new StringWriter(); |
|
135 |
PrintWriter out = new PrintWriter(sw); |
|
136 |
//sun.tools.javap.Main.entry(args); |
|
137 |
int rc = com.sun.tools.javap.Main.run(args, out); |
|
138 |
if (rc != 0) |
|
139 |
throw new Error("javap failed. rc=" + rc); |
|
140 |
out.close(); |
|
141 |
System.out.println(sw); |
|
142 |
return sw.toString(); |
|
143 |
} |
|
144 |
||
145 |
String compile(String... args) throws Exception { |
|
146 |
System.err.println("compile: " + Arrays.asList(args)); |
|
147 |
StringWriter sw = new StringWriter(); |
|
148 |
PrintWriter pw = new PrintWriter(sw); |
|
149 |
int rc = com.sun.tools.javac.Main.compile(args, pw); |
|
150 |
pw.close(); |
|
151 |
String out = sw.toString(); |
|
152 |
if (out.length() > 0) |
|
153 |
System.err.println(out); |
|
154 |
if (rc != 0) |
|
155 |
error("compilation failed, rc=" + rc); |
|
156 |
return out; |
|
157 |
} |
|
158 |
||
159 |
File writeFile(File dir, String path, String body) throws IOException { |
|
160 |
File f = new File(dir, path); |
|
161 |
f.getParentFile().mkdirs(); |
|
162 |
FileWriter out = new FileWriter(f); |
|
163 |
out.write(body); |
|
164 |
out.close(); |
|
165 |
return f; |
|
166 |
} |
|
167 |
||
168 |
void error(String msg) { |
|
169 |
System.err.println("Error: " + msg); |
|
170 |
errors++; |
|
171 |
} |
|
172 |
||
173 |
int errors; |
|
174 |
||
175 |
} |