Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/T4777949.java
32285 views
/*1* Copyright (c) 2008, 2013, 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*/2223import java.io.*;24import java.util.*;25import javax.tools.*;26import com.sun.tools.javap.*;2728/*29* @test30* @bug 477794931* @summary Warn javap usage on package with simple name32*/33public class T4777949 {34public static void main(String... args) throws Exception {35new T4777949().run();36}3738void run() throws Exception {39File javaFile = writeTestFile();40File classFile = compileTestFile(javaFile);4142test(".", "p.q.r.Test", false);43test("p", "q.r.Test", true);44test("p/q", "r.Test", true);45test("p/q/r", "Test", true);46test(".", "p.q.r.Test.Inner", false);47test(".", "p.q.r.Test$Inner", false);48test("p", "q.r.Test.Inner", true);49test("p", "q.r.Test$Inner", true);5051if (errors > 0)52throw new Exception(errors + " errors found");53}5455void test(String classPath, String className, boolean expectWarnings) {56List<Diagnostic<? extends JavaFileObject>> diags =57javap(Arrays.asList("-classpath", classPath), Arrays.asList(className));58boolean foundWarnings = false;59for (Diagnostic<? extends JavaFileObject> d: diags) {60if (d.getKind() == Diagnostic.Kind.WARNING)61foundWarnings = true;62}63}646566File writeTestFile() throws IOException {67File f = new File("Test.java");68PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));69out.println("package p.q.r;");70out.println("class Test { class Inner { } }");71out.close();72return f;73}7475File compileTestFile(File f) {76int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() });77if (rc != 0)78throw new Error("compilation failed. rc=" + rc);79String path = f.getPath();80return new File(path.substring(0, path.length() - 5) + ".class");81}8283List<Diagnostic<? extends JavaFileObject>> javap(List<String> args, List<String> classes) {84DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();85StringWriter sw = new StringWriter();86PrintWriter pw = new PrintWriter(sw);87JavaFileManager fm = JavapFileManager.create(dc, pw);88JavapTask t = new JavapTask(pw, fm, dc, args, classes);89int ok = t.run();9091List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();9293if (ok != 0)94error("javap failed unexpectedly");9596System.err.println("args=" + args + " classes=" + classes + "\n"97+ diags + "\n"98+ sw);99100return diags;101}102103void error(String msg) {104System.err.println("error: " + msg);105errors++;106}107108int errors;109}110111112113