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