test/tools/javap/MethodParameters.java

Thu, 10 Jan 2013 19:38:57 -0800

author
jjg
date
Thu, 10 Jan 2013 19:38:57 -0800
changeset 1490
fc4cb1577ad6
parent 1478
a9cb93cca229
child 1586
9fb4f223a90d
permissions
-rw-r--r--

8004834: Add doclint support into javadoc
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2010, 2012, 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  */
    24 /*
    25  * @test
    26  * @bug 8004727 8005647
    27  * @summary javac should generate method parameters correctly.
    28  */
    30 import java.io.*;
    31 import java.util.*;
    33 public class MethodParameters {
    35     static final String Foo_name = "Foo";
    36     static 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(" +", " ");
    43     static final String Init0_expected =
    44         ("  Foo();\n" +
    45          "    flags: \n" +
    46          "    Code:\n" +
    47          "      stack=1, locals=1, args_size=1\n" +
    48          "         0: aload_0       \n" +
    49          "         1: invokespecial #1                  // Method java/lang/Object.\"<init>\":()V\n" +
    50          "         4: return        \n" +
    51          "      LineNumberTable:\n" +
    52          "        line 2: 0").replaceAll(" +", " ");
    53     static final String Init1_expected =
    54         ("  Foo(int);\n" +
    55          "    flags: \n" +
    56          "    Code:\n" +
    57          "      stack=1, locals=2, args_size=2\n" +
    58          "         0: aload_0       \n" +
    59          "         1: invokespecial #1                  // Method java/lang/Object.\"<init>\":()V\n" +
    60          "         4: return        \n" +
    61          "      LineNumberTable:\n" +
    62          "        line 3: 0\n" +
    63          "    MethodParameters:\n" +
    64          "      Name                                Flags\n" +
    65          "      i").replaceAll(" +", " ");
    66     static final String foo0_expected =
    67         ("  void foo0();\n" +
    68          "    flags: \n" +
    69          "    Code:\n" +
    70          "      stack=0, locals=1, args_size=1\n" +
    71          "         0: return        \n" +
    72          "      LineNumberTable:\n" +
    73          "        line 4: 0").replaceAll(" +", " ");
    74     static final String foo2_expected =
    75         ("  void foo2(int, int);\n" +
    76          "    flags: \n" +
    77          "    Code:\n" +
    78          "      stack=0, locals=3, args_size=3\n" +
    79          "         0: return        \n" +
    80          "      LineNumberTable:\n" +
    81          "        line 5: 0\n" +
    82          "    MethodParameters:\n" +
    83          "      Name                                Flags\n" +
    84          "      j                              \n" +
    85          "      k").replaceAll(" +", " ");
    87     static final File classesdir = new File("methodparameters");
    88     static final String separator = System.getProperty("line.separator");
    90     public static void main(String... args) throws Exception {
    91         new MethodParameters().run();
    92     }
    94     void run() throws Exception {
    95         classesdir.mkdir();
    96         final File Foo_java =
    97             writeFile(classesdir, Foo_name + ".java", Foo_contents);
    98         compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath());
    99         System.out.println("Run javap");
   100         String output =
   101             javap("-c", "-verbose", "-classpath",
   102                   classesdir.getPath(), Foo_name);
   103         String normalized =
   104             output.replaceAll(separator, "\n").replaceAll(" +", " ");
   106         if (!normalized.contains(Init0_expected))
   107             error("Bad output for zero-parameter constructor.  Expected\n" +
   108                   Init0_expected + "\n" + "but got\n" + normalized);
   109         if (!normalized.contains(Init1_expected))
   110            error("Bad output for one-parameter constructor.  Expected\n" +
   111                  Init1_expected + "\n" + "but got\n" + normalized);
   112         if (!normalized.contains(foo0_expected))
   113            error("Bad output for zero-parameter method.  Expected\n" +
   114                  foo0_expected + "\n" + "but got\n" + normalized);
   115         if (!normalized.contains(foo2_expected))
   116            error("Bad output for two-parameter method  Expected\n" +
   117                  foo2_expected + "\n" + "but got\n" + normalized);
   119         if (0 != errors)
   120             throw new Exception("MethodParameters test failed with " +
   121                                 errors + " errors");
   122     }
   124     String javap(String... args) {
   125         StringWriter sw = new StringWriter();
   126         PrintWriter out = new PrintWriter(sw);
   127         //sun.tools.javap.Main.entry(args);
   128         int rc = com.sun.tools.javap.Main.run(args, out);
   129         if (rc != 0)
   130             throw new Error("javap failed. rc=" + rc);
   131         out.close();
   132         System.out.println(sw);
   133         return sw.toString();
   134     }
   136     String compile(String... args) throws Exception {
   137         System.err.println("compile: " + Arrays.asList(args));
   138         StringWriter sw = new StringWriter();
   139         PrintWriter pw = new PrintWriter(sw);
   140         int rc = com.sun.tools.javac.Main.compile(args, pw);
   141         pw.close();
   142         String out = sw.toString();
   143         if (out.length() > 0)
   144             System.err.println(out);
   145         if (rc != 0)
   146             error("compilation failed, rc=" + rc);
   147         return out;
   148     }
   150     File writeFile(File dir, String path, String body) throws IOException {
   151         File f = new File(dir, path);
   152         f.getParentFile().mkdirs();
   153         FileWriter out = new FileWriter(f);
   154         out.write(body);
   155         out.close();
   156         return f;
   157     }
   159     void error(String msg) {
   160         System.err.println("Error: " + msg);
   161         errors++;
   162     }
   164     int errors;
   166 }

mercurial