2
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
* @bug 8004727 8005647
27
* @summary javac should generate method parameters correctly.
28
* @modules jdk.jdeps/com.sun.tools.javap
34
public class MethodParameters {
36
static final String Foo_name = "Foo";
37
static final String Foo_contents =
38
("public class Foo {\n" +
42
" void foo2(int j, int k) {}\n" +
43
"}").replaceAll(" +", " ");
45
static final String Init0_expected =
47
" descriptor: ()V\n" +
48
" flags: (0x0000)\n" +
50
" stack=1, locals=1, args_size=1\n" +
52
" 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +
54
" LineNumberTable:\n" +
55
" line 2: 0").replaceAll(" +", " ");
57
static final String Init1_expected =
59
" descriptor: (I)V\n" +
60
" flags: (0x0000)\n" +
62
" stack=1, locals=2, args_size=2\n" +
64
" 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +
66
" LineNumberTable:\n" +
68
" MethodParameters:\n" +
70
" i").replaceAll(" +", " ");
72
static final String foo0_expected =
74
" descriptor: ()V\n" +
75
" flags: (0x0000)\n" +
77
" stack=0, locals=1, args_size=1\n" +
79
" LineNumberTable:\n" +
80
" line 4: 0").replaceAll(" +", " ");
82
static final String foo2_expected =
83
(" void foo2(int, int);\n" +
84
" descriptor: (II)V\n" +
85
" flags: (0x0000)\n" +
87
" stack=0, locals=3, args_size=3\n" +
89
" LineNumberTable:\n" +
91
" MethodParameters:\n" +
94
" k").replaceAll(" +", " ");
96
static final File classesdir = new File("methodparameters");
97
static final String separator = System.getProperty("line.separator");
99
public static void main(String... args) throws Exception {
100
new MethodParameters().run();
103
void run() throws Exception {
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");
110
javap("-c", "-verbose", "-classpath",
111
classesdir.getPath(), Foo_name);
113
output.replaceAll(separator, "\n").replaceAll(" +", " ");
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);
129
throw new Exception("MethodParameters test failed with " +
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);
139
throw new Error("javap failed. rc=" + rc);
141
System.out.println(sw);
142
return sw.toString();
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);
151
String out = sw.toString();
152
if (out.length() > 0)
153
System.err.println(out);
155
error("compilation failed, rc=" + rc);
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);
168
void error(String msg) {
169
System.err.println("Error: " + msg);