64 import java.util.Arrays; |
66 import java.util.Arrays; |
65 import java.util.Locale; |
67 import java.util.Locale; |
66 |
68 |
67 import javax.tools.Diagnostic; |
69 import javax.tools.Diagnostic; |
68 import javax.tools.JavaCompiler; |
70 import javax.tools.JavaCompiler; |
69 import javax.tools.JavaFileManager; |
|
70 import javax.tools.JavaFileObject; |
71 import javax.tools.JavaFileObject; |
71 import javax.tools.SimpleJavaFileObject; |
72 import javax.tools.SimpleJavaFileObject; |
72 import javax.tools.StandardJavaFileManager; |
73 import javax.tools.StandardJavaFileManager; |
73 import javax.tools.ToolProvider; |
74 import javax.tools.ToolProvider; |
74 |
75 |
75 import static com.sun.tools.javac.jvm.ClassFile.*; |
76 import static com.sun.tools.javac.jvm.ClassFile.*; |
76 |
77 |
77 public class TestInvokeDynamic { |
78 public class TestInvokeDynamic |
78 |
79 extends JavacTestingAbstractThreadedTest |
79 static int checkCount = 0; |
80 implements Runnable { |
80 |
81 |
81 enum StaticArgumentKind { |
82 enum StaticArgumentKind { |
82 STRING("Hello!", "String", "Ljava/lang/String;") { |
83 STRING("Hello!", "String", "Ljava/lang/String;") { |
83 @Override |
84 @Override |
84 boolean check(CPInfo cpInfo) throws Exception { |
85 boolean check(CPInfo cpInfo) throws Exception { |
85 return (cpInfo instanceof CONSTANT_String_info) && |
86 return (cpInfo instanceof CONSTANT_String_info) && |
86 ((CONSTANT_String_info)cpInfo).getString().equals(value); |
87 ((CONSTANT_String_info)cpInfo).getString() |
|
88 .equals(value); |
87 } |
89 } |
88 }, |
90 }, |
89 CLASS(null, "Class<?>", "Ljava/lang/Class;") { |
91 CLASS(null, "Class<?>", "Ljava/lang/Class;") { |
90 @Override |
92 @Override |
91 boolean check(CPInfo cpInfo) throws Exception { |
93 boolean check(CPInfo cpInfo) throws Exception { |
92 return (cpInfo instanceof CONSTANT_Class_info) && |
94 return (cpInfo instanceof CONSTANT_Class_info) && |
93 ((CONSTANT_Class_info)cpInfo).getName().equals("java/lang/String"); |
95 ((CONSTANT_Class_info)cpInfo).getName() |
|
96 .equals("java/lang/String"); |
94 } |
97 } |
95 }, |
98 }, |
96 INTEGER(1, "int", "I") { |
99 INTEGER(1, "int", "I") { |
97 @Override |
100 @Override |
98 boolean check(CPInfo cpInfo) throws Exception { |
101 boolean check(CPInfo cpInfo) throws Exception { |
99 return (cpInfo instanceof CONSTANT_Integer_info) && |
102 return (cpInfo instanceof CONSTANT_Integer_info) && |
100 ((CONSTANT_Integer_info)cpInfo).value == ((Integer)value).intValue(); |
103 ((CONSTANT_Integer_info)cpInfo).value == |
|
104 ((Integer)value).intValue(); |
101 } |
105 } |
102 }, |
106 }, |
103 LONG(1L, "long", "J") { |
107 LONG(1L, "long", "J") { |
104 @Override |
108 @Override |
105 boolean check(CPInfo cpInfo) throws Exception { |
109 boolean check(CPInfo cpInfo) throws Exception { |
106 return (cpInfo instanceof CONSTANT_Long_info) && |
110 return (cpInfo instanceof CONSTANT_Long_info) && |
107 ((CONSTANT_Long_info)cpInfo).value == ((Long)value).longValue(); |
111 ((CONSTANT_Long_info)cpInfo).value == |
|
112 ((Long)value).longValue(); |
108 } |
113 } |
109 }, |
114 }, |
110 FLOAT(1.0f, "float", "F") { |
115 FLOAT(1.0f, "float", "F") { |
111 @Override |
116 @Override |
112 boolean check(CPInfo cpInfo) throws Exception { |
117 boolean check(CPInfo cpInfo) throws Exception { |
113 return (cpInfo instanceof CONSTANT_Float_info) && |
118 return (cpInfo instanceof CONSTANT_Float_info) && |
114 ((CONSTANT_Float_info)cpInfo).value == ((Float)value).floatValue(); |
119 ((CONSTANT_Float_info)cpInfo).value == |
|
120 ((Float)value).floatValue(); |
115 } |
121 } |
116 }, |
122 }, |
117 DOUBLE(1.0, "double","D") { |
123 DOUBLE(1.0, "double","D") { |
118 @Override |
124 @Override |
119 boolean check(CPInfo cpInfo) throws Exception { |
125 boolean check(CPInfo cpInfo) throws Exception { |
120 return (cpInfo instanceof CONSTANT_Double_info) && |
126 return (cpInfo instanceof CONSTANT_Double_info) && |
121 ((CONSTANT_Double_info)cpInfo).value == ((Double)value).doubleValue(); |
127 ((CONSTANT_Double_info)cpInfo).value == |
|
128 ((Double)value).doubleValue(); |
122 } |
129 } |
123 }, |
130 }, |
124 METHOD_HANDLE(null, "MethodHandle", "Ljava/lang/invoke/MethodHandle;") { |
131 METHOD_HANDLE(null, "MethodHandle", "Ljava/lang/invoke/MethodHandle;") { |
125 @Override |
132 @Override |
126 boolean check(CPInfo cpInfo) throws Exception { |
133 boolean check(CPInfo cpInfo) throws Exception { |
127 if (!(cpInfo instanceof CONSTANT_MethodHandle_info)) return false; |
134 if (!(cpInfo instanceof CONSTANT_MethodHandle_info)) |
128 CONSTANT_MethodHandle_info handleInfo = (CONSTANT_MethodHandle_info)cpInfo; |
135 return false; |
|
136 CONSTANT_MethodHandle_info handleInfo = |
|
137 (CONSTANT_MethodHandle_info)cpInfo; |
129 return handleInfo.getCPRefInfo().getClassName().equals("Array") && |
138 return handleInfo.getCPRefInfo().getClassName().equals("Array") && |
130 handleInfo.reference_kind == RefKind.REF_invokeVirtual && |
139 handleInfo.reference_kind == RefKind.REF_invokeVirtual && |
131 handleInfo.getCPRefInfo().getNameAndTypeInfo().getName().equals("clone") && |
140 handleInfo.getCPRefInfo() |
132 handleInfo.getCPRefInfo().getNameAndTypeInfo().getType().equals("()Ljava/lang/Object;"); |
141 .getNameAndTypeInfo().getName().equals("clone") && |
|
142 handleInfo.getCPRefInfo() |
|
143 .getNameAndTypeInfo().getType().equals("()Ljava/lang/Object;"); |
133 } |
144 } |
134 }, |
145 }, |
135 METHOD_TYPE(null, "MethodType", "Ljava/lang/invoke/MethodType;") { |
146 METHOD_TYPE(null, "MethodType", "Ljava/lang/invoke/MethodType;") { |
136 @Override |
147 @Override |
137 boolean check(CPInfo cpInfo) throws Exception { |
148 boolean check(CPInfo cpInfo) throws Exception { |
138 return (cpInfo instanceof CONSTANT_MethodType_info) && |
149 return (cpInfo instanceof CONSTANT_MethodType_info) && |
139 ((CONSTANT_MethodType_info)cpInfo).getType().equals("()Ljava/lang/Object;"); |
150 ((CONSTANT_MethodType_info)cpInfo).getType() |
|
151 .equals("()Ljava/lang/Object;"); |
140 } |
152 } |
141 }; |
153 }; |
142 |
154 |
143 Object value; |
155 Object value; |
144 String sourceTypeStr; |
156 String sourceTypeStr; |
145 String bytecodeTypeStr; |
157 String bytecodeTypeStr; |
146 |
158 |
147 StaticArgumentKind(Object value, String sourceTypeStr, String bytecodeTypeStr) { |
159 StaticArgumentKind(Object value, String sourceTypeStr, |
|
160 String bytecodeTypeStr) { |
148 this.value = value; |
161 this.value = value; |
149 this.sourceTypeStr = sourceTypeStr; |
162 this.sourceTypeStr = sourceTypeStr; |
150 this.bytecodeTypeStr = bytecodeTypeStr; |
163 this.bytecodeTypeStr = bytecodeTypeStr; |
151 } |
164 } |
152 |
165 |
184 this.arity = arity; |
198 this.arity = arity; |
185 } |
199 } |
186 } |
200 } |
187 |
201 |
188 public static void main(String... args) throws Exception { |
202 public static void main(String... args) throws Exception { |
189 // Create a single file manager and compiler and reuse it for each compile to save time. |
|
190 StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
|
191 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
|
192 for (StaticArgumentsArity arity : StaticArgumentsArity.values()) { |
203 for (StaticArgumentsArity arity : StaticArgumentsArity.values()) { |
193 if (arity.arity == 0) { |
204 if (arity.arity == 0) { |
194 new TestInvokeDynamic(arity).compileAndCheck(fm, tool); |
205 pool.execute(new TestInvokeDynamic(arity)); |
195 } else { |
206 } else { |
196 for (StaticArgumentKind sak1 : StaticArgumentKind.values()) { |
207 for (StaticArgumentKind sak1 : StaticArgumentKind.values()) { |
197 if (arity.arity == 1) { |
208 if (arity.arity == 1) { |
198 new TestInvokeDynamic(arity, sak1).compileAndCheck(fm, tool); |
209 pool.execute(new TestInvokeDynamic(arity, sak1)); |
199 } else { |
210 } else { |
200 for (StaticArgumentKind sak2 : StaticArgumentKind.values()) { |
211 for (StaticArgumentKind sak2 : StaticArgumentKind.values()) { |
201 if (arity.arity == 2) { |
212 if (arity.arity == 2) { |
202 new TestInvokeDynamic(arity, sak1, sak2).compileAndCheck(fm, tool); |
213 pool.execute(new TestInvokeDynamic(arity, sak1, sak2)); |
203 } else { |
214 } else { |
204 for (StaticArgumentKind sak3 : StaticArgumentKind.values()) { |
215 for (StaticArgumentKind sak3 : StaticArgumentKind.values()) { |
205 new TestInvokeDynamic(arity, sak1, sak2, sak3).compileAndCheck(fm, tool); |
216 pool.execute( |
|
217 new TestInvokeDynamic(arity, sak1, sak2, sak3)); |
206 } |
218 } |
207 } |
219 } |
208 } |
220 } |
209 } |
221 } |
210 } |
222 } |
211 } |
223 } |
212 } |
224 } |
213 |
225 |
214 System.out.println("Total checks made: " + checkCount); |
226 checkAfterExec(); |
215 } |
227 } |
216 |
228 |
217 StaticArgumentsArity arity; |
229 StaticArgumentsArity arity; |
218 StaticArgumentKind[] saks; |
230 StaticArgumentKind[] saks; |
219 JavaSource source; |
|
220 DiagChecker dc; |
231 DiagChecker dc; |
221 |
232 |
222 TestInvokeDynamic(StaticArgumentsArity arity, StaticArgumentKind... saks) { |
233 TestInvokeDynamic(StaticArgumentsArity arity, StaticArgumentKind... saks) { |
223 this.arity = arity; |
234 this.arity = arity; |
224 this.saks = saks; |
235 this.saks = saks; |
225 source = new JavaSource(); |
|
226 dc = new DiagChecker(); |
236 dc = new DiagChecker(); |
227 } |
237 } |
228 |
238 |
229 void compileAndCheck(JavaFileManager fm, JavaCompiler tool) throws Exception { |
239 public void run() { |
230 JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, fm, dc, |
240 int id = checkCount.incrementAndGet(); |
|
241 JavaSource source = new JavaSource(id); |
|
242 JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, |
231 null, null, Arrays.asList(source)); |
243 null, null, Arrays.asList(source)); |
232 Context context = ct.getContext(); |
244 Context context = ct.getContext(); |
233 Symtab syms = Symtab.instance(context); |
245 Symtab syms = Symtab.instance(context); |
234 Names names = Names.instance(context); |
246 Names names = Names.instance(context); |
235 Types types = Types.instance(context); |
247 Types types = Types.instance(context); |
236 ct.addTaskListener(new Indifier(syms, names, types)); |
248 ct.addTaskListener(new Indifier(syms, names, types)); |
237 try { |
249 try { |
238 ct.generate(); |
250 ct.generate(); |
239 } catch (Throwable t) { |
251 } catch (Throwable t) { |
240 t.printStackTrace(); |
252 t.printStackTrace(); |
241 throw new AssertionError(String.format("Error thrown when compiling following code\n%s", source.source)); |
253 throw new AssertionError( |
|
254 String.format("Error thrown when compiling following code\n%s", |
|
255 source.source)); |
242 } |
256 } |
243 if (dc.diagFound) { |
257 if (dc.diagFound) { |
244 throw new AssertionError(String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); |
258 throw new AssertionError( |
245 } |
259 String.format("Diags found when compiling following code\n%s\n\n%s", |
246 verifyBytecode(); |
260 source.source, dc.printDiags())); |
247 } |
261 } |
248 |
262 verifyBytecode(id); |
249 void verifyBytecode() { |
263 } |
250 File compiledTest = new File("Test.class"); |
264 |
|
265 void verifyBytecode(int id) { |
|
266 File compiledTest = new File(String.format("Test%d.class", id)); |
251 try { |
267 try { |
252 ClassFile cf = ClassFile.read(compiledTest); |
268 ClassFile cf = ClassFile.read(compiledTest); |
253 Method testMethod = null; |
269 Method testMethod = null; |
254 for (Method m : cf.methods) { |
270 for (Method m : cf.methods) { |
255 if (m.getName(cf.constant_pool).equals("test")) { |
271 if (m.getName(cf.constant_pool).equals("test")) { |
258 } |
274 } |
259 } |
275 } |
260 if (testMethod == null) { |
276 if (testMethod == null) { |
261 throw new Error("Test method not found"); |
277 throw new Error("Test method not found"); |
262 } |
278 } |
263 Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); |
279 Code_attribute ea = |
|
280 (Code_attribute)testMethod.attributes.get(Attribute.Code); |
264 if (testMethod == null) { |
281 if (testMethod == null) { |
265 throw new Error("Code attribute for test() method not found"); |
282 throw new Error("Code attribute for test() method not found"); |
266 } |
283 } |
267 |
284 |
268 int bsmIdx = -1; |
285 int bsmIdx = -1; |
269 |
286 |
270 for (Instruction i : ea.getInstructions()) { |
287 for (Instruction i : ea.getInstructions()) { |
271 if (i.getMnemonic().equals("invokedynamic")) { |
288 if (i.getMnemonic().equals("invokedynamic")) { |
272 CONSTANT_InvokeDynamic_info indyInfo = |
289 CONSTANT_InvokeDynamic_info indyInfo = |
273 (CONSTANT_InvokeDynamic_info)cf.constant_pool.get(i.getShort(1)); |
290 (CONSTANT_InvokeDynamic_info)cf |
|
291 .constant_pool.get(i.getShort(1)); |
274 bsmIdx = indyInfo.bootstrap_method_attr_index; |
292 bsmIdx = indyInfo.bootstrap_method_attr_index; |
275 if (!indyInfo.getNameAndTypeInfo().getType().equals("()V")) { |
293 if (!indyInfo.getNameAndTypeInfo().getType().equals("()V")) { |
276 throw new AssertionError("type mismatch for CONSTANT_InvokeDynamic_info"); |
294 throw new |
|
295 AssertionError("type mismatch for CONSTANT_InvokeDynamic_info"); |
277 } |
296 } |
278 } |
297 } |
279 } |
298 } |
280 if (bsmIdx == -1) { |
299 if (bsmIdx == -1) { |
281 throw new Error("Missing invokedynamic in generated code"); |
300 throw new Error("Missing invokedynamic in generated code"); |
282 } |
301 } |
283 |
302 |
284 BootstrapMethods_attribute bsm_attr = (BootstrapMethods_attribute)cf.getAttribute(Attribute.BootstrapMethods); |
303 BootstrapMethods_attribute bsm_attr = |
|
304 (BootstrapMethods_attribute)cf |
|
305 .getAttribute(Attribute.BootstrapMethods); |
285 if (bsm_attr.bootstrap_method_specifiers.length != 1) { |
306 if (bsm_attr.bootstrap_method_specifiers.length != 1) { |
286 throw new Error("Bad number of method specifiers in BootstrapMethods attribute"); |
307 throw new Error("Bad number of method specifiers " + |
|
308 "in BootstrapMethods attribute"); |
287 } |
309 } |
288 BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec = |
310 BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec = |
289 bsm_attr.bootstrap_method_specifiers[0]; |
311 bsm_attr.bootstrap_method_specifiers[0]; |
290 |
312 |
291 if (bsm_spec.bootstrap_arguments.length != arity.arity) { |
313 if (bsm_spec.bootstrap_arguments.length != arity.arity) { |
292 throw new Error("Bad number of static invokedynamic args in BootstrapMethod attribute"); |
314 throw new Error("Bad number of static invokedynamic args " + |
|
315 "in BootstrapMethod attribute"); |
293 } |
316 } |
294 |
317 |
295 int count = 0; |
318 int count = 0; |
296 for (StaticArgumentKind sak : saks) { |
319 for (StaticArgumentKind sak : saks) { |
297 if (!sak.check(cf.constant_pool.get(bsm_spec.bootstrap_arguments[count]))) { |
320 if (!sak.check(cf.constant_pool |
|
321 .get(bsm_spec.bootstrap_arguments[count]))) { |
298 throw new Error("Bad static argument value " + sak); |
322 throw new Error("Bad static argument value " + sak); |
299 } |
323 } |
300 count++; |
324 count++; |
301 } |
325 } |
302 |
326 |
303 CONSTANT_MethodHandle_info bsm_handle = |
327 CONSTANT_MethodHandle_info bsm_handle = |
304 (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_method_ref); |
328 (CONSTANT_MethodHandle_info)cf.constant_pool |
|
329 .get(bsm_spec.bootstrap_method_ref); |
305 |
330 |
306 if (bsm_handle.reference_kind != RefKind.REF_invokeStatic) { |
331 if (bsm_handle.reference_kind != RefKind.REF_invokeStatic) { |
307 throw new Error("Bad kind on boostrap method handle"); |
332 throw new Error("Bad kind on boostrap method handle"); |
308 } |
333 } |
309 |
334 |
310 CONSTANT_Methodref_info bsm_ref = |
335 CONSTANT_Methodref_info bsm_ref = |
311 (CONSTANT_Methodref_info)cf.constant_pool.get(bsm_handle.reference_index); |
336 (CONSTANT_Methodref_info)cf.constant_pool |
|
337 .get(bsm_handle.reference_index); |
312 |
338 |
313 if (!bsm_ref.getClassInfo().getName().equals("Bootstrap")) { |
339 if (!bsm_ref.getClassInfo().getName().equals("Bootstrap")) { |
314 throw new Error("Bad owner of boostrap method"); |
340 throw new Error("Bad owner of boostrap method"); |
315 } |
341 } |
316 |
342 |
317 if (!bsm_ref.getNameAndTypeInfo().getName().equals("bsm")) { |
343 if (!bsm_ref.getNameAndTypeInfo().getName().equals("bsm")) { |
318 throw new Error("Bad boostrap method name"); |
344 throw new Error("Bad boostrap method name"); |
319 } |
345 } |
320 |
346 |
321 if (!bsm_ref.getNameAndTypeInfo().getType().equals(asBSMSignatureString())) { |
347 if (!bsm_ref.getNameAndTypeInfo() |
322 throw new Error("Bad boostrap method type" + bsm_ref.getNameAndTypeInfo().getType() + " " + asBSMSignatureString()); |
348 .getType().equals(asBSMSignatureString())) { |
|
349 throw new Error("Bad boostrap method type" + |
|
350 bsm_ref.getNameAndTypeInfo().getType() + " " + |
|
351 asBSMSignatureString()); |
323 } |
352 } |
324 } catch (Exception e) { |
353 } catch (Exception e) { |
325 e.printStackTrace(); |
354 e.printStackTrace(); |
326 throw new Error("error reading " + compiledTest +": " + e); |
355 throw new Error("error reading " + compiledTest +": " + e); |
327 } |
356 } |