1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javap/T4777949.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,111 @@ 1.4 +/* 1.5 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import java.util.*; 1.29 +import javax.tools.*; 1.30 +import com.sun.tools.javap.*; 1.31 + 1.32 +/* 1.33 + * @test 1.34 + * @bug 4777949 1.35 + * @summary Warn javap usage on package with simple name 1.36 + */ 1.37 +public class T4777949 { 1.38 + public static void main(String... args) throws Exception { 1.39 + new T4777949().run(); 1.40 + } 1.41 + 1.42 + void run() throws Exception { 1.43 + File javaFile = writeTestFile(); 1.44 + File classFile = compileTestFile(javaFile); 1.45 + 1.46 + test(".", "p.q.r.Test", false); 1.47 + test("p", "q.r.Test", true); 1.48 + test("p/q", "r.Test", true); 1.49 + test("p/q/r", "Test", true); 1.50 + test(".", "p.q.r.Test.Inner", false); 1.51 + test(".", "p.q.r.Test$Inner", false); 1.52 + test("p", "q.r.Test.Inner", true); 1.53 + test("p", "q.r.Test$Inner", true); 1.54 + 1.55 + if (errors > 0) 1.56 + throw new Exception(errors + " errors found"); 1.57 + } 1.58 + 1.59 + void test(String classPath, String className, boolean expectWarnings) { 1.60 + List<Diagnostic<? extends JavaFileObject>> diags = 1.61 + javap(Arrays.asList("-classpath", classPath), Arrays.asList(className)); 1.62 + boolean foundWarnings = false; 1.63 + for (Diagnostic<? extends JavaFileObject> d: diags) { 1.64 + if (d.getKind() == Diagnostic.Kind.WARNING) 1.65 + foundWarnings = true; 1.66 + } 1.67 + } 1.68 + 1.69 + 1.70 + File writeTestFile() throws IOException { 1.71 + File f = new File("Test.java"); 1.72 + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 1.73 + out.println("package p.q.r;"); 1.74 + out.println("class Test { class Inner { } }"); 1.75 + out.close(); 1.76 + return f; 1.77 + } 1.78 + 1.79 + File compileTestFile(File f) { 1.80 + int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() }); 1.81 + if (rc != 0) 1.82 + throw new Error("compilation failed. rc=" + rc); 1.83 + String path = f.getPath(); 1.84 + return new File(path.substring(0, path.length() - 5) + ".class"); 1.85 + } 1.86 + 1.87 + List<Diagnostic<? extends JavaFileObject>> javap(List<String> args, List<String> classes) { 1.88 + DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); 1.89 + StringWriter sw = new StringWriter(); 1.90 + PrintWriter pw = new PrintWriter(sw); 1.91 + JavaFileManager fm = JavapFileManager.create(dc, pw); 1.92 + JavapTask t = new JavapTask(pw, fm, dc, args, classes); 1.93 + int ok = t.run(); 1.94 + 1.95 + List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics(); 1.96 + 1.97 + if (ok != 0) 1.98 + error("javap failed unexpectedly"); 1.99 + 1.100 + System.err.println("args=" + args + " classes=" + classes + "\n" 1.101 + + diags + "\n" 1.102 + + sw); 1.103 + 1.104 + return diags; 1.105 + } 1.106 + 1.107 + void error(String msg) { 1.108 + System.err.println("error: " + msg); 1.109 + errors++; 1.110 + } 1.111 + 1.112 + int errors; 1.113 +} 1.114 +