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