langtools/test/tools/javap/DescriptorTest.java
author mchung
Wed, 27 May 2015 13:25:18 -0700
changeset 30846 2b3f379840f0
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
permissions -rw-r--r--
8074432: Move jdeps and javap to jdk.jdeps module Reviewed-by: jjg, alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16291
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 16291
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
16291
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     4
 *
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     8
 *
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    13
 * accompanied this code).
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    14
 *
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    18
 *
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    21
 * questions.
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    22
 */
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    23
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    24
/**
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    25
 * @test
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    26
 * @bug     8007052
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    27
 * @summary javap should include the descriptor for a method in verbose mode
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    28
 * @modules jdk.jdeps
16291
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    29
 */
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    30
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    31
import java.io.File;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    32
import java.io.FileWriter;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    33
import java.io.IOException;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    34
import java.io.PrintWriter;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    35
import java.io.StringWriter;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    36
import java.util.regex.Pattern;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    37
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    38
public class DescriptorTest {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    39
    public static void main(String... args) throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    40
        new DescriptorTest().run();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    41
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    42
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    43
    void run() throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    44
        File srcDir = new File("src");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    45
        srcDir.mkdirs();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    46
        File classesDir = new File("classes");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    47
        classesDir.mkdirs();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    48
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    49
        File f = writeFile(new File(srcDir, "E.java"), "enum E { A, B }");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    50
        javac("-d", classesDir.getPath(), f.getPath());
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    51
        String out = javap("-p", "-v", new File(classesDir, "E.class").getPath());
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    52
        Pattern expect = Pattern.compile("\\Qprivate E();\\E\\s+\\Qdescriptor: (Ljava/lang/String;I)V\\E");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    53
        checkContains(out, expect);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    54
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    55
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    56
    File writeFile(File f, String body) throws IOException {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    57
        try (FileWriter out = new FileWriter(f)) {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    58
            out.write(body);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    59
        }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    60
        return f;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    61
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    62
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    63
    void javac(String... args) throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    64
        StringWriter sw = new StringWriter();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    65
        PrintWriter pw = new PrintWriter(sw);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    66
        int rc = com.sun.tools.javac.Main.compile(args, pw);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    67
        pw.flush();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    68
        String out = sw.toString();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    69
        if (!out.isEmpty())
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    70
            System.err.println(out);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    71
        if (rc != 0)
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    72
            throw new Exception("compilation failed");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    73
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    74
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    75
    String javap(String... args) throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    76
        StringWriter sw = new StringWriter();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    77
        PrintWriter pw = new PrintWriter(sw);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    78
        int rc = com.sun.tools.javap.Main.run(args, pw);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    79
        pw.flush();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    80
        String out = sw.toString();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    81
        if (!out.isEmpty())
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    82
            System.err.println(out);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    83
        if (rc != 0)
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    84
            throw new Exception("javap failed");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    85
        return out;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    86
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    87
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    88
    void checkContains(String s, Pattern p) throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    89
        if (!p.matcher(s).find())
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    90
            throw new Exception("expected pattern not found: " + p);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    91
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    92
}