test/tools/javap/MethodParameters.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2287
b2bc7b778287
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial