test/tools/javap/DescriptorTest.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

     1 /*
     2  * Copyright (c) 2013, 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     8007052
    27  * @summary javap should include the descriptor for a method in verbose mode
    28  */
    30 import java.io.File;
    31 import java.io.FileWriter;
    32 import java.io.IOException;
    33 import java.io.PrintWriter;
    34 import java.io.StringWriter;
    35 import java.util.regex.Pattern;
    37 public class DescriptorTest {
    38     public static void main(String... args) throws Exception {
    39         new DescriptorTest().run();
    40     }
    42     void run() throws Exception {
    43         File srcDir = new File("src");
    44         srcDir.mkdirs();
    45         File classesDir = new File("classes");
    46         classesDir.mkdirs();
    48         File f = writeFile(new File(srcDir, "E.java"), "enum E { A, B }");
    49         javac("-d", classesDir.getPath(), f.getPath());
    50         String out = javap("-p", "-v", new File(classesDir, "E.class").getPath());
    51         Pattern expect = Pattern.compile("\\Qprivate E();\\E\\s+\\Qdescriptor: (Ljava/lang/String;I)V\\E");
    52         checkContains(out, expect);
    53     }
    55     File writeFile(File f, String body) throws IOException {
    56         try (FileWriter out = new FileWriter(f)) {
    57             out.write(body);
    58         }
    59         return f;
    60     }
    62     void javac(String... args) throws Exception {
    63         StringWriter sw = new StringWriter();
    64         PrintWriter pw = new PrintWriter(sw);
    65         int rc = com.sun.tools.javac.Main.compile(args, pw);
    66         pw.flush();
    67         String out = sw.toString();
    68         if (!out.isEmpty())
    69             System.err.println(out);
    70         if (rc != 0)
    71             throw new Exception("compilation failed");
    72     }
    74     String javap(String... args) throws Exception {
    75         StringWriter sw = new StringWriter();
    76         PrintWriter pw = new PrintWriter(sw);
    77         int rc = com.sun.tools.javap.Main.run(args, pw);
    78         pw.flush();
    79         String out = sw.toString();
    80         if (!out.isEmpty())
    81             System.err.println(out);
    82         if (rc != 0)
    83             throw new Exception("javap failed");
    84         return out;
    85     }
    87     void checkContains(String s, Pattern p) throws Exception {
    88         if (!p.matcher(s).find())
    89             throw new Exception("expected pattern not found: " + p);
    90     }
    91 }

mercurial