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