Path: blob/master/test/langtools/tools/javap/DescriptorTest.java
41145 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 800705226* @summary javap should include the descriptor for a method in verbose mode27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.File;31import java.io.FileWriter;32import java.io.IOException;33import java.io.PrintWriter;34import java.io.StringWriter;35import java.util.regex.Pattern;3637public class DescriptorTest {38public static void main(String... args) throws Exception {39new DescriptorTest().run();40}4142void run() throws Exception {43File srcDir = new File("src");44srcDir.mkdirs();45File classesDir = new File("classes");46classesDir.mkdirs();4748File f = writeFile(new File(srcDir, "E.java"), "enum E { A, B }");49javac("-d", classesDir.getPath(), f.getPath());50String out = javap("-p", "-v", new File(classesDir, "E.class").getPath());51Pattern expect = Pattern.compile("\\Qprivate E();\\E\\s+\\Qdescriptor: (Ljava/lang/String;I)V\\E");52checkContains(out, expect);53}5455File writeFile(File f, String body) throws IOException {56try (FileWriter out = new FileWriter(f)) {57out.write(body);58}59return f;60}6162void javac(String... args) throws Exception {63StringWriter sw = new StringWriter();64PrintWriter pw = new PrintWriter(sw);65int rc = com.sun.tools.javac.Main.compile(args, pw);66pw.flush();67String out = sw.toString();68if (!out.isEmpty())69System.err.println(out);70if (rc != 0)71throw new Exception("compilation failed");72}7374String javap(String... args) throws Exception {75StringWriter sw = new StringWriter();76PrintWriter pw = new PrintWriter(sw);77int rc = com.sun.tools.javap.Main.run(args, pw);78pw.flush();79String out = sw.toString();80if (!out.isEmpty())81System.err.println(out);82if (rc != 0)83throw new Exception("javap failed");84return out;85}8687void checkContains(String s, Pattern p) throws Exception {88if (!p.matcher(s).find())89throw new Exception("expected pattern not found: " + p);90}91}929394