Path: blob/master/test/langtools/tools/javap/MethodParameters.java
41145 views
/*1* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8004727 800564726* @summary javac should generate method parameters correctly.27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;31import java.util.*;3233public class MethodParameters {3435static final String Foo_name = "Foo";36static 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(" +", " ");4344static final String Init0_expected =45(" Foo();\n" +46" descriptor: ()V\n" +47" flags: (0x0000)\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(" +", " ");5556static final String Init1_expected =57(" Foo(int);\n" +58" descriptor: (I)V\n" +59" flags: (0x0000)\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(" +", " ");7071static final String foo0_expected =72(" void foo0();\n" +73" descriptor: ()V\n" +74" flags: (0x0000)\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(" +", " ");8081static final String foo2_expected =82(" void foo2(int, int);\n" +83" descriptor: (II)V\n" +84" flags: (0x0000)\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(" +", " ");9495static final File classesdir = new File("methodparameters");96static final String separator = System.getProperty("line.separator");9798public static void main(String... args) throws Exception {99new MethodParameters().run();100}101102void run() throws Exception {103classesdir.mkdir();104final File Foo_java =105writeFile(classesdir, Foo_name + ".java", Foo_contents);106compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath());107System.out.println("Run javap");108String output =109javap("-c", "-verbose", "-classpath",110classesdir.getPath(), Foo_name);111String normalized =112output.replaceAll(separator, "\n").replaceAll(" +", " ");113114if (!normalized.contains(Init0_expected))115error("Bad output for zero-parameter constructor. Expected\n" +116Init0_expected + "\n" + "but got\n" + normalized);117if (!normalized.contains(Init1_expected))118error("Bad output for one-parameter constructor. Expected\n" +119Init1_expected + "\n" + "but got\n" + normalized);120if (!normalized.contains(foo0_expected))121error("Bad output for zero-parameter method. Expected\n" +122foo0_expected + "\n" + "but got\n" + normalized);123if (!normalized.contains(foo2_expected))124error("Bad output for two-parameter method Expected\n" +125foo2_expected + "\n" + "but got\n" + normalized);126127if (0 != errors)128throw new Exception("MethodParameters test failed with " +129errors + " errors");130}131132String javap(String... args) {133StringWriter sw = new StringWriter();134PrintWriter out = new PrintWriter(sw);135//sun.tools.javap.Main.entry(args);136int rc = com.sun.tools.javap.Main.run(args, out);137if (rc != 0)138throw new Error("javap failed. rc=" + rc);139out.close();140System.out.println(sw);141return sw.toString();142}143144String compile(String... args) throws Exception {145System.err.println("compile: " + Arrays.asList(args));146StringWriter sw = new StringWriter();147PrintWriter pw = new PrintWriter(sw);148int rc = com.sun.tools.javac.Main.compile(args, pw);149pw.close();150String out = sw.toString();151if (out.length() > 0)152System.err.println(out);153if (rc != 0)154error("compilation failed, rc=" + rc);155return out;156}157158File writeFile(File dir, String path, String body) throws IOException {159File f = new File(dir, path);160f.getParentFile().mkdirs();161FileWriter out = new FileWriter(f);162out.write(body);163out.close();164return f;165}166167void error(String msg) {168System.err.println("Error: " + msg);169errors++;170}171172int errors;173174}175176177