test/tools/javap/MethodParameters.java

Fri, 15 Feb 2013 11:26:11 -0800

author
jjg
date
Fri, 15 Feb 2013 11:26:11 -0800
changeset 1586
9fb4f223a90d
parent 1478
a9cb93cca229
child 2287
b2bc7b778287
permissions
-rw-r--r--

8008313: 8007052 breaks test/tools/javap/MethodParameters.java
Reviewed-by: darcy

jjg@1473 1 /*
jjg@1586 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1473 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1473 4 *
jjg@1473 5 * This code is free software; you can redistribute it and/or modify it
jjg@1473 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1473 7 * published by the Free Software Foundation.
jjg@1473 8 *
jjg@1473 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1473 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1473 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1473 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1473 13 * accompanied this code).
jjg@1473 14 *
jjg@1473 15 * You should have received a copy of the GNU General Public License version
jjg@1473 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1473 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1473 18 *
jjg@1473 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1473 20 * or visit www.oracle.com if you need additional information or have any
jjg@1473 21 * questions.
jjg@1473 22 */
jjg@1473 23
jjg@1473 24 /*
jjg@1473 25 * @test
jjh@1478 26 * @bug 8004727 8005647
jjg@1473 27 * @summary javac should generate method parameters correctly.
jjg@1473 28 */
jjg@1473 29
jjg@1473 30 import java.io.*;
jjg@1473 31 import java.util.*;
jjg@1473 32
jjg@1473 33 public class MethodParameters {
jjg@1473 34
jjg@1473 35 static final String Foo_name = "Foo";
jjg@1473 36 static final String Foo_contents =
jjg@1473 37 ("public class Foo {\n" +
jjg@1473 38 " Foo() {}\n" +
jjg@1473 39 " Foo(int i) {}\n" +
jjg@1473 40 " void foo0() {}\n" +
jjg@1473 41 " void foo2(int j, int k) {}\n" +
jjg@1473 42 "}").replaceAll(" +", " ");
jjg@1586 43
jjg@1473 44 static final String Init0_expected =
jjg@1473 45 (" Foo();\n" +
jjg@1586 46 " descriptor: ()V\n" +
jjg@1473 47 " flags: \n" +
jjg@1473 48 " Code:\n" +
jjg@1473 49 " stack=1, locals=1, args_size=1\n" +
jjg@1473 50 " 0: aload_0 \n" +
jjg@1473 51 " 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +
jjg@1473 52 " 4: return \n" +
jjg@1473 53 " LineNumberTable:\n" +
jjg@1473 54 " line 2: 0").replaceAll(" +", " ");
jjg@1586 55
jjg@1473 56 static final String Init1_expected =
jjg@1473 57 (" Foo(int);\n" +
jjg@1586 58 " descriptor: (I)V\n" +
jjg@1473 59 " flags: \n" +
jjg@1473 60 " Code:\n" +
jjg@1473 61 " stack=1, locals=2, args_size=2\n" +
jjg@1473 62 " 0: aload_0 \n" +
jjg@1473 63 " 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +
jjg@1473 64 " 4: return \n" +
jjg@1473 65 " LineNumberTable:\n" +
jjg@1473 66 " line 3: 0\n" +
jjg@1473 67 " MethodParameters:\n" +
jjg@1473 68 " Name Flags\n" +
jjg@1473 69 " i").replaceAll(" +", " ");
jjg@1586 70
jjg@1473 71 static final String foo0_expected =
jjg@1473 72 (" void foo0();\n" +
jjg@1586 73 " descriptor: ()V\n" +
jjg@1473 74 " flags: \n" +
jjg@1473 75 " Code:\n" +
jjg@1473 76 " stack=0, locals=1, args_size=1\n" +
jjg@1473 77 " 0: return \n" +
jjg@1473 78 " LineNumberTable:\n" +
jjg@1473 79 " line 4: 0").replaceAll(" +", " ");
jjg@1586 80
jjg@1473 81 static final String foo2_expected =
jjg@1473 82 (" void foo2(int, int);\n" +
jjg@1586 83 " descriptor: (II)V\n" +
jjg@1473 84 " flags: \n" +
jjg@1473 85 " Code:\n" +
jjg@1473 86 " stack=0, locals=3, args_size=3\n" +
jjg@1473 87 " 0: return \n" +
jjg@1473 88 " LineNumberTable:\n" +
jjg@1473 89 " line 5: 0\n" +
jjg@1473 90 " MethodParameters:\n" +
jjg@1473 91 " Name Flags\n" +
jjg@1473 92 " j \n" +
jjg@1473 93 " k").replaceAll(" +", " ");
jjg@1473 94
jjg@1473 95 static final File classesdir = new File("methodparameters");
jjg@1473 96 static final String separator = System.getProperty("line.separator");
jjg@1473 97
jjg@1473 98 public static void main(String... args) throws Exception {
jjg@1473 99 new MethodParameters().run();
jjg@1473 100 }
jjg@1473 101
jjg@1473 102 void run() throws Exception {
jjg@1473 103 classesdir.mkdir();
jjg@1473 104 final File Foo_java =
jjg@1473 105 writeFile(classesdir, Foo_name + ".java", Foo_contents);
jjg@1473 106 compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath());
jjg@1473 107 System.out.println("Run javap");
jjg@1473 108 String output =
jjg@1473 109 javap("-c", "-verbose", "-classpath",
jjg@1473 110 classesdir.getPath(), Foo_name);
jjg@1473 111 String normalized =
jjg@1473 112 output.replaceAll(separator, "\n").replaceAll(" +", " ");
jjg@1473 113
jjg@1473 114 if (!normalized.contains(Init0_expected))
jjg@1473 115 error("Bad output for zero-parameter constructor. Expected\n" +
jjg@1473 116 Init0_expected + "\n" + "but got\n" + normalized);
jjg@1473 117 if (!normalized.contains(Init1_expected))
jjg@1473 118 error("Bad output for one-parameter constructor. Expected\n" +
jjg@1473 119 Init1_expected + "\n" + "but got\n" + normalized);
jjg@1473 120 if (!normalized.contains(foo0_expected))
jjg@1473 121 error("Bad output for zero-parameter method. Expected\n" +
jjg@1473 122 foo0_expected + "\n" + "but got\n" + normalized);
jjg@1473 123 if (!normalized.contains(foo2_expected))
jjg@1473 124 error("Bad output for two-parameter method Expected\n" +
jjg@1473 125 foo2_expected + "\n" + "but got\n" + normalized);
jjg@1473 126
jjg@1473 127 if (0 != errors)
jjg@1473 128 throw new Exception("MethodParameters test failed with " +
jjg@1473 129 errors + " errors");
jjg@1473 130 }
jjg@1473 131
jjg@1473 132 String javap(String... args) {
jjg@1473 133 StringWriter sw = new StringWriter();
jjg@1473 134 PrintWriter out = new PrintWriter(sw);
jjg@1473 135 //sun.tools.javap.Main.entry(args);
jjg@1473 136 int rc = com.sun.tools.javap.Main.run(args, out);
jjg@1473 137 if (rc != 0)
jjg@1473 138 throw new Error("javap failed. rc=" + rc);
jjg@1473 139 out.close();
jjg@1473 140 System.out.println(sw);
jjg@1473 141 return sw.toString();
jjg@1473 142 }
jjg@1473 143
jjg@1473 144 String compile(String... args) throws Exception {
jjg@1473 145 System.err.println("compile: " + Arrays.asList(args));
jjg@1473 146 StringWriter sw = new StringWriter();
jjg@1473 147 PrintWriter pw = new PrintWriter(sw);
jjg@1473 148 int rc = com.sun.tools.javac.Main.compile(args, pw);
jjg@1473 149 pw.close();
jjg@1473 150 String out = sw.toString();
jjg@1473 151 if (out.length() > 0)
jjg@1473 152 System.err.println(out);
jjg@1473 153 if (rc != 0)
jjg@1473 154 error("compilation failed, rc=" + rc);
jjg@1473 155 return out;
jjg@1473 156 }
jjg@1473 157
jjg@1473 158 File writeFile(File dir, String path, String body) throws IOException {
jjg@1473 159 File f = new File(dir, path);
jjg@1473 160 f.getParentFile().mkdirs();
jjg@1473 161 FileWriter out = new FileWriter(f);
jjg@1473 162 out.write(body);
jjg@1473 163 out.close();
jjg@1473 164 return f;
jjg@1473 165 }
jjg@1473 166
jjg@1473 167 void error(String msg) {
jjg@1473 168 System.err.println("Error: " + msg);
jjg@1473 169 errors++;
jjg@1473 170 }
jjg@1473 171
jjg@1473 172 int errors;
jjg@1473 173
jjg@1473 174 }

mercurial