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