Tue, 08 Jan 2013 13:47:57 +0000
8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh
1 /*
2 * Copyright (c) 2011, 2013, 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 */
24 /*
25 * @test
26 * @bug 7093325
27 * @summary Redundant entry in bytecode exception table
28 * @library lib
29 * @build JavacTestingAbstractThreadedTest
30 * @run main T7093325
31 */
33 import java.io.File;
34 import java.net.URI;
35 import java.util.Arrays;
36 import javax.tools.JavaCompiler;
37 import javax.tools.JavaFileObject;
38 import javax.tools.SimpleJavaFileObject;
39 import javax.tools.ToolProvider;
41 import com.sun.source.util.JavacTask;
42 import com.sun.tools.classfile.Attribute;
43 import com.sun.tools.classfile.ClassFile;
44 import com.sun.tools.classfile.Code_attribute;
45 import com.sun.tools.classfile.ConstantPool.*;
46 import com.sun.tools.classfile.Method;
48 public class T7093325
49 extends JavacTestingAbstractThreadedTest
50 implements Runnable {
52 enum StatementKind {
53 THROW("throw new RuntimeException();", false, false),
54 RETURN_NONEMPTY("System.out.println(); return;", true, false),
55 RETURN_EMPTY("return;", true, true),
56 APPLY("System.out.println();", true, false);
58 String stmt;
59 boolean canInline;
60 boolean empty;
62 private StatementKind(String stmt, boolean canInline, boolean empty) {
63 this.stmt = stmt;
64 this.canInline = canInline;
65 this.empty = empty;
66 }
67 }
69 enum CatchArity {
70 NONE(""),
71 ONE("catch (A a) { #S1 }"),
72 TWO("catch (B b) { #S2 }"),
73 THREE("catch (C c) { #S3 }"),
74 FOUR("catch (D d) { #S4 }");
76 String catchStr;
78 private CatchArity(String catchStr) {
79 this.catchStr = catchStr;
80 }
82 String catchers() {
83 if (this.ordinal() == 0) {
84 return catchStr;
85 } else {
86 return CatchArity.values()[this.ordinal() - 1].catchers() +
87 catchStr;
88 }
89 }
90 }
92 public static void main(String... args) throws Exception {
93 for (CatchArity ca : CatchArity.values()) {
94 for (StatementKind stmt0 : StatementKind.values()) {
95 if (ca.ordinal() == 0) {
96 pool.execute(new T7093325(ca, stmt0));
97 continue;
98 }
99 for (StatementKind stmt1 : StatementKind.values()) {
100 if (ca.ordinal() == 1) {
101 pool.execute(new T7093325(ca, stmt0, stmt1));
102 continue;
103 }
104 for (StatementKind stmt2 : StatementKind.values()) {
105 if (ca.ordinal() == 2) {
106 pool.execute(new T7093325(ca, stmt0, stmt1, stmt2));
107 continue;
108 }
109 for (StatementKind stmt3 : StatementKind.values()) {
110 if (ca.ordinal() == 3) {
111 pool.execute(
112 new T7093325(ca, stmt0, stmt1, stmt2, stmt3));
113 continue;
114 }
115 for (StatementKind stmt4 : StatementKind.values()) {
116 if (ca.ordinal() == 4) {
117 pool.execute(
118 new T7093325(ca, stmt0, stmt1,
119 stmt2, stmt3, stmt4));
120 continue;
121 }
122 for (StatementKind stmt5 : StatementKind.values()) {
123 pool.execute(
124 new T7093325(ca, stmt0, stmt1, stmt2,
125 stmt3, stmt4, stmt5));
126 }
127 }
128 }
129 }
130 }
131 }
132 }
134 checkAfterExec();
135 }
137 /** instance decls **/
139 CatchArity ca;
140 StatementKind[] stmts;
142 public T7093325(CatchArity ca, StatementKind... stmts) {
143 this.ca = ca;
144 this.stmts = stmts;
145 }
147 @Override
148 public void run() {
149 int id = checkCount.incrementAndGet();
150 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
151 JavaSource source = new JavaSource(id);
152 JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), null,
153 null, null, Arrays.asList(source));
154 ct.call();
155 verifyBytecode(source, id);
156 }
158 void verifyBytecode(JavaSource source, int id) {
159 boolean lastInlined = false;
160 boolean hasCode = false;
161 int gapsCount = 0;
162 for (int i = 0; i < stmts.length ; i++) {
163 lastInlined = stmts[i].canInline;
164 hasCode = hasCode || !stmts[i].empty;
165 if (lastInlined && hasCode) {
166 hasCode = false;
167 gapsCount++;
168 }
169 }
170 if (!lastInlined) {
171 gapsCount++;
172 }
174 //System.out.printf("gaps %d \n %s \n", gapsCount, source.toString());
176 File compiledTest = new File(String.format("Test%s.class", id));
177 try {
178 ClassFile cf = ClassFile.read(compiledTest);
179 if (cf == null) {
180 throw new Error("Classfile not found: " +
181 compiledTest.getName());
182 }
184 Method test_method = null;
185 for (Method m : cf.methods) {
186 if (m.getName(cf.constant_pool).equals("test")) {
187 test_method = m;
188 break;
189 }
190 }
192 if (test_method == null) {
193 throw new Error("Method test() not found in class Test");
194 }
196 Code_attribute code = null;
197 for (Attribute a : test_method.attributes) {
198 if (a.getName(cf.constant_pool).equals(Attribute.Code)) {
199 code = (Code_attribute)a;
200 break;
201 }
202 }
204 if (code == null) {
205 throw new Error("Code attribute not found in method test()");
206 }
208 int actualGapsCount = 0;
209 for (int i = 0; i < code.exception_table_langth ; i++) {
210 int catchType = code.exception_table[i].catch_type;
211 if (catchType == 0) { //any
212 actualGapsCount++;
213 }
214 }
216 if (actualGapsCount != gapsCount) {
217 throw new Error("Bad exception table for test()\n" +
218 "expected gaps: " + gapsCount + "\n" +
219 "found gaps: " + actualGapsCount + "\n" +
220 source);
221 }
222 } catch (Exception e) {
223 e.printStackTrace();
224 throw new Error("error reading " + compiledTest +": " + e);
225 }
227 }
229 class JavaSource extends SimpleJavaFileObject {
231 static final String source_template =
232 "class A extends RuntimeException {} \n" +
233 "class B extends RuntimeException {} \n" +
234 "class C extends RuntimeException {} \n" +
235 "class D extends RuntimeException {} \n" +
236 "class E extends RuntimeException {} \n" +
237 "class Test#ID {\n" +
238 " void test() {\n" +
239 " try { #S0 } #C finally { System.out.println(); }\n" +
240 " }\n" +
241 "}";
243 String source;
245 public JavaSource(int id) {
246 super(URI.create(String.format("myfo:/Test%s.java", id)),
247 JavaFileObject.Kind.SOURCE);
248 source = source_template.replace("#C", ca.catchers());
249 source = source.replace("#S0", stmts[0].stmt);
250 source = source.replace("#ID", String.valueOf(id));
251 for (int i = 1; i < ca.ordinal() + 1; i++) {
252 source = source.replace("#S" + i, stmts[i].stmt);
253 }
254 }
256 @Override
257 public String toString() {
258 return source;
259 }
261 @Override
262 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
263 return source;
264 }
265 }
267 }