jjg@340: /* ohair@554: * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. jjg@340: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@340: * jjg@340: * This code is free software; you can redistribute it and/or modify it jjg@340: * under the terms of the GNU General Public License version 2 only, as jjg@340: * published by the Free Software Foundation. jjg@340: * jjg@340: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@340: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@340: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@340: * version 2 for more details (a copy is included in the LICENSE file that jjg@340: * accompanied this code). jjg@340: * jjg@340: * You should have received a copy of the GNU General Public License version jjg@340: * 2 along with this work; if not, write to the Free Software Foundation, jjg@340: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@340: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@340: */ jjg@340: jjg@340: import java.io.*; jjg@340: import java.util.*; jjg@340: import javax.tools.*; jjg@340: import com.sun.tools.javap.*; jjg@340: jjg@340: /* jjg@340: * @test jjg@340: * @bug 4777949 jjg@340: * @summary Warn javap usage on package with simple name jjg@340: */ jjg@340: public class T4777949 { jjg@340: public static void main(String... args) throws Exception { jjg@340: new T4777949().run(); jjg@340: } jjg@340: jjg@340: void run() throws Exception { jjg@340: File javaFile = writeTestFile(); jjg@340: File classFile = compileTestFile(javaFile); jjg@340: jjg@340: test(".", "p.q.r.Test", false); jjg@340: test("p", "q.r.Test", true); jjg@340: test("p/q", "r.Test", true); jjg@340: test("p/q/r", "Test", true); jjg@340: test(".", "p.q.r.Test.Inner", false); jjg@340: test(".", "p.q.r.Test$Inner", false); jjg@340: test("p", "q.r.Test.Inner", true); jjg@340: test("p", "q.r.Test$Inner", true); jjg@340: jjg@340: if (errors > 0) jjg@340: throw new Exception(errors + " errors found"); jjg@340: } jjg@340: jjg@340: void test(String classPath, String className, boolean expectWarnings) { jjg@340: List> diags = jjg@340: javap(Arrays.asList("-classpath", classPath), Arrays.asList(className)); jjg@340: boolean foundWarnings = false; jjg@340: for (Diagnostic d: diags) { jjg@340: if (d.getKind() == Diagnostic.Kind.WARNING) jjg@340: foundWarnings = true; jjg@340: } jjg@340: } jjg@340: jjg@340: jjg@340: File writeTestFile() throws IOException { jjg@340: File f = new File("Test.java"); jjg@340: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); jjg@340: out.println("package p.q.r;"); jjg@340: out.println("class Test { class Inner { } }"); jjg@340: out.close(); jjg@340: return f; jjg@340: } jjg@340: jjg@340: File compileTestFile(File f) { jjg@340: int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() }); jjg@340: if (rc != 0) jjg@340: throw new Error("compilation failed. rc=" + rc); jjg@340: String path = f.getPath(); jjg@340: return new File(path.substring(0, path.length() - 5) + ".class"); jjg@340: } jjg@340: jjg@340: List> javap(List args, List classes) { jjg@340: DiagnosticCollector dc = new DiagnosticCollector(); jjg@340: StringWriter sw = new StringWriter(); jjg@340: PrintWriter pw = new PrintWriter(sw); jjg@340: JavaFileManager fm = JavapFileManager.create(dc, pw); jjg@340: JavapTask t = new JavapTask(pw, fm, dc, args, classes); jjg@340: boolean ok = t.run(); jjg@340: jjg@340: List> diags = dc.getDiagnostics(); jjg@340: jjg@340: if (!ok) jjg@340: error("javap failed unexpectedly"); jjg@340: jjg@340: System.err.println("args=" + args + " classes=" + classes + "\n" jjg@340: + diags + "\n" jjg@340: + sw); jjg@340: jjg@340: return diags; jjg@340: } jjg@340: jjg@340: void error(String msg) { jjg@340: System.err.println("error: " + msg); jjg@340: errors++; jjg@340: } jjg@340: jjg@340: int errors; jjg@340: } jjg@340: