Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/MethodParameters.java
32285 views
/*1* Copyright (c) 2010, 2014, 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*/2829import java.io.*;30import java.util.*;3132public class MethodParameters {3334static final String Foo_name = "Foo";35static final String Foo_contents =36("public class Foo {\n" +37" Foo() {}\n" +38" Foo(int i) {}\n" +39" void foo0() {}\n" +40" void foo2(int j, int k) {}\n" +41"}").replaceAll(" +", " ");4243static final String Init0_expected =44(" Foo();\n" +45" descriptor: ()V\n" +46" flags:\n" +47" Code:\n" +48" stack=1, locals=1, args_size=1\n" +49" 0: aload_0\n" +50" 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +51" 4: return\n" +52" LineNumberTable:\n" +53" line 2: 0").replaceAll(" +", " ");5455static final String Init1_expected =56(" Foo(int);\n" +57" descriptor: (I)V\n" +58" flags:\n" +59" Code:\n" +60" stack=1, locals=2, args_size=2\n" +61" 0: aload_0\n" +62" 1: invokespecial #1 // Method java/lang/Object.\"<init>\":()V\n" +63" 4: return\n" +64" LineNumberTable:\n" +65" line 3: 0\n" +66" MethodParameters:\n" +67" Name Flags\n" +68" i").replaceAll(" +", " ");6970static final String foo0_expected =71(" void foo0();\n" +72" descriptor: ()V\n" +73" flags:\n" +74" Code:\n" +75" stack=0, locals=1, args_size=1\n" +76" 0: return\n" +77" LineNumberTable:\n" +78" line 4: 0").replaceAll(" +", " ");7980static final String foo2_expected =81(" void foo2(int, int);\n" +82" descriptor: (II)V\n" +83" flags:\n" +84" Code:\n" +85" stack=0, locals=3, args_size=3\n" +86" 0: return\n" +87" LineNumberTable:\n" +88" line 5: 0\n" +89" MethodParameters:\n" +90" Name Flags\n" +91" j\n" +92" k").replaceAll(" +", " ");9394static final File classesdir = new File("methodparameters");95static final String separator = System.getProperty("line.separator");9697public static void main(String... args) throws Exception {98new MethodParameters().run();99}100101void run() throws Exception {102classesdir.mkdir();103final File Foo_java =104writeFile(classesdir, Foo_name + ".java", Foo_contents);105compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath());106System.out.println("Run javap");107String output =108javap("-c", "-verbose", "-classpath",109classesdir.getPath(), Foo_name);110String normalized =111output.replaceAll(separator, "\n").replaceAll(" +", " ");112113if (!normalized.contains(Init0_expected))114error("Bad output for zero-parameter constructor. Expected\n" +115Init0_expected + "\n" + "but got\n" + normalized);116if (!normalized.contains(Init1_expected))117error("Bad output for one-parameter constructor. Expected\n" +118Init1_expected + "\n" + "but got\n" + normalized);119if (!normalized.contains(foo0_expected))120error("Bad output for zero-parameter method. Expected\n" +121foo0_expected + "\n" + "but got\n" + normalized);122if (!normalized.contains(foo2_expected))123error("Bad output for two-parameter method Expected\n" +124foo2_expected + "\n" + "but got\n" + normalized);125126if (0 != errors)127throw new Exception("MethodParameters test failed with " +128errors + " errors");129}130131String javap(String... args) {132StringWriter sw = new StringWriter();133PrintWriter out = new PrintWriter(sw);134//sun.tools.javap.Main.entry(args);135int rc = com.sun.tools.javap.Main.run(args, out);136if (rc != 0)137throw new Error("javap failed. rc=" + rc);138out.close();139System.out.println(sw);140return sw.toString();141}142143String compile(String... args) throws Exception {144System.err.println("compile: " + Arrays.asList(args));145StringWriter sw = new StringWriter();146PrintWriter pw = new PrintWriter(sw);147int rc = com.sun.tools.javac.Main.compile(args, pw);148pw.close();149String out = sw.toString();150if (out.length() > 0)151System.err.println(out);152if (rc != 0)153error("compilation failed, rc=" + rc);154return out;155}156157File writeFile(File dir, String path, String body) throws IOException {158File f = new File(dir, path);159f.getParentFile().mkdirs();160FileWriter out = new FileWriter(f);161out.write(body);162out.close();163return f;164}165166void error(String msg) {167System.err.println("Error: " + msg);168errors++;169}170171int errors;172173}174175176