1 /*
2  * Copyright (c) 2009, 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 6729471
27  * @summary javap does not output inner interfaces of an interface
28  * @library /tools/lib
29  * @modules jdk.compiler/com.sun.tools.javac.api
30  *          jdk.compiler/com.sun.tools.javac.main
31  *          jdk.jdeps/com.sun.tools.javap
32  * @build toolbox.ToolBox toolbox.JarTask
33  * @run main T6729471
34  */
35 
36 import java.io.*;
37 import java.net.*;
38 import java.util.*;
39 import javax.tools.*;
40 
41 import toolbox.JarTask;
42 import toolbox.ToolBox;
43 
44 public class T6729471
45 {
main(String... args)46     public static void main(String... args) throws IOException {
47         new T6729471().run();
48     }
49 
run()50     void run() throws IOException {
51         File testClasses = new File(System.getProperty("test.classes"));
52 
53         // simple class
54         verify("java.util.Map",
55                 "public abstract boolean containsKey(java.lang.Object)");
56 
57         // inner class
58         verify("java.util.Map.Entry",
59                 "public abstract K getKey()");
60 
61         // file name
62         verify(new File(testClasses, "T6729471.class").getPath(),
63                 "public static void main(java.lang.String...)");
64 
65         // file url
66         verify(new File(testClasses, "T6729471.class").toURI().toString(),
67                 "public static void main(java.lang.String...)");
68 
69         // jar url
70         File testJar = createJar("test.jar", "java.util.*");
71         try {
72             verify("jar:" + testJar.toURL() + "!/java/util/Map.class",
73                 "public abstract boolean containsKey(java.lang.Object)");
74         } catch (MalformedURLException e) {
75             error(e.toString());
76         }
77 
78         if (errors > 0)
79             throw new Error(errors + " found.");
80     }
81 
createJar(String name, String... paths)82     File createJar(String name, String... paths) throws IOException {
83         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
84         try (JavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
85             File f = new File(name);
86             ToolBox tb = new ToolBox();
87             new JarTask(tb, f.getPath())
88                 .files(fm, StandardLocation.PLATFORM_CLASS_PATH, paths)
89                 .run();
90             return f;
91         }
92     }
93 
verify(String className, String... expects)94     void verify(String className, String... expects) {
95         String output = javap(className);
96         for (String expect: expects) {
97             if (output.indexOf(expect)< 0)
98                 error(expect + " not found");
99         }
100     }
101 
error(String msg)102     void error(String msg) {
103         System.err.println(msg);
104         errors++;
105     }
106 
107     int errors;
108 
javap(String className)109     String javap(String className) {
110         String testClasses = System.getProperty("test.classes", ".");
111         StringWriter sw = new StringWriter();
112         PrintWriter out = new PrintWriter(sw);
113         String[] args = { "-classpath", testClasses, className };
114         int rc = com.sun.tools.javap.Main.run(args, out);
115         out.close();
116         String output = sw.toString();
117         System.out.println("class " + className);
118         System.out.println(output);
119         if (rc != 0)
120             throw new Error("javap failed. rc=" + rc);
121         if (output.indexOf("Error:") != -1)
122             throw new Error("javap reported error.");
123         return output;
124     }
125 }
126 
127