Path: blob/master/test/langtools/tools/javap/T4777949.java
41144 views
/*1* Copyright (c) 2008, 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 477794926* @summary Warn javap usage on package with simple name27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;31import java.util.*;32import javax.tools.*;33import com.sun.tools.javap.*;3435public class T4777949 {36public static void main(String... args) throws Exception {37new T4777949().run();38}3940void run() throws Exception {41File javaFile = writeTestFile();42File classFile = compileTestFile(javaFile);4344test(".", "p.q.r.Test", false);45test("p", "q.r.Test", true);46test("p/q", "r.Test", true);47test("p/q/r", "Test", true);48test(".", "p.q.r.Test.Inner", false);49test(".", "p.q.r.Test$Inner", false);50test("p", "q.r.Test.Inner", true);51test("p", "q.r.Test$Inner", true);5253if (errors > 0)54throw new Exception(errors + " errors found");55}5657void test(String classPath, String className, boolean expectWarnings) {58List<Diagnostic<? extends JavaFileObject>> diags =59javap(Arrays.asList("-classpath", classPath), Arrays.asList(className));60boolean foundWarnings = false;61for (Diagnostic<? extends JavaFileObject> d: diags) {62if (d.getKind() == Diagnostic.Kind.WARNING)63foundWarnings = true;64}65}666768File writeTestFile() throws IOException {69File f = new File("Test.java");70PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));71out.println("package p.q.r;");72out.println("class Test { class Inner { } }");73out.close();74return f;75}7677File compileTestFile(File f) {78int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() });79if (rc != 0)80throw new Error("compilation failed. rc=" + rc);81String path = f.getPath();82return new File(path.substring(0, path.length() - 5) + ".class");83}8485List<Diagnostic<? extends JavaFileObject>> javap(List<String> args, List<String> classes) {86DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();87StringWriter sw = new StringWriter();88PrintWriter pw = new PrintWriter(sw);89JavaFileManager fm = JavapFileManager.create(dc, pw);90JavapTask t = new JavapTask(pw, fm, dc, args, classes);91int ok = t.run();9293List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();9495if (ok != 0)96error("javap failed unexpectedly");9798System.err.println("args=" + args + " classes=" + classes + "\n"99+ diags + "\n"100+ sw);101102return diags;103}104105void error(String msg) {106System.err.println("error: " + msg);107errors++;108}109110int errors;111}112113114115