1/* 2 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 4777949 27 * @summary Warn javap usage on package with simple name 28 * @modules jdk.jdeps/com.sun.tools.javap 29 */ 30 31import java.io.*; 32import java.util.*; 33import javax.tools.*; 34import com.sun.tools.javap.*; 35 36public class T4777949 { 37 public static void main(String... args) throws Exception { 38 new T4777949().run(); 39 } 40 41 void run() throws Exception { 42 File javaFile = writeTestFile(); 43 File classFile = compileTestFile(javaFile); 44 45 test(".", "p.q.r.Test", false); 46 test("p", "q.r.Test", true); 47 test("p/q", "r.Test", true); 48 test("p/q/r", "Test", true); 49 test(".", "p.q.r.Test.Inner", false); 50 test(".", "p.q.r.Test$Inner", false); 51 test("p", "q.r.Test.Inner", true); 52 test("p", "q.r.Test$Inner", true); 53 54 if (errors > 0) 55 throw new Exception(errors + " errors found"); 56 } 57 58 void test(String classPath, String className, boolean expectWarnings) { 59 List<Diagnostic<? extends JavaFileObject>> diags = 60 javap(Arrays.asList("-classpath", classPath), Arrays.asList(className)); 61 boolean foundWarnings = false; 62 for (Diagnostic<? extends JavaFileObject> d: diags) { 63 if (d.getKind() == Diagnostic.Kind.WARNING) 64 foundWarnings = true; 65 } 66 } 67 68 69 File writeTestFile() throws IOException { 70 File f = new File("Test.java"); 71 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 72 out.println("package p.q.r;"); 73 out.println("class Test { class Inner { } }"); 74 out.close(); 75 return f; 76 } 77 78 File compileTestFile(File f) { 79 int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() }); 80 if (rc != 0) 81 throw new Error("compilation failed. rc=" + rc); 82 String path = f.getPath(); 83 return new File(path.substring(0, path.length() - 5) + ".class"); 84 } 85 86 List<Diagnostic<? extends JavaFileObject>> javap(List<String> args, List<String> classes) { 87 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); 88 StringWriter sw = new StringWriter(); 89 PrintWriter pw = new PrintWriter(sw); 90 JavaFileManager fm = JavapFileManager.create(dc, pw); 91 JavapTask t = new JavapTask(pw, fm, dc, args, classes); 92 int ok = t.run(); 93 94 List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics(); 95 96 if (ok != 0) 97 error("javap failed unexpectedly"); 98 99 System.err.println("args=" + args + " classes=" + classes + "\n" 100 + diags + "\n" 101 + sw); 102 103 return diags; 104 } 105 106 void error(String msg) { 107 System.err.println("error: " + msg); 108 errors++; 109 } 110 111 int errors; 112} 113 114