test/tools/javap/T4777949.java

Thu, 04 Apr 2013 19:05:42 -0700

author
katleman
date
Thu, 04 Apr 2013 19:05:42 -0700
changeset 1662
4a48f3173534
parent 554
9d9f26857129
child 1819
7fe655cad9b1
permissions
-rw-r--r--

Added tag jdk8-b84 for changeset cfb65ca92082

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

mercurial