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