1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/TestInvokeDynamic.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,494 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7194586 8003280 8006694 8010404 1.30 + * @summary Add lambda tests 1.31 + * Add back-end support for invokedynamic 1.32 + * temporarily workaround combo tests are causing time out in several platforms 1.33 + * @library ../lib 1.34 + * @build JavacTestingAbstractThreadedTest 1.35 + * @run main/othervm TestInvokeDynamic 1.36 + */ 1.37 + 1.38 +// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) 1.39 +// see JDK-8006746 1.40 + 1.41 +import java.io.File; 1.42 +import java.net.URI; 1.43 +import java.util.ArrayList; 1.44 +import java.util.Arrays; 1.45 +import java.util.Locale; 1.46 + 1.47 +import javax.tools.Diagnostic; 1.48 +import javax.tools.JavaFileObject; 1.49 +import javax.tools.SimpleJavaFileObject; 1.50 + 1.51 +import com.sun.source.tree.MethodInvocationTree; 1.52 +import com.sun.source.tree.MethodTree; 1.53 +import com.sun.source.util.TaskEvent; 1.54 +import com.sun.source.util.TaskListener; 1.55 +import com.sun.source.util.TreeScanner; 1.56 + 1.57 +import com.sun.tools.classfile.Attribute; 1.58 +import com.sun.tools.classfile.BootstrapMethods_attribute; 1.59 +import com.sun.tools.classfile.ClassFile; 1.60 +import com.sun.tools.classfile.Code_attribute; 1.61 +import com.sun.tools.classfile.ConstantPool.*; 1.62 +import com.sun.tools.classfile.Instruction; 1.63 +import com.sun.tools.classfile.LineNumberTable_attribute; 1.64 +import com.sun.tools.classfile.Method; 1.65 + 1.66 +import com.sun.tools.javac.api.JavacTaskImpl; 1.67 +import com.sun.tools.javac.code.Symbol; 1.68 +import com.sun.tools.javac.code.Symbol.MethodSymbol; 1.69 +import com.sun.tools.javac.code.Symtab; 1.70 +import com.sun.tools.javac.code.Types; 1.71 +import com.sun.tools.javac.jvm.Pool; 1.72 +import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; 1.73 +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; 1.74 +import com.sun.tools.javac.tree.JCTree.JCIdent; 1.75 +import com.sun.tools.javac.util.Context; 1.76 +import com.sun.tools.javac.util.Names; 1.77 + 1.78 +import static com.sun.tools.javac.jvm.ClassFile.*; 1.79 + 1.80 +public class TestInvokeDynamic 1.81 + extends JavacTestingAbstractThreadedTest 1.82 + implements Runnable { 1.83 + 1.84 + enum StaticArgumentKind { 1.85 + STRING("Hello!", "String", "Ljava/lang/String;") { 1.86 + @Override 1.87 + boolean check(CPInfo cpInfo) throws Exception { 1.88 + return (cpInfo instanceof CONSTANT_String_info) && 1.89 + ((CONSTANT_String_info)cpInfo).getString() 1.90 + .equals(value); 1.91 + } 1.92 + }, 1.93 + CLASS(null, "Class<?>", "Ljava/lang/Class;") { 1.94 + @Override 1.95 + boolean check(CPInfo cpInfo) throws Exception { 1.96 + return (cpInfo instanceof CONSTANT_Class_info) && 1.97 + ((CONSTANT_Class_info)cpInfo).getName() 1.98 + .equals("java/lang/String"); 1.99 + } 1.100 + }, 1.101 + INTEGER(1, "int", "I") { 1.102 + @Override 1.103 + boolean check(CPInfo cpInfo) throws Exception { 1.104 + return (cpInfo instanceof CONSTANT_Integer_info) && 1.105 + ((CONSTANT_Integer_info)cpInfo).value == 1.106 + ((Integer)value).intValue(); 1.107 + } 1.108 + }, 1.109 + LONG(1L, "long", "J") { 1.110 + @Override 1.111 + boolean check(CPInfo cpInfo) throws Exception { 1.112 + return (cpInfo instanceof CONSTANT_Long_info) && 1.113 + ((CONSTANT_Long_info)cpInfo).value == 1.114 + ((Long)value).longValue(); 1.115 + } 1.116 + }, 1.117 + FLOAT(1.0f, "float", "F") { 1.118 + @Override 1.119 + boolean check(CPInfo cpInfo) throws Exception { 1.120 + return (cpInfo instanceof CONSTANT_Float_info) && 1.121 + ((CONSTANT_Float_info)cpInfo).value == 1.122 + ((Float)value).floatValue(); 1.123 + } 1.124 + }, 1.125 + DOUBLE(1.0, "double","D") { 1.126 + @Override 1.127 + boolean check(CPInfo cpInfo) throws Exception { 1.128 + return (cpInfo instanceof CONSTANT_Double_info) && 1.129 + ((CONSTANT_Double_info)cpInfo).value == 1.130 + ((Double)value).doubleValue(); 1.131 + } 1.132 + }, 1.133 + METHOD_HANDLE(null, "MethodHandle", "Ljava/lang/invoke/MethodHandle;") { 1.134 + @Override 1.135 + boolean check(CPInfo cpInfo) throws Exception { 1.136 + if (!(cpInfo instanceof CONSTANT_MethodHandle_info)) 1.137 + return false; 1.138 + CONSTANT_MethodHandle_info handleInfo = 1.139 + (CONSTANT_MethodHandle_info)cpInfo; 1.140 + return handleInfo.getCPRefInfo().getClassName().equals("Array") && 1.141 + handleInfo.reference_kind == RefKind.REF_invokeVirtual && 1.142 + handleInfo.getCPRefInfo() 1.143 + .getNameAndTypeInfo().getName().equals("clone") && 1.144 + handleInfo.getCPRefInfo() 1.145 + .getNameAndTypeInfo().getType().equals("()Ljava/lang/Object;"); 1.146 + } 1.147 + }, 1.148 + METHOD_TYPE(null, "MethodType", "Ljava/lang/invoke/MethodType;") { 1.149 + @Override 1.150 + boolean check(CPInfo cpInfo) throws Exception { 1.151 + return (cpInfo instanceof CONSTANT_MethodType_info) && 1.152 + ((CONSTANT_MethodType_info)cpInfo).getType() 1.153 + .equals("()Ljava/lang/Object;"); 1.154 + } 1.155 + }; 1.156 + 1.157 + Object value; 1.158 + String sourceTypeStr; 1.159 + String bytecodeTypeStr; 1.160 + 1.161 + StaticArgumentKind(Object value, String sourceTypeStr, 1.162 + String bytecodeTypeStr) { 1.163 + this.value = value; 1.164 + this.sourceTypeStr = sourceTypeStr; 1.165 + this.bytecodeTypeStr = bytecodeTypeStr; 1.166 + } 1.167 + 1.168 + abstract boolean check(CPInfo cpInfo) throws Exception; 1.169 + 1.170 + Object getValue(Symtab syms, Names names, Types types) { 1.171 + switch (this) { 1.172 + case STRING: 1.173 + case INTEGER: 1.174 + case LONG: 1.175 + case FLOAT: 1.176 + case DOUBLE: 1.177 + return value; 1.178 + case CLASS: 1.179 + return syms.stringType.tsym; 1.180 + case METHOD_HANDLE: 1.181 + return new Pool.MethodHandle(REF_invokeVirtual, 1.182 + syms.arrayCloneMethod, types); 1.183 + case METHOD_TYPE: 1.184 + return syms.arrayCloneMethod.type; 1.185 + default: 1.186 + throw new AssertionError(); 1.187 + } 1.188 + } 1.189 + } 1.190 + 1.191 + enum StaticArgumentsArity { 1.192 + ZERO(0), 1.193 + ONE(1), 1.194 + TWO(2), 1.195 + THREE(3); 1.196 + 1.197 + int arity; 1.198 + 1.199 + StaticArgumentsArity(int arity) { 1.200 + this.arity = arity; 1.201 + } 1.202 + } 1.203 + 1.204 + public static void main(String... args) throws Exception { 1.205 + for (StaticArgumentsArity arity : StaticArgumentsArity.values()) { 1.206 + if (arity.arity == 0) { 1.207 + pool.execute(new TestInvokeDynamic(arity)); 1.208 + } else { 1.209 + for (StaticArgumentKind sak1 : StaticArgumentKind.values()) { 1.210 + if (arity.arity == 1) { 1.211 + pool.execute(new TestInvokeDynamic(arity, sak1)); 1.212 + } else { 1.213 + for (StaticArgumentKind sak2 : StaticArgumentKind.values()) { 1.214 + if (arity.arity == 2) { 1.215 + pool.execute(new TestInvokeDynamic(arity, sak1, sak2)); 1.216 + } else { 1.217 + for (StaticArgumentKind sak3 : StaticArgumentKind.values()) { 1.218 + pool.execute( 1.219 + new TestInvokeDynamic(arity, sak1, sak2, sak3)); 1.220 + } 1.221 + } 1.222 + } 1.223 + } 1.224 + } 1.225 + } 1.226 + } 1.227 + 1.228 + checkAfterExec(); 1.229 + } 1.230 + 1.231 + StaticArgumentsArity arity; 1.232 + StaticArgumentKind[] saks; 1.233 + DiagChecker dc; 1.234 + 1.235 + TestInvokeDynamic(StaticArgumentsArity arity, StaticArgumentKind... saks) { 1.236 + this.arity = arity; 1.237 + this.saks = saks; 1.238 + dc = new DiagChecker(); 1.239 + } 1.240 + 1.241 + public void run() { 1.242 + int id = checkCount.incrementAndGet(); 1.243 + JavaSource source = new JavaSource(id); 1.244 + JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, 1.245 + Arrays.asList("-g"), null, Arrays.asList(source)); 1.246 + Context context = ct.getContext(); 1.247 + Symtab syms = Symtab.instance(context); 1.248 + Names names = Names.instance(context); 1.249 + Types types = Types.instance(context); 1.250 + ct.addTaskListener(new Indifier(syms, names, types)); 1.251 + try { 1.252 + ct.generate(); 1.253 + } catch (Throwable t) { 1.254 + t.printStackTrace(); 1.255 + throw new AssertionError( 1.256 + String.format("Error thrown when compiling following code\n%s", 1.257 + source.source)); 1.258 + } 1.259 + if (dc.diagFound) { 1.260 + throw new AssertionError( 1.261 + String.format("Diags found when compiling following code\n%s\n\n%s", 1.262 + source.source, dc.printDiags())); 1.263 + } 1.264 + verifyBytecode(id); 1.265 + } 1.266 + 1.267 + void verifyBytecode(int id) { 1.268 + File compiledTest = new File(String.format("Test%d.class", id)); 1.269 + try { 1.270 + ClassFile cf = ClassFile.read(compiledTest); 1.271 + Method testMethod = null; 1.272 + for (Method m : cf.methods) { 1.273 + if (m.getName(cf.constant_pool).equals("test")) { 1.274 + testMethod = m; 1.275 + break; 1.276 + } 1.277 + } 1.278 + if (testMethod == null) { 1.279 + throw new Error("Test method not found"); 1.280 + } 1.281 + Code_attribute ea = 1.282 + (Code_attribute)testMethod.attributes.get(Attribute.Code); 1.283 + if (testMethod == null) { 1.284 + throw new Error("Code attribute for test() method not found"); 1.285 + } 1.286 + 1.287 + int bsmIdx = -1; 1.288 + 1.289 + for (Instruction i : ea.getInstructions()) { 1.290 + if (i.getMnemonic().equals("invokedynamic")) { 1.291 + CONSTANT_InvokeDynamic_info indyInfo = 1.292 + (CONSTANT_InvokeDynamic_info)cf 1.293 + .constant_pool.get(i.getShort(1)); 1.294 + bsmIdx = indyInfo.bootstrap_method_attr_index; 1.295 + if (!indyInfo.getNameAndTypeInfo().getType().equals("()V")) { 1.296 + throw new 1.297 + AssertionError("type mismatch for CONSTANT_InvokeDynamic_info"); 1.298 + } 1.299 + } 1.300 + } 1.301 + if (bsmIdx == -1) { 1.302 + throw new Error("Missing invokedynamic in generated code"); 1.303 + } 1.304 + 1.305 + BootstrapMethods_attribute bsm_attr = 1.306 + (BootstrapMethods_attribute)cf 1.307 + .getAttribute(Attribute.BootstrapMethods); 1.308 + if (bsm_attr.bootstrap_method_specifiers.length != 1) { 1.309 + throw new Error("Bad number of method specifiers " + 1.310 + "in BootstrapMethods attribute"); 1.311 + } 1.312 + BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec = 1.313 + bsm_attr.bootstrap_method_specifiers[0]; 1.314 + 1.315 + if (bsm_spec.bootstrap_arguments.length != arity.arity) { 1.316 + throw new Error("Bad number of static invokedynamic args " + 1.317 + "in BootstrapMethod attribute"); 1.318 + } 1.319 + 1.320 + int count = 0; 1.321 + for (StaticArgumentKind sak : saks) { 1.322 + if (!sak.check(cf.constant_pool 1.323 + .get(bsm_spec.bootstrap_arguments[count]))) { 1.324 + throw new Error("Bad static argument value " + sak); 1.325 + } 1.326 + count++; 1.327 + } 1.328 + 1.329 + CONSTANT_MethodHandle_info bsm_handle = 1.330 + (CONSTANT_MethodHandle_info)cf.constant_pool 1.331 + .get(bsm_spec.bootstrap_method_ref); 1.332 + 1.333 + if (bsm_handle.reference_kind != RefKind.REF_invokeStatic) { 1.334 + throw new Error("Bad kind on boostrap method handle"); 1.335 + } 1.336 + 1.337 + CONSTANT_Methodref_info bsm_ref = 1.338 + (CONSTANT_Methodref_info)cf.constant_pool 1.339 + .get(bsm_handle.reference_index); 1.340 + 1.341 + if (!bsm_ref.getClassInfo().getName().equals("Bootstrap")) { 1.342 + throw new Error("Bad owner of boostrap method"); 1.343 + } 1.344 + 1.345 + if (!bsm_ref.getNameAndTypeInfo().getName().equals("bsm")) { 1.346 + throw new Error("Bad boostrap method name"); 1.347 + } 1.348 + 1.349 + if (!bsm_ref.getNameAndTypeInfo() 1.350 + .getType().equals(asBSMSignatureString())) { 1.351 + throw new Error("Bad boostrap method type" + 1.352 + bsm_ref.getNameAndTypeInfo().getType() + " " + 1.353 + asBSMSignatureString()); 1.354 + } 1.355 + 1.356 + LineNumberTable_attribute lnt = 1.357 + (LineNumberTable_attribute)ea.attributes.get(Attribute.LineNumberTable); 1.358 + 1.359 + if (lnt == null) { 1.360 + throw new Error("No LineNumberTable attribute"); 1.361 + } 1.362 + if (lnt.line_number_table_length != 3) { 1.363 + throw new Error("Wrong number of entries in LineNumberTable"); 1.364 + } 1.365 + } catch (Exception e) { 1.366 + e.printStackTrace(); 1.367 + throw new Error("error reading " + compiledTest +": " + e); 1.368 + } 1.369 + } 1.370 + 1.371 + String asBSMSignatureString() { 1.372 + StringBuilder buf = new StringBuilder(); 1.373 + buf.append("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;"); 1.374 + for (StaticArgumentKind sak : saks) { 1.375 + buf.append(sak.bytecodeTypeStr); 1.376 + } 1.377 + buf.append(")Ljava/lang/invoke/CallSite;"); 1.378 + return buf.toString(); 1.379 + } 1.380 + 1.381 + class JavaSource extends SimpleJavaFileObject { 1.382 + 1.383 + static final String source_template = "import java.lang.invoke.*;\n" + 1.384 + "class Bootstrap {\n" + 1.385 + " public static CallSite bsm(MethodHandles.Lookup lookup, " + 1.386 + "String name, MethodType methodType #SARGS) {\n" + 1.387 + " return null;\n" + 1.388 + " }\n" + 1.389 + "}\n" + 1.390 + "class Test#ID {\n" + 1.391 + " void m() { }\n" + 1.392 + " void test() {\n" + 1.393 + " Object o = this; // marker statement \n" + 1.394 + " m();\n" + 1.395 + " }\n" + 1.396 + "}"; 1.397 + 1.398 + String source; 1.399 + 1.400 + JavaSource(int id) { 1.401 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.402 + source = source_template.replace("#SARGS", asSignatureString()) 1.403 + .replace("#ID", String.valueOf(id)); 1.404 + } 1.405 + 1.406 + @Override 1.407 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.408 + return source; 1.409 + } 1.410 + 1.411 + String asSignatureString() { 1.412 + int count = 0; 1.413 + StringBuilder buf = new StringBuilder(); 1.414 + for (StaticArgumentKind sak : saks) { 1.415 + buf.append(","); 1.416 + buf.append(sak.sourceTypeStr); 1.417 + buf.append(' '); 1.418 + buf.append(String.format("x%d", count++)); 1.419 + } 1.420 + return buf.toString(); 1.421 + } 1.422 + } 1.423 + 1.424 + class Indifier extends TreeScanner<Void, Void> implements TaskListener { 1.425 + 1.426 + MethodSymbol bsm; 1.427 + Symtab syms; 1.428 + Names names; 1.429 + Types types; 1.430 + 1.431 + Indifier(Symtab syms, Names names, Types types) { 1.432 + this.syms = syms; 1.433 + this.names = names; 1.434 + this.types = types; 1.435 + } 1.436 + 1.437 + @Override 1.438 + public void started(TaskEvent e) { 1.439 + //do nothing 1.440 + } 1.441 + 1.442 + @Override 1.443 + public void finished(TaskEvent e) { 1.444 + if (e.getKind() == TaskEvent.Kind.ANALYZE) { 1.445 + scan(e.getCompilationUnit(), null); 1.446 + } 1.447 + } 1.448 + 1.449 + @Override 1.450 + public Void visitMethodInvocation(MethodInvocationTree node, Void p) { 1.451 + super.visitMethodInvocation(node, p); 1.452 + JCMethodInvocation apply = (JCMethodInvocation)node; 1.453 + JCIdent ident = (JCIdent)apply.meth; 1.454 + Symbol oldSym = ident.sym; 1.455 + if (!oldSym.isConstructor()) { 1.456 + Object[] staticArgs = new Object[arity.arity]; 1.457 + for (int i = 0; i < arity.arity ; i++) { 1.458 + staticArgs[i] = saks[i].getValue(syms, names, types); 1.459 + } 1.460 + ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name, 1.461 + oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs); 1.462 + } 1.463 + return null; 1.464 + } 1.465 + 1.466 + @Override 1.467 + public Void visitMethod(MethodTree node, Void p) { 1.468 + super.visitMethod(node, p); 1.469 + if (node.getName().toString().equals("bsm")) { 1.470 + bsm = ((JCMethodDecl)node).sym; 1.471 + } 1.472 + return null; 1.473 + } 1.474 + } 1.475 + 1.476 + static class DiagChecker 1.477 + implements javax.tools.DiagnosticListener<JavaFileObject> { 1.478 + 1.479 + boolean diagFound; 1.480 + ArrayList<String> diags = new ArrayList<>(); 1.481 + 1.482 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.483 + diags.add(diagnostic.getMessage(Locale.getDefault())); 1.484 + diagFound = true; 1.485 + } 1.486 + 1.487 + String printDiags() { 1.488 + StringBuilder buf = new StringBuilder(); 1.489 + for (String s : diags) { 1.490 + buf.append(s); 1.491 + buf.append("\n"); 1.492 + } 1.493 + return buf.toString(); 1.494 + } 1.495 + } 1.496 + 1.497 +}