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