langtools/test/tools/javap/DescriptorTest.java
author duke
Wed, 05 Jul 2017 18:59:39 +0200
changeset 18044 f1a01fb5f958
parent 16291 d4249734ecd8
child 30730 d3ce7619db2c
permissions -rw-r--r--
Merge
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
/*
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    28
 */
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
import java.io.File;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    31
import java.io.FileWriter;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    32
import java.io.IOException;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    33
import java.io.PrintWriter;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    34
import java.io.StringWriter;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    35
import java.util.regex.Pattern;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    36
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    37
public class DescriptorTest {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    38
    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
    39
        new DescriptorTest().run();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    40
    }
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
    void run() throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    43
        File srcDir = new File("src");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    44
        srcDir.mkdirs();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    45
        File classesDir = new File("classes");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    46
        classesDir.mkdirs();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    47
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    48
        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
    49
        javac("-d", classesDir.getPath(), f.getPath());
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    50
        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
    51
        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
    52
        checkContains(out, expect);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    53
    }
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
    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
    56
        try (FileWriter out = new FileWriter(f)) {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    57
            out.write(body);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    58
        }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    59
        return f;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    60
    }
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
    void javac(String... args) throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    63
        StringWriter sw = new StringWriter();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    64
        PrintWriter pw = new PrintWriter(sw);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    65
        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
    66
        pw.flush();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    67
        String out = sw.toString();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    68
        if (!out.isEmpty())
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    69
            System.err.println(out);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    70
        if (rc != 0)
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    71
            throw new Exception("compilation failed");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    72
    }
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
    String javap(String... args) throws Exception {
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    75
        StringWriter sw = new StringWriter();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    76
        PrintWriter pw = new PrintWriter(sw);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    77
        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
    78
        pw.flush();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    79
        String out = sw.toString();
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    80
        if (!out.isEmpty())
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    81
            System.err.println(out);
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    82
        if (rc != 0)
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    83
            throw new Exception("javap failed");
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    84
        return out;
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    85
    }
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
    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
    88
        if (!p.matcher(s).find())
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    89
            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
    90
    }
d4249734ecd8 8007052: javap should include the descriptor for a method in verbose mode
jjg
parents:
diff changeset
    91
}