test/tools/javap/MethodParameters.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javap/MethodParameters.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,174 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8004727 8005647
    1.30 + * @summary javac should generate method parameters correctly.
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +
    1.36 +public class MethodParameters {
    1.37 +
    1.38 +    static final String Foo_name = "Foo";
    1.39 +    static final String Foo_contents =
    1.40 +        ("public class Foo {\n" +
    1.41 +         "  Foo() {}\n" +
    1.42 +         "  Foo(int i) {}\n" +
    1.43 +         "  void foo0() {}\n" +
    1.44 +         "  void foo2(int j, int k) {}\n" +
    1.45 +         "}").replaceAll(" +", " ");
    1.46 +
    1.47 +    static final String Init0_expected =
    1.48 +        ("  Foo();\n" +
    1.49 +         "    descriptor: ()V\n" +
    1.50 +         "    flags:\n" +
    1.51 +         "    Code:\n" +
    1.52 +         "      stack=1, locals=1, args_size=1\n" +
    1.53 +         "         0: aload_0\n" +
    1.54 +         "         1: invokespecial #1                  // Method java/lang/Object.\"<init>\":()V\n" +
    1.55 +         "         4: return\n" +
    1.56 +         "      LineNumberTable:\n" +
    1.57 +         "        line 2: 0").replaceAll(" +", " ");
    1.58 +
    1.59 +    static final String Init1_expected =
    1.60 +        ("  Foo(int);\n" +
    1.61 +         "    descriptor: (I)V\n" +
    1.62 +         "    flags:\n" +
    1.63 +         "    Code:\n" +
    1.64 +         "      stack=1, locals=2, args_size=2\n" +
    1.65 +         "         0: aload_0\n" +
    1.66 +         "         1: invokespecial #1                  // Method java/lang/Object.\"<init>\":()V\n" +
    1.67 +         "         4: return\n" +
    1.68 +         "      LineNumberTable:\n" +
    1.69 +         "        line 3: 0\n" +
    1.70 +         "    MethodParameters:\n" +
    1.71 +         "      Name                                Flags\n" +
    1.72 +         "      i").replaceAll(" +", " ");
    1.73 +
    1.74 +    static final String foo0_expected =
    1.75 +        ("  void foo0();\n" +
    1.76 +         "    descriptor: ()V\n" +
    1.77 +         "    flags:\n" +
    1.78 +         "    Code:\n" +
    1.79 +         "      stack=0, locals=1, args_size=1\n" +
    1.80 +         "         0: return\n" +
    1.81 +         "      LineNumberTable:\n" +
    1.82 +         "        line 4: 0").replaceAll(" +", " ");
    1.83 +
    1.84 +    static final String foo2_expected =
    1.85 +        ("  void foo2(int, int);\n" +
    1.86 +         "    descriptor: (II)V\n" +
    1.87 +         "    flags:\n" +
    1.88 +         "    Code:\n" +
    1.89 +         "      stack=0, locals=3, args_size=3\n" +
    1.90 +         "         0: return\n" +
    1.91 +         "      LineNumberTable:\n" +
    1.92 +         "        line 5: 0\n" +
    1.93 +         "    MethodParameters:\n" +
    1.94 +         "      Name                                Flags\n" +
    1.95 +         "      j\n" +
    1.96 +         "      k").replaceAll(" +", " ");
    1.97 +
    1.98 +    static final File classesdir = new File("methodparameters");
    1.99 +    static final String separator = System.getProperty("line.separator");
   1.100 +
   1.101 +    public static void main(String... args) throws Exception {
   1.102 +        new MethodParameters().run();
   1.103 +    }
   1.104 +
   1.105 +    void run() throws Exception {
   1.106 +        classesdir.mkdir();
   1.107 +        final File Foo_java =
   1.108 +            writeFile(classesdir, Foo_name + ".java", Foo_contents);
   1.109 +        compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath());
   1.110 +        System.out.println("Run javap");
   1.111 +        String output =
   1.112 +            javap("-c", "-verbose", "-classpath",
   1.113 +                  classesdir.getPath(), Foo_name);
   1.114 +        String normalized =
   1.115 +            output.replaceAll(separator, "\n").replaceAll(" +", " ");
   1.116 +
   1.117 +        if (!normalized.contains(Init0_expected))
   1.118 +            error("Bad output for zero-parameter constructor.  Expected\n" +
   1.119 +                  Init0_expected + "\n" + "but got\n" + normalized);
   1.120 +        if (!normalized.contains(Init1_expected))
   1.121 +           error("Bad output for one-parameter constructor.  Expected\n" +
   1.122 +                 Init1_expected + "\n" + "but got\n" + normalized);
   1.123 +        if (!normalized.contains(foo0_expected))
   1.124 +           error("Bad output for zero-parameter method.  Expected\n" +
   1.125 +                 foo0_expected + "\n" + "but got\n" + normalized);
   1.126 +        if (!normalized.contains(foo2_expected))
   1.127 +           error("Bad output for two-parameter method  Expected\n" +
   1.128 +                 foo2_expected + "\n" + "but got\n" + normalized);
   1.129 +
   1.130 +        if (0 != errors)
   1.131 +            throw new Exception("MethodParameters test failed with " +
   1.132 +                                errors + " errors");
   1.133 +    }
   1.134 +
   1.135 +    String javap(String... args) {
   1.136 +        StringWriter sw = new StringWriter();
   1.137 +        PrintWriter out = new PrintWriter(sw);
   1.138 +        //sun.tools.javap.Main.entry(args);
   1.139 +        int rc = com.sun.tools.javap.Main.run(args, out);
   1.140 +        if (rc != 0)
   1.141 +            throw new Error("javap failed. rc=" + rc);
   1.142 +        out.close();
   1.143 +        System.out.println(sw);
   1.144 +        return sw.toString();
   1.145 +    }
   1.146 +
   1.147 +    String compile(String... args) throws Exception {
   1.148 +        System.err.println("compile: " + Arrays.asList(args));
   1.149 +        StringWriter sw = new StringWriter();
   1.150 +        PrintWriter pw = new PrintWriter(sw);
   1.151 +        int rc = com.sun.tools.javac.Main.compile(args, pw);
   1.152 +        pw.close();
   1.153 +        String out = sw.toString();
   1.154 +        if (out.length() > 0)
   1.155 +            System.err.println(out);
   1.156 +        if (rc != 0)
   1.157 +            error("compilation failed, rc=" + rc);
   1.158 +        return out;
   1.159 +    }
   1.160 +
   1.161 +    File writeFile(File dir, String path, String body) throws IOException {
   1.162 +        File f = new File(dir, path);
   1.163 +        f.getParentFile().mkdirs();
   1.164 +        FileWriter out = new FileWriter(f);
   1.165 +        out.write(body);
   1.166 +        out.close();
   1.167 +        return f;
   1.168 +    }
   1.169 +
   1.170 +    void error(String msg) {
   1.171 +        System.err.println("Error: " + msg);
   1.172 +        errors++;
   1.173 +    }
   1.174 +
   1.175 +    int errors;
   1.176 +
   1.177 +}

mercurial