|
1 /* |
|
2 * Copyright (c) 2012, 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 7194586 |
|
27 * |
|
28 * @summary Add back-end support for invokedynamic |
|
29 * |
|
30 */ |
|
31 |
|
32 import com.sun.source.tree.MethodInvocationTree; |
|
33 import com.sun.source.tree.MethodTree; |
|
34 import com.sun.source.util.TaskEvent; |
|
35 import com.sun.source.util.TaskListener; |
|
36 import com.sun.source.util.TreeScanner; |
|
37 |
|
38 import com.sun.tools.classfile.Attribute; |
|
39 import com.sun.tools.classfile.BootstrapMethods_attribute; |
|
40 import com.sun.tools.classfile.ClassFile; |
|
41 import com.sun.tools.classfile.Code_attribute; |
|
42 import com.sun.tools.classfile.ConstantPool.*; |
|
43 import com.sun.tools.classfile.Instruction; |
|
44 import com.sun.tools.classfile.Method; |
|
45 |
|
46 import com.sun.tools.javac.api.JavacTaskImpl; |
|
47 import com.sun.tools.javac.api.JavacTool; |
|
48 import com.sun.tools.javac.code.Symbol; |
|
49 import com.sun.tools.javac.code.Symbol.MethodSymbol; |
|
50 import com.sun.tools.javac.code.Symtab; |
|
51 import com.sun.tools.javac.jvm.Pool; |
|
52 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; |
|
53 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
|
54 import com.sun.tools.javac.tree.JCTree.JCIdent; |
|
55 import com.sun.tools.javac.util.Context; |
|
56 import com.sun.tools.javac.util.Names; |
|
57 |
|
58 import java.io.File; |
|
59 import java.net.URI; |
|
60 import java.util.ArrayList; |
|
61 import java.util.Arrays; |
|
62 import java.util.Locale; |
|
63 |
|
64 import javax.tools.Diagnostic; |
|
65 import javax.tools.JavaCompiler; |
|
66 import javax.tools.JavaFileManager; |
|
67 import javax.tools.JavaFileObject; |
|
68 import javax.tools.SimpleJavaFileObject; |
|
69 import javax.tools.StandardJavaFileManager; |
|
70 import javax.tools.ToolProvider; |
|
71 |
|
72 import static com.sun.tools.javac.jvm.ClassFile.*; |
|
73 |
|
74 public class TestInvokeDynamic { |
|
75 |
|
76 static int checkCount = 0; |
|
77 |
|
78 enum StaticArgumentKind { |
|
79 STRING("Hello!", "String", "Ljava/lang/String;") { |
|
80 @Override |
|
81 boolean check(CPInfo cpInfo) throws Exception { |
|
82 return (cpInfo instanceof CONSTANT_String_info) && |
|
83 ((CONSTANT_String_info)cpInfo).getString().equals(value); |
|
84 } |
|
85 }, |
|
86 CLASS(null, "Class<?>", "Ljava/lang/Class;") { |
|
87 @Override |
|
88 boolean check(CPInfo cpInfo) throws Exception { |
|
89 return (cpInfo instanceof CONSTANT_Class_info) && |
|
90 ((CONSTANT_Class_info)cpInfo).getName().equals("java/lang/String"); |
|
91 } |
|
92 }, |
|
93 INTEGER(1, "int", "I") { |
|
94 @Override |
|
95 boolean check(CPInfo cpInfo) throws Exception { |
|
96 return (cpInfo instanceof CONSTANT_Integer_info) && |
|
97 ((CONSTANT_Integer_info)cpInfo).value == ((Integer)value).intValue(); |
|
98 } |
|
99 }, |
|
100 LONG(1L, "long", "J") { |
|
101 @Override |
|
102 boolean check(CPInfo cpInfo) throws Exception { |
|
103 return (cpInfo instanceof CONSTANT_Long_info) && |
|
104 ((CONSTANT_Long_info)cpInfo).value == ((Long)value).longValue(); |
|
105 } |
|
106 }, |
|
107 FLOAT(1.0f, "float", "F") { |
|
108 @Override |
|
109 boolean check(CPInfo cpInfo) throws Exception { |
|
110 return (cpInfo instanceof CONSTANT_Float_info) && |
|
111 ((CONSTANT_Float_info)cpInfo).value == ((Float)value).floatValue(); |
|
112 } |
|
113 }, |
|
114 DOUBLE(1.0, "double","D") { |
|
115 @Override |
|
116 boolean check(CPInfo cpInfo) throws Exception { |
|
117 return (cpInfo instanceof CONSTANT_Double_info) && |
|
118 ((CONSTANT_Double_info)cpInfo).value == ((Double)value).doubleValue(); |
|
119 } |
|
120 }, |
|
121 METHOD_HANDLE(null, "MethodHandle", "Ljava/lang/invoke/MethodHandle;") { |
|
122 @Override |
|
123 boolean check(CPInfo cpInfo) throws Exception { |
|
124 if (!(cpInfo instanceof CONSTANT_MethodHandle_info)) return false; |
|
125 CONSTANT_MethodHandle_info handleInfo = (CONSTANT_MethodHandle_info)cpInfo; |
|
126 return handleInfo.getCPRefInfo().getClassName().equals("Array") && |
|
127 handleInfo.reference_kind == RefKind.REF_invokeVirtual && |
|
128 handleInfo.getCPRefInfo().getNameAndTypeInfo().getName().equals("clone") && |
|
129 handleInfo.getCPRefInfo().getNameAndTypeInfo().getType().equals("()Ljava/lang/Object;"); |
|
130 } |
|
131 }, |
|
132 METHOD_TYPE(null, "MethodType", "Ljava/lang/invoke/MethodType;") { |
|
133 @Override |
|
134 boolean check(CPInfo cpInfo) throws Exception { |
|
135 return (cpInfo instanceof CONSTANT_MethodType_info) && |
|
136 ((CONSTANT_MethodType_info)cpInfo).getType().equals("()Ljava/lang/Object;"); |
|
137 } |
|
138 }; |
|
139 |
|
140 Object value; |
|
141 String sourceTypeStr; |
|
142 String bytecodeTypeStr; |
|
143 |
|
144 StaticArgumentKind(Object value, String sourceTypeStr, String bytecodeTypeStr) { |
|
145 this.value = value; |
|
146 this.sourceTypeStr = sourceTypeStr; |
|
147 this.bytecodeTypeStr = bytecodeTypeStr; |
|
148 } |
|
149 |
|
150 abstract boolean check(CPInfo cpInfo) throws Exception; |
|
151 |
|
152 Object getValue(Symtab syms, Names names) { |
|
153 switch (this) { |
|
154 case STRING: |
|
155 case INTEGER: |
|
156 case LONG: |
|
157 case FLOAT: |
|
158 case DOUBLE: |
|
159 return value; |
|
160 case CLASS: |
|
161 return syms.stringType.tsym; |
|
162 case METHOD_HANDLE: |
|
163 return new Pool.MethodHandle(REF_invokeVirtual, syms.arrayCloneMethod, names); |
|
164 case METHOD_TYPE: |
|
165 return syms.arrayCloneMethod.type; |
|
166 default: |
|
167 throw new AssertionError(); |
|
168 } |
|
169 } |
|
170 } |
|
171 |
|
172 enum StaticArgumentsArity { |
|
173 ZERO(0), |
|
174 ONE(1), |
|
175 TWO(2), |
|
176 THREE(3); |
|
177 |
|
178 int arity; |
|
179 |
|
180 StaticArgumentsArity(int arity) { |
|
181 this.arity = arity; |
|
182 } |
|
183 } |
|
184 |
|
185 public static void main(String... args) throws Exception { |
|
186 // Create a single file manager and compiler and reuse it for each compile to save time. |
|
187 StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
|
188 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
|
189 for (StaticArgumentsArity arity : StaticArgumentsArity.values()) { |
|
190 if (arity.arity == 0) { |
|
191 new TestInvokeDynamic(arity).compileAndCheck(fm, tool); |
|
192 } else { |
|
193 for (StaticArgumentKind sak1 : StaticArgumentKind.values()) { |
|
194 if (arity.arity == 1) { |
|
195 new TestInvokeDynamic(arity, sak1).compileAndCheck(fm, tool); |
|
196 } else { |
|
197 for (StaticArgumentKind sak2 : StaticArgumentKind.values()) { |
|
198 if (arity.arity == 2) { |
|
199 new TestInvokeDynamic(arity, sak1, sak2).compileAndCheck(fm, tool); |
|
200 } else { |
|
201 for (StaticArgumentKind sak3 : StaticArgumentKind.values()) { |
|
202 new TestInvokeDynamic(arity, sak1, sak2, sak3).compileAndCheck(fm, tool); |
|
203 } |
|
204 } |
|
205 } |
|
206 } |
|
207 } |
|
208 } |
|
209 } |
|
210 |
|
211 System.out.println("Total checks made: " + checkCount); |
|
212 } |
|
213 |
|
214 StaticArgumentsArity arity; |
|
215 StaticArgumentKind[] saks; |
|
216 JavaSource source; |
|
217 DiagChecker dc; |
|
218 |
|
219 TestInvokeDynamic(StaticArgumentsArity arity, StaticArgumentKind... saks) { |
|
220 this.arity = arity; |
|
221 this.saks = saks; |
|
222 source = new JavaSource(); |
|
223 dc = new DiagChecker(); |
|
224 } |
|
225 |
|
226 void compileAndCheck(JavaFileManager fm, JavaCompiler tool) throws Exception { |
|
227 JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, fm, dc, |
|
228 null, null, Arrays.asList(source)); |
|
229 Context context = ct.getContext(); |
|
230 Symtab syms = Symtab.instance(context); |
|
231 Names names = Names.instance(context); |
|
232 ct.addTaskListener(new Indifier(syms, names)); |
|
233 try { |
|
234 ct.generate(); |
|
235 } catch (Throwable t) { |
|
236 t.printStackTrace(); |
|
237 throw new AssertionError(String.format("Error thrown when compiling following code\n%s", source.source)); |
|
238 } |
|
239 if (dc.diagFound) { |
|
240 throw new AssertionError(String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); |
|
241 } |
|
242 verifyBytecode(); |
|
243 } |
|
244 |
|
245 void verifyBytecode() { |
|
246 File compiledTest = new File("Test.class"); |
|
247 try { |
|
248 ClassFile cf = ClassFile.read(compiledTest); |
|
249 Method testMethod = null; |
|
250 for (Method m : cf.methods) { |
|
251 if (m.getName(cf.constant_pool).equals("test")) { |
|
252 testMethod = m; |
|
253 break; |
|
254 } |
|
255 } |
|
256 if (testMethod == null) { |
|
257 throw new Error("Test method not found"); |
|
258 } |
|
259 Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); |
|
260 if (testMethod == null) { |
|
261 throw new Error("Code attribute for test() method not found"); |
|
262 } |
|
263 |
|
264 int bsmIdx = -1; |
|
265 |
|
266 for (Instruction i : ea.getInstructions()) { |
|
267 if (i.getMnemonic().equals("invokedynamic")) { |
|
268 CONSTANT_InvokeDynamic_info indyInfo = |
|
269 (CONSTANT_InvokeDynamic_info)cf.constant_pool.get(i.getShort(1)); |
|
270 bsmIdx = indyInfo.bootstrap_method_attr_index; |
|
271 if (!indyInfo.getNameAndTypeInfo().getType().equals("()V")) { |
|
272 throw new AssertionError("type mismatch for CONSTANT_InvokeDynamic_info"); |
|
273 } |
|
274 } |
|
275 } |
|
276 if (bsmIdx == -1) { |
|
277 throw new Error("Missing invokedynamic in generated code"); |
|
278 } |
|
279 |
|
280 BootstrapMethods_attribute bsm_attr = (BootstrapMethods_attribute)cf.getAttribute(Attribute.BootstrapMethods); |
|
281 if (bsm_attr.bootstrap_method_specifiers.length != 1) { |
|
282 throw new Error("Bad number of method specifiers in BootstrapMethods attribute"); |
|
283 } |
|
284 BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec = |
|
285 bsm_attr.bootstrap_method_specifiers[0]; |
|
286 |
|
287 if (bsm_spec.bootstrap_arguments.length != arity.arity) { |
|
288 throw new Error("Bad number of static invokedynamic args in BootstrapMethod attribute"); |
|
289 } |
|
290 |
|
291 int count = 0; |
|
292 for (StaticArgumentKind sak : saks) { |
|
293 if (!sak.check(cf.constant_pool.get(bsm_spec.bootstrap_arguments[count]))) { |
|
294 throw new Error("Bad static argument value " + sak); |
|
295 } |
|
296 count++; |
|
297 } |
|
298 |
|
299 CONSTANT_MethodHandle_info bsm_handle = |
|
300 (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_method_ref); |
|
301 |
|
302 if (bsm_handle.reference_kind != RefKind.REF_invokeStatic) { |
|
303 throw new Error("Bad kind on boostrap method handle"); |
|
304 } |
|
305 |
|
306 CONSTANT_Methodref_info bsm_ref = |
|
307 (CONSTANT_Methodref_info)cf.constant_pool.get(bsm_handle.reference_index); |
|
308 |
|
309 if (!bsm_ref.getClassInfo().getName().equals("Bootstrap")) { |
|
310 throw new Error("Bad owner of boostrap method"); |
|
311 } |
|
312 |
|
313 if (!bsm_ref.getNameAndTypeInfo().getName().equals("bsm")) { |
|
314 throw new Error("Bad boostrap method name"); |
|
315 } |
|
316 |
|
317 if (!bsm_ref.getNameAndTypeInfo().getType().equals(asBSMSignatureString())) { |
|
318 throw new Error("Bad boostrap method type" + bsm_ref.getNameAndTypeInfo().getType() + " " + asBSMSignatureString()); |
|
319 } |
|
320 } catch (Exception e) { |
|
321 e.printStackTrace(); |
|
322 throw new Error("error reading " + compiledTest +": " + e); |
|
323 } |
|
324 } |
|
325 |
|
326 String asBSMSignatureString() { |
|
327 StringBuilder buf = new StringBuilder(); |
|
328 buf.append("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;"); |
|
329 for (StaticArgumentKind sak : saks) { |
|
330 buf.append(sak.bytecodeTypeStr); |
|
331 } |
|
332 buf.append(")Ljava/lang/invoke/CallSite;"); |
|
333 return buf.toString(); |
|
334 } |
|
335 |
|
336 class JavaSource extends SimpleJavaFileObject { |
|
337 |
|
338 static final String source_template = "import java.lang.invoke.*;\n" + |
|
339 "class Bootstrap {\n" + |
|
340 " public static CallSite bsm(MethodHandles.Lookup lookup, String name, MethodType methodType #SARGS) {\n" + |
|
341 " return null;\n" + |
|
342 " }\n" + |
|
343 "}\n" + |
|
344 "class Test {\n" + |
|
345 " void m() { }\n" + |
|
346 " void test() { m(); }\n" + |
|
347 "}"; |
|
348 |
|
349 String source; |
|
350 |
|
351 JavaSource() { |
|
352 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
353 source = source_template.replace("#SARGS", asSignatureString()); |
|
354 } |
|
355 |
|
356 @Override |
|
357 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
358 return source; |
|
359 } |
|
360 |
|
361 String asSignatureString() { |
|
362 int count = 0; |
|
363 StringBuilder buf = new StringBuilder(); |
|
364 for (StaticArgumentKind sak : saks) { |
|
365 buf.append(","); |
|
366 buf.append(sak.sourceTypeStr); |
|
367 buf.append(' '); |
|
368 buf.append(String.format("x%d", count++)); |
|
369 } |
|
370 return buf.toString(); |
|
371 } |
|
372 } |
|
373 |
|
374 class Indifier extends TreeScanner<Void, Void> implements TaskListener { |
|
375 |
|
376 MethodSymbol bsm; |
|
377 Symtab syms; |
|
378 Names names; |
|
379 |
|
380 Indifier(Symtab syms, Names names) { |
|
381 this.syms = syms; |
|
382 this.names = names; |
|
383 } |
|
384 |
|
385 @Override |
|
386 public void started(TaskEvent e) { |
|
387 //do nothing |
|
388 } |
|
389 |
|
390 @Override |
|
391 public void finished(TaskEvent e) { |
|
392 if (e.getKind() == TaskEvent.Kind.ANALYZE) { |
|
393 scan(e.getCompilationUnit(), null); |
|
394 } |
|
395 } |
|
396 |
|
397 @Override |
|
398 public Void visitMethodInvocation(MethodInvocationTree node, Void p) { |
|
399 super.visitMethodInvocation(node, p); |
|
400 JCMethodInvocation apply = (JCMethodInvocation)node; |
|
401 JCIdent ident = (JCIdent)apply.meth; |
|
402 Symbol oldSym = ident.sym; |
|
403 if (!oldSym.isConstructor()) { |
|
404 Object[] staticArgs = new Object[arity.arity]; |
|
405 for (int i = 0; i < arity.arity ; i++) { |
|
406 staticArgs[i] = saks[i].getValue(syms, names); |
|
407 } |
|
408 ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name, oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs); |
|
409 } |
|
410 return null; |
|
411 } |
|
412 |
|
413 @Override |
|
414 public Void visitMethod(MethodTree node, Void p) { |
|
415 super.visitMethod(node, p); |
|
416 if (node.getName().toString().equals("bsm")) { |
|
417 bsm = ((JCMethodDecl)node).sym; |
|
418 } |
|
419 return null; |
|
420 } |
|
421 } |
|
422 |
|
423 static class DiagChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
|
424 |
|
425 boolean diagFound; |
|
426 ArrayList<String> diags = new ArrayList<>(); |
|
427 |
|
428 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
429 diags.add(diagnostic.getMessage(Locale.getDefault())); |
|
430 diagFound = true; |
|
431 } |
|
432 |
|
433 String printDiags() { |
|
434 StringBuilder buf = new StringBuilder(); |
|
435 for (String s : diags) { |
|
436 buf.append(s); |
|
437 buf.append("\n"); |
|
438 } |
|
439 return buf.toString(); |
|
440 } |
|
441 } |
|
442 } |