aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8004727 8005647 aoqi@0: * @summary javac should generate method parameters correctly. aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: aoqi@0: public class MethodParameters { aoqi@0: aoqi@0: static final String Foo_name = "Foo"; aoqi@0: static final String Foo_contents = aoqi@0: ("public class Foo {\n" + aoqi@0: " Foo() {}\n" + aoqi@0: " Foo(int i) {}\n" + aoqi@0: " void foo0() {}\n" + aoqi@0: " void foo2(int j, int k) {}\n" + aoqi@0: "}").replaceAll(" +", " "); aoqi@0: aoqi@0: static final String Init0_expected = aoqi@0: (" Foo();\n" + aoqi@0: " descriptor: ()V\n" + aoqi@0: " flags:\n" + aoqi@0: " Code:\n" + aoqi@0: " stack=1, locals=1, args_size=1\n" + aoqi@0: " 0: aload_0\n" + aoqi@0: " 1: invokespecial #1 // Method java/lang/Object.\"\":()V\n" + aoqi@0: " 4: return\n" + aoqi@0: " LineNumberTable:\n" + aoqi@0: " line 2: 0").replaceAll(" +", " "); aoqi@0: aoqi@0: static final String Init1_expected = aoqi@0: (" Foo(int);\n" + aoqi@0: " descriptor: (I)V\n" + aoqi@0: " flags:\n" + aoqi@0: " Code:\n" + aoqi@0: " stack=1, locals=2, args_size=2\n" + aoqi@0: " 0: aload_0\n" + aoqi@0: " 1: invokespecial #1 // Method java/lang/Object.\"\":()V\n" + aoqi@0: " 4: return\n" + aoqi@0: " LineNumberTable:\n" + aoqi@0: " line 3: 0\n" + aoqi@0: " MethodParameters:\n" + aoqi@0: " Name Flags\n" + aoqi@0: " i").replaceAll(" +", " "); aoqi@0: aoqi@0: static final String foo0_expected = aoqi@0: (" void foo0();\n" + aoqi@0: " descriptor: ()V\n" + aoqi@0: " flags:\n" + aoqi@0: " Code:\n" + aoqi@0: " stack=0, locals=1, args_size=1\n" + aoqi@0: " 0: return\n" + aoqi@0: " LineNumberTable:\n" + aoqi@0: " line 4: 0").replaceAll(" +", " "); aoqi@0: aoqi@0: static final String foo2_expected = aoqi@0: (" void foo2(int, int);\n" + aoqi@0: " descriptor: (II)V\n" + aoqi@0: " flags:\n" + aoqi@0: " Code:\n" + aoqi@0: " stack=0, locals=3, args_size=3\n" + aoqi@0: " 0: return\n" + aoqi@0: " LineNumberTable:\n" + aoqi@0: " line 5: 0\n" + aoqi@0: " MethodParameters:\n" + aoqi@0: " Name Flags\n" + aoqi@0: " j\n" + aoqi@0: " k").replaceAll(" +", " "); aoqi@0: aoqi@0: static final File classesdir = new File("methodparameters"); aoqi@0: static final String separator = System.getProperty("line.separator"); aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new MethodParameters().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: classesdir.mkdir(); aoqi@0: final File Foo_java = aoqi@0: writeFile(classesdir, Foo_name + ".java", Foo_contents); aoqi@0: compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath()); aoqi@0: System.out.println("Run javap"); aoqi@0: String output = aoqi@0: javap("-c", "-verbose", "-classpath", aoqi@0: classesdir.getPath(), Foo_name); aoqi@0: String normalized = aoqi@0: output.replaceAll(separator, "\n").replaceAll(" +", " "); aoqi@0: aoqi@0: if (!normalized.contains(Init0_expected)) aoqi@0: error("Bad output for zero-parameter constructor. Expected\n" + aoqi@0: Init0_expected + "\n" + "but got\n" + normalized); aoqi@0: if (!normalized.contains(Init1_expected)) aoqi@0: error("Bad output for one-parameter constructor. Expected\n" + aoqi@0: Init1_expected + "\n" + "but got\n" + normalized); aoqi@0: if (!normalized.contains(foo0_expected)) aoqi@0: error("Bad output for zero-parameter method. Expected\n" + aoqi@0: foo0_expected + "\n" + "but got\n" + normalized); aoqi@0: if (!normalized.contains(foo2_expected)) aoqi@0: error("Bad output for two-parameter method Expected\n" + aoqi@0: foo2_expected + "\n" + "but got\n" + normalized); aoqi@0: aoqi@0: if (0 != errors) aoqi@0: throw new Exception("MethodParameters test failed with " + aoqi@0: errors + " errors"); aoqi@0: } aoqi@0: aoqi@0: String javap(String... args) { aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter out = new PrintWriter(sw); aoqi@0: //sun.tools.javap.Main.entry(args); aoqi@0: int rc = com.sun.tools.javap.Main.run(args, out); aoqi@0: if (rc != 0) aoqi@0: throw new Error("javap failed. rc=" + rc); aoqi@0: out.close(); aoqi@0: System.out.println(sw); aoqi@0: return sw.toString(); aoqi@0: } aoqi@0: aoqi@0: String compile(String... args) throws Exception { aoqi@0: System.err.println("compile: " + Arrays.asList(args)); aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javac.Main.compile(args, pw); aoqi@0: pw.close(); aoqi@0: String out = sw.toString(); aoqi@0: if (out.length() > 0) aoqi@0: System.err.println(out); aoqi@0: if (rc != 0) aoqi@0: error("compilation failed, rc=" + rc); aoqi@0: return out; aoqi@0: } aoqi@0: aoqi@0: File writeFile(File dir, String path, String body) throws IOException { aoqi@0: File f = new File(dir, path); aoqi@0: f.getParentFile().mkdirs(); aoqi@0: FileWriter out = new FileWriter(f); aoqi@0: out.write(body); aoqi@0: out.close(); aoqi@0: return f; aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: }