|
1 /* |
|
2 * Copyright (c) 2011, 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 7031005 |
|
27 * @summary javap prints "extends java.lang.Object" |
|
28 */ |
|
29 |
|
30 import java.io.File; |
|
31 import java.io.IOException; |
|
32 import java.io.PrintWriter; |
|
33 import java.io.StringWriter; |
|
34 import java.net.URI; |
|
35 import java.util.Arrays; |
|
36 import javax.tools.JavaCompiler; |
|
37 import javax.tools.JavaCompiler.CompilationTask; |
|
38 import javax.tools.JavaFileObject; |
|
39 import javax.tools.SimpleJavaFileObject; |
|
40 import javax.tools.StandardJavaFileManager; |
|
41 import javax.tools.StandardLocation; |
|
42 import javax.tools.ToolProvider; |
|
43 |
|
44 public class TestSuperclass { |
|
45 enum ClassKind { |
|
46 CLASS("class"), |
|
47 INTERFACE("interface"); |
|
48 ClassKind(String keyword) { |
|
49 this.keyword = keyword; |
|
50 } |
|
51 final String keyword; |
|
52 } |
|
53 |
|
54 enum GenericKind { |
|
55 NO(""), |
|
56 YES("<T>"); |
|
57 GenericKind(String typarams) { |
|
58 this.typarams = typarams; |
|
59 } |
|
60 final String typarams; |
|
61 } |
|
62 |
|
63 enum SuperKind { |
|
64 NONE(null), |
|
65 SUPER("Super"); |
|
66 SuperKind(String name) { |
|
67 this.name = name; |
|
68 } |
|
69 String extend() { |
|
70 return (name == null) ? "" : "extends " + name; |
|
71 } |
|
72 String decl(ClassKind ck) { |
|
73 return (name == null) ? "" : ck.keyword + " " + name + " { }"; |
|
74 } |
|
75 final String name; |
|
76 } |
|
77 |
|
78 public static void main(String... args) throws Exception { |
|
79 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
|
80 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
|
81 int errors = 0; |
|
82 |
|
83 for (ClassKind ck: ClassKind.values()) { |
|
84 for (GenericKind gk: GenericKind.values()) { |
|
85 for (SuperKind sk: SuperKind.values()) { |
|
86 errors += new TestSuperclass(ck, gk, sk).run(comp, fm); |
|
87 } |
|
88 } |
|
89 } |
|
90 |
|
91 if (errors > 0) |
|
92 throw new Exception(errors + " errors found"); |
|
93 } |
|
94 |
|
95 final ClassKind ck; |
|
96 final GenericKind gk; |
|
97 final SuperKind sk; |
|
98 |
|
99 TestSuperclass(ClassKind ck, GenericKind gk, SuperKind sk) { |
|
100 this.ck = ck; |
|
101 this.gk = gk; |
|
102 this.sk = sk; |
|
103 } |
|
104 |
|
105 int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException { |
|
106 System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk); |
|
107 File testDir = new File(ck + "-" + gk + "-" + sk); |
|
108 testDir.mkdirs(); |
|
109 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir)); |
|
110 |
|
111 JavaSource js = new JavaSource(); |
|
112 System.err.println(js.getCharContent(false)); |
|
113 CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js)); |
|
114 if (!t.call()) |
|
115 throw new Error("compilation failed"); |
|
116 |
|
117 File testClass = new File(testDir, "Test.class"); |
|
118 String out = javap(testClass); |
|
119 |
|
120 // Extract class sig from first line of Java source |
|
121 String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1"); |
|
122 |
|
123 // Extract class sig from line from javap output |
|
124 String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1"); |
|
125 |
|
126 checkEqual("class signature", expect, found); |
|
127 |
|
128 return errors; |
|
129 } |
|
130 |
|
131 String javap(File file) { |
|
132 StringWriter sw = new StringWriter(); |
|
133 PrintWriter pw = new PrintWriter(sw); |
|
134 String[] args = { file.getPath() }; |
|
135 int rc = com.sun.tools.javap.Main.run(args, pw); |
|
136 pw.close(); |
|
137 String out = sw.toString(); |
|
138 if (!out.isEmpty()) |
|
139 System.err.println(out); |
|
140 if (rc != 0) |
|
141 throw new Error("javap failed: rc=" + rc); |
|
142 return out; |
|
143 } |
|
144 |
|
145 void checkEqual(String label, String expect, String found) { |
|
146 if (!expect.equals(found)) |
|
147 error("Unexpected " + label + " found: '" + found + "', expected: '" + expect + "'"); |
|
148 } |
|
149 |
|
150 void error(String msg) { |
|
151 System.err.println("Error: " + msg); |
|
152 errors++; |
|
153 } |
|
154 |
|
155 int errors; |
|
156 |
|
157 class JavaSource extends SimpleJavaFileObject { |
|
158 static final String template = |
|
159 "#CK Test#GK #EK { }\n" |
|
160 + "#SK\n"; |
|
161 final String source; |
|
162 |
|
163 public JavaSource() { |
|
164 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
165 source = template |
|
166 .replace("#CK", ck.keyword) |
|
167 .replace("#GK", gk.typarams) |
|
168 .replace("#EK", sk.extend()) |
|
169 .replace("#SK", sk.decl(ck)); |
|
170 } |
|
171 |
|
172 @Override |
|
173 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
174 return source; |
|
175 } |
|
176 } |
|
177 |
|
178 } |