test/tools/javap/MethodParameters.java

Sat, 29 Dec 2012 17:33:17 -0800

author
jjg
date
Sat, 29 Dec 2012 17:33:17 -0800
changeset 1473
31780dd06ec7
child 1478
a9cb93cca229
permissions
-rw-r--r--

8004727: Add compiler support for parameter reflection
Reviewed-by: jjg
Contributed-by: eric.mccorkle@oracle.com

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

mercurial