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