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