1 /* 2 * Copyright (c) 2008, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.invoke; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedAction; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.Collections; 33 import java.util.List; 34 35 import sun.invoke.empty.Empty; 36 import sun.invoke.util.ValueConversions; 37 import sun.invoke.util.VerifyType; 38 import sun.invoke.util.Wrapper; 39 import sun.reflect.CallerSensitive; 40 import sun.reflect.Reflection; 41 import static java.lang.invoke.LambdaForm.*; 42 import static java.lang.invoke.MethodHandleStatics.*; 43 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; 44 45 /** 46 * Trusted implementation code for MethodHandle. 47 * @author jrose 48 */ 49 /*non-public*/ abstract class MethodHandleImpl { 50 // Do not adjust this except for special platforms: 51 private static final int MAX_ARITY; 52 static { 53 final Object[] values = { 255 }; 54 AccessController.doPrivileged(new PrivilegedAction<Void>() { 55 @Override 56 public Void run() { 57 values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255); 58 return null; 59 } 60 }); 61 MAX_ARITY = (Integer) values[0]; 62 } 63 64 /// Factory methods to create method handles: 65 66 static void initStatics() { 67 // Trigger selected static initializations. 68 MemberName.Factory.INSTANCE.getClass(); 69 } 70 71 static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) { 72 if (arrayClass == Object[].class) 73 return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER); 74 if (!arrayClass.isArray()) 75 throw newIllegalArgumentException("not an array: "+arrayClass); 76 MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass); 77 int cacheIndex = (isSetter ? ArrayAccessor.SETTER_INDEX : ArrayAccessor.GETTER_INDEX); 78 MethodHandle mh = cache[cacheIndex]; 79 if (mh != null) return mh; 80 mh = ArrayAccessor.getAccessor(arrayClass, isSetter); 81 MethodType correctType = ArrayAccessor.correctType(arrayClass, isSetter); 82 if (mh.type() != correctType) { 83 assert(mh.type().parameterType(0) == Object[].class); 84 assert((isSetter ? mh.type().parameterType(2) : mh.type().returnType()) == Object.class); 85 assert(isSetter || correctType.parameterType(0).getComponentType() == correctType.returnType()); 86 // safe to view non-strictly, because element type follows from array type 87 mh = mh.viewAsType(correctType); 88 } 89 // Atomically update accessor cache. 90 synchronized(cache) { 91 if (cache[cacheIndex] == null) { 92 cache[cacheIndex] = mh; 93 } else { 94 // Throw away newly constructed accessor and use cached version. 95 mh = cache[cacheIndex]; 96 } 97 } 98 return mh; 99 } 100 101 static final class ArrayAccessor { 102 /// Support for array element access 103 static final int GETTER_INDEX = 0, SETTER_INDEX = 1, INDEX_LIMIT = 2; 104 static final ClassValue<MethodHandle[]> TYPED_ACCESSORS 105 = new ClassValue<MethodHandle[]>() { 106 @Override 107 protected MethodHandle[] computeValue(Class<?> type) { 108 return new MethodHandle[INDEX_LIMIT]; 109 } 110 }; 111 static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER; 112 static { 113 MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class); 114 cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = getAccessor(Object[].class, false); 115 cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = getAccessor(Object[].class, true); 116 117 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName())); 118 assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName())); 119 } 120 121 static int getElementI(int[] a, int i) { return a[i]; } 122 static long getElementJ(long[] a, int i) { return a[i]; } 123 static float getElementF(float[] a, int i) { return a[i]; } 124 static double getElementD(double[] a, int i) { return a[i]; } 125 static boolean getElementZ(boolean[] a, int i) { return a[i]; } 126 static byte getElementB(byte[] a, int i) { return a[i]; } 127 static short getElementS(short[] a, int i) { return a[i]; } 128 static char getElementC(char[] a, int i) { return a[i]; } 129 static Object getElementL(Object[] a, int i) { return a[i]; } 130 131 static void setElementI(int[] a, int i, int x) { a[i] = x; } 132 static void setElementJ(long[] a, int i, long x) { a[i] = x; } 133 static void setElementF(float[] a, int i, float x) { a[i] = x; } 134 static void setElementD(double[] a, int i, double x) { a[i] = x; } 135 static void setElementZ(boolean[] a, int i, boolean x) { a[i] = x; } 136 static void setElementB(byte[] a, int i, byte x) { a[i] = x; } 137 static void setElementS(short[] a, int i, short x) { a[i] = x; } 138 static void setElementC(char[] a, int i, char x) { a[i] = x; } 139 static void setElementL(Object[] a, int i, Object x) { a[i] = x; } 140 141 static String name(Class<?> arrayClass, boolean isSetter) { 142 Class<?> elemClass = arrayClass.getComponentType(); 143 if (elemClass == null) throw newIllegalArgumentException("not an array", arrayClass); 144 return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass); 145 } 146 static MethodType type(Class<?> arrayClass, boolean isSetter) { 147 Class<?> elemClass = arrayClass.getComponentType(); 148 Class<?> arrayArgClass = arrayClass; 149 if (!elemClass.isPrimitive()) { 150 arrayArgClass = Object[].class; 151 elemClass = Object.class; 152 } 153 return !isSetter ? 154 MethodType.methodType(elemClass, arrayArgClass, int.class) : 155 MethodType.methodType(void.class, arrayArgClass, int.class, elemClass); 156 } 157 static MethodType correctType(Class<?> arrayClass, boolean isSetter) { 158 Class<?> elemClass = arrayClass.getComponentType(); 159 return !isSetter ? 160 MethodType.methodType(elemClass, arrayClass, int.class) : 161 MethodType.methodType(void.class, arrayClass, int.class, elemClass); 162 } 163 static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) { 164 String name = name(arrayClass, isSetter); 165 MethodType type = type(arrayClass, isSetter); 166 try { 167 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type); 168 } catch (ReflectiveOperationException ex) { 169 throw uncaughtException(ex); 170 } 171 } 172 } 173 174 /** 175 * Create a JVM-level adapter method handle to conform the given method 176 * handle to the similar newType, using only pairwise argument conversions. 177 * For each argument, convert incoming argument to the exact type needed. 178 * The argument conversions allowed are casting, boxing and unboxing, 179 * integral widening or narrowing, and floating point widening or narrowing. 180 * @param srcType required call type 181 * @param target original method handle 182 * @param level which strength of conversion is allowed 183 * @return an adapter to the original handle with the desired new type, 184 * or the original target if the types are already identical 185 * or null if the adaptation cannot be made 186 */ 187 static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType, int level) { 188 assert(level >= 0 && level <= 2); 189 MethodType dstType = target.type(); 190 assert(dstType.parameterCount() == target.type().parameterCount()); 191 if (srcType == dstType) 192 return target; 193 194 // Calculate extra arguments (temporaries) required in the names array. 195 // FIXME: Use an ArrayList<Name>. Some arguments require more than one conversion step. 196 final int INARG_COUNT = srcType.parameterCount(); 197 int conversions = 0; 198 boolean[] needConv = new boolean[1+INARG_COUNT]; 199 for (int i = 0; i <= INARG_COUNT; i++) { 200 Class<?> src = (i == INARG_COUNT) ? dstType.returnType() : srcType.parameterType(i); 201 Class<?> dst = (i == INARG_COUNT) ? srcType.returnType() : dstType.parameterType(i); 202 if (!VerifyType.isNullConversion(src, dst, false) || 203 level <= 1 && dst.isInterface() && !dst.isAssignableFrom(src)) { 204 needConv[i] = true; 205 conversions++; 206 } 207 } 208 boolean retConv = needConv[INARG_COUNT]; 209 if (retConv && srcType.returnType() == void.class) { 210 retConv = false; 211 conversions--; 212 } 213 214 final int IN_MH = 0; 215 final int INARG_BASE = 1; 216 final int INARG_LIMIT = INARG_BASE + INARG_COUNT; 217 final int NAME_LIMIT = INARG_LIMIT + conversions + 1; 218 final int RETURN_CONV = (!retConv ? -1 : NAME_LIMIT - 1); 219 final int OUT_CALL = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1; 220 final int RESULT = (srcType.returnType() == void.class ? -1 : NAME_LIMIT - 1); 221 222 // Now build a LambdaForm. 223 MethodType lambdaType = srcType.basicType().invokerType(); 224 Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType); 225 226 // Collect the arguments to the outgoing call, maybe with conversions: 227 final int OUTARG_BASE = 0; // target MH is Name.function, name Name.arguments[0] 228 Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT]; 229 230 int nameCursor = INARG_LIMIT; 231 for (int i = 0; i < INARG_COUNT; i++) { 232 Class<?> src = srcType.parameterType(i); 233 Class<?> dst = dstType.parameterType(i); 234 235 if (!needConv[i]) { 236 // do nothing: difference is trivial 237 outArgs[OUTARG_BASE + i] = names[INARG_BASE + i]; 238 continue; 239 } 240 241 // Tricky case analysis follows. 242 MethodHandle fn = null; 243 if (src.isPrimitive()) { 244 if (dst.isPrimitive()) { 245 fn = ValueConversions.convertPrimitive(src, dst); 246 } else { 247 Wrapper w = Wrapper.forPrimitiveType(src); 248 MethodHandle boxMethod = ValueConversions.box(w); 249 if (dst == w.wrapperType()) 250 fn = boxMethod; 251 else 252 fn = boxMethod.asType(MethodType.methodType(dst, src)); 253 } 254 } else { 255 if (dst.isPrimitive()) { 256 // Caller has boxed a primitive. Unbox it for the target. 257 Wrapper w = Wrapper.forPrimitiveType(dst); 258 if (level == 0 || VerifyType.isNullConversion(src, w.wrapperType(), false)) { 259 fn = ValueConversions.unbox(dst); 260 } else if (src == Object.class || !Wrapper.isWrapperType(src)) { 261 // Examples: Object->int, Number->int, Comparable->int; Byte->int, Character->int 262 // must include additional conversions 263 // src must be examined at runtime, to detect Byte, Character, etc. 264 MethodHandle unboxMethod = (level == 1 265 ? ValueConversions.unbox(dst) 266 : ValueConversions.unboxCast(dst)); 267 fn = unboxMethod; 268 } else { 269 // Example: Byte->int 270 // Do this by reformulating the problem to Byte->byte. 271 Class<?> srcPrim = Wrapper.forWrapperType(src).primitiveType(); 272 MethodHandle unbox = ValueConversions.unbox(srcPrim); 273 // Compose the two conversions. FIXME: should make two Names for this job 274 fn = unbox.asType(MethodType.methodType(dst, src)); 275 } 276 } else { 277 // Simple reference conversion. 278 // Note: Do not check for a class hierarchy relation 279 // between src and dst. In all cases a 'null' argument 280 // will pass the cast conversion. 281 fn = ValueConversions.cast(dst, Lazy.MH_castReference); 282 } 283 } 284 Name conv = new Name(fn, names[INARG_BASE + i]); 285 assert(names[nameCursor] == null); 286 names[nameCursor++] = conv; 287 assert(outArgs[OUTARG_BASE + i] == null); 288 outArgs[OUTARG_BASE + i] = conv; 289 } 290 291 // Build argument array for the call. 292 assert(nameCursor == OUT_CALL); 293 names[OUT_CALL] = new Name(target, outArgs); 294 295 if (RETURN_CONV < 0) { 296 assert(OUT_CALL == names.length-1); 297 } else { 298 Class<?> needReturn = srcType.returnType(); 299 Class<?> haveReturn = dstType.returnType(); 300 MethodHandle fn; 301 Object[] arg = { names[OUT_CALL] }; 302 if (haveReturn == void.class) { 303 // synthesize a zero value for the given void 304 Object zero = Wrapper.forBasicType(needReturn).zero(); 305 fn = MethodHandles.constant(needReturn, zero); 306 arg = new Object[0]; // don't pass names[OUT_CALL] to conversion 307 } else { 308 MethodHandle identity = MethodHandles.identity(needReturn); 309 MethodType needConversion = identity.type().changeParameterType(0, haveReturn); 310 fn = makePairwiseConvert(identity, needConversion, level); 311 } 312 assert(names[RETURN_CONV] == null); 313 names[RETURN_CONV] = new Name(fn, arg); 314 assert(RETURN_CONV == names.length-1); 315 } 316 317 LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names, RESULT); 318 return SimpleMethodHandle.make(srcType, form); 319 } 320 321 /** 322 * Identity function, with reference cast. 323 * @param t an arbitrary reference type 324 * @param x an arbitrary reference value 325 * @return the same value x 326 */ 327 @ForceInline 328 @SuppressWarnings("unchecked") 329 static <T,U> T castReference(Class<? extends T> t, U x) { 330 // inlined Class.cast because we can't ForceInline it 331 if (x != null && !t.isInstance(x)) 332 throw newClassCastException(t, x); 333 return (T) x; 334 } 335 336 private static ClassCastException newClassCastException(Class<?> t, Object obj) { 337 return new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + t.getName()); 338 } 339 340 static MethodHandle makeReferenceIdentity(Class<?> refType) { 341 MethodType lambdaType = MethodType.genericMethodType(1).invokerType(); 342 Name[] names = arguments(1, lambdaType); 343 names[names.length - 1] = new Name(ValueConversions.identity(), names[1]); 344 LambdaForm form = new LambdaForm("identity", lambdaType.parameterCount(), names); 345 return SimpleMethodHandle.make(MethodType.methodType(refType, refType), form); 346 } 347 348 static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) { 349 MethodType type = target.type(); 350 int last = type.parameterCount() - 1; 351 if (type.parameterType(last) != arrayType) 352 target = target.asType(type.changeParameterType(last, arrayType)); 353 target = target.asFixedArity(); // make sure this attribute is turned off 354 return new AsVarargsCollector(target, target.type(), arrayType); 355 } 356 357 static class AsVarargsCollector extends MethodHandle { 358 private final MethodHandle target; 359 private final Class<?> arrayType; 360 private /*@Stable*/ MethodHandle asCollectorCache; 361 362 AsVarargsCollector(MethodHandle target, MethodType type, Class<?> arrayType) { 363 super(type, reinvokerForm(target)); 364 this.target = target; 365 this.arrayType = arrayType; 366 this.asCollectorCache = target.asCollector(arrayType, 0); 367 } 368 369 @Override MethodHandle reinvokerTarget() { return target; } 370 371 @Override 372 public boolean isVarargsCollector() { 373 return true; 374 } 375 376 @Override 377 public MethodHandle asFixedArity() { 378 return target; 379 } 380 381 @Override 382 public MethodHandle asTypeUncached(MethodType newType) { 383 MethodType type = this.type(); 384 int collectArg = type.parameterCount() - 1; 385 int newArity = newType.parameterCount(); 386 if (newArity == collectArg+1 && 387 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) { 388 // if arity and trailing parameter are compatible, do normal thing 389 return asTypeCache = asFixedArity().asType(newType); 390 } 391 // check cache 392 MethodHandle acc = asCollectorCache; 393 if (acc != null && acc.type().parameterCount() == newArity) 394 return asTypeCache = acc.asType(newType); 395 // build and cache a collector 396 int arrayLength = newArity - collectArg; 397 MethodHandle collector; 398 try { 399 collector = asFixedArity().asCollector(arrayType, arrayLength); 400 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector; 401 } catch (IllegalArgumentException ex) { 402 throw new WrongMethodTypeException("cannot build collector", ex); 403 } 404 asCollectorCache = collector; 405 return asTypeCache = collector.asType(newType); 406 } 407 408 @Override 409 MethodHandle setVarargs(MemberName member) { 410 if (member.isVarargs()) return this; 411 return asFixedArity(); 412 } 413 414 @Override 415 MethodHandle viewAsType(MethodType newType) { 416 if (newType.lastParameterType() != type().lastParameterType()) 417 throw new InternalError(); 418 MethodHandle newTarget = asFixedArity().viewAsType(newType); 419 // put back the varargs bit: 420 return new AsVarargsCollector(newTarget, newType, arrayType); 421 } 422 423 @Override 424 MemberName internalMemberName() { 425 return asFixedArity().internalMemberName(); 426 } 427 @Override 428 Class<?> internalCallerClass() { 429 return asFixedArity().internalCallerClass(); 430 } 431 432 /*non-public*/ 433 @Override 434 boolean isInvokeSpecial() { 435 return asFixedArity().isInvokeSpecial(); 436 } 437 438 439 @Override 440 MethodHandle bindArgument(int pos, BasicType basicType, Object value) { 441 return asFixedArity().bindArgument(pos, basicType, value); 442 } 443 444 @Override 445 MethodHandle bindReceiver(Object receiver) { 446 return asFixedArity().bindReceiver(receiver); 447 } 448 449 @Override 450 MethodHandle dropArguments(MethodType srcType, int pos, int drops) { 451 return asFixedArity().dropArguments(srcType, pos, drops); 452 } 453 454 @Override 455 MethodHandle permuteArguments(MethodType newType, int[] reorder) { 456 return asFixedArity().permuteArguments(newType, reorder); 457 } 458 } 459 460 /** Factory method: Spread selected argument. */ 461 static MethodHandle makeSpreadArguments(MethodHandle target, 462 Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) { 463 MethodType targetType = target.type(); 464 465 for (int i = 0; i < spreadArgCount; i++) { 466 Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i); 467 if (arg == null) arg = Object.class; 468 targetType = targetType.changeParameterType(spreadArgPos + i, arg); 469 } 470 target = target.asType(targetType); 471 472 MethodType srcType = targetType 473 .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType); 474 // Now build a LambdaForm. 475 MethodType lambdaType = srcType.invokerType(); 476 Name[] names = arguments(spreadArgCount + 2, lambdaType); 477 int nameCursor = lambdaType.parameterCount(); 478 int[] indexes = new int[targetType.parameterCount()]; 479 480 for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) { 481 Class<?> src = lambdaType.parameterType(i); 482 if (i == spreadArgPos) { 483 // Spread the array. 484 MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType); 485 Name array = names[argIndex]; 486 names[nameCursor++] = new Name(Lazy.NF_checkSpreadArgument, array, spreadArgCount); 487 for (int j = 0; j < spreadArgCount; i++, j++) { 488 indexes[i] = nameCursor; 489 names[nameCursor++] = new Name(aload, array, j); 490 } 491 } else if (i < indexes.length) { 492 indexes[i] = argIndex; 493 } 494 } 495 assert(nameCursor == names.length-1); // leave room for the final call 496 497 // Build argument array for the call. 498 Name[] targetArgs = new Name[targetType.parameterCount()]; 499 for (int i = 0; i < targetType.parameterCount(); i++) { 500 int idx = indexes[i]; 501 targetArgs[i] = names[idx]; 502 } 503 names[names.length - 1] = new Name(target, (Object[]) targetArgs); 504 505 LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names); 506 return SimpleMethodHandle.make(srcType, form); 507 } 508 509 static void checkSpreadArgument(Object av, int n) { 510 if (av == null) { 511 if (n == 0) return; 512 } else if (av instanceof Object[]) { 513 int len = ((Object[])av).length; 514 if (len == n) return; 515 } else { 516 int len = java.lang.reflect.Array.getLength(av); 517 if (len == n) return; 518 } 519 // fall through to error: 520 throw newIllegalArgumentException("array is not of length "+n); 521 } 522 523 /** 524 * Pre-initialized NamedFunctions for bootstrapping purposes. 525 * Factored in an inner class to delay initialization until first usage. 526 */ 527 private static class Lazy { 528 private static final Class<?> MHI = MethodHandleImpl.class; 529 530 static final NamedFunction NF_checkSpreadArgument; 531 static final NamedFunction NF_guardWithCatch; 532 static final NamedFunction NF_selectAlternative; 533 static final NamedFunction NF_throwException; 534 535 static final MethodHandle MH_castReference; 536 static final MethodHandle MH_copyAsPrimitiveArray; 537 static final MethodHandle MH_copyAsReferenceArray; 538 static final MethodHandle MH_fillNewTypedArray; 539 static final MethodHandle MH_fillNewArray; 540 static final MethodHandle MH_arrayIdentity; 541 542 static { 543 try { 544 NF_checkSpreadArgument = new NamedFunction(MHI.getDeclaredMethod("checkSpreadArgument", Object.class, int.class)); 545 NF_guardWithCatch = new NamedFunction(MHI.getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class, 546 MethodHandle.class, Object[].class)); 547 NF_selectAlternative = new NamedFunction(MHI.getDeclaredMethod("selectAlternative", boolean.class, MethodHandle.class, 548 MethodHandle.class)); 549 NF_throwException = new NamedFunction(MHI.getDeclaredMethod("throwException", Throwable.class)); 550 551 NF_checkSpreadArgument.resolve(); 552 NF_guardWithCatch.resolve(); 553 NF_selectAlternative.resolve(); 554 NF_throwException.resolve(); 555 556 MH_castReference = IMPL_LOOKUP.findStatic(MHI, "castReference", 557 MethodType.methodType(Object.class, Class.class, Object.class)); 558 MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray", 559 MethodType.methodType(Object.class, Wrapper.class, Object[].class)); 560 MH_copyAsReferenceArray = IMPL_LOOKUP.findStatic(MHI, "copyAsReferenceArray", 561 MethodType.methodType(Object[].class, Class.class, Object[].class)); 562 MH_arrayIdentity = IMPL_LOOKUP.findStatic(MHI, "identity", 563 MethodType.methodType(Object[].class, Object[].class)); 564 MH_fillNewArray = IMPL_LOOKUP.findStatic(MHI, "fillNewArray", 565 MethodType.methodType(Object[].class, Integer.class, Object[].class)); 566 MH_fillNewTypedArray = IMPL_LOOKUP.findStatic(MHI, "fillNewTypedArray", 567 MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class)); 568 } catch (ReflectiveOperationException ex) { 569 throw newInternalError(ex); 570 } 571 } 572 } 573 574 /** Factory method: Collect or filter selected argument(s). */ 575 static MethodHandle makeCollectArguments(MethodHandle target, 576 MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) { 577 MethodType targetType = target.type(); // (a..., c, [b...])=>r 578 MethodType collectorType = collector.type(); // (b...)=>c 579 int collectArgCount = collectorType.parameterCount(); 580 Class<?> collectValType = collectorType.returnType(); 581 int collectValCount = (collectValType == void.class ? 0 : 1); 582 MethodType srcType = targetType // (a..., [b...])=>r 583 .dropParameterTypes(collectArgPos, collectArgPos+collectValCount); 584 if (!retainOriginalArgs) { // (a..., b...)=>r 585 srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList()); 586 } 587 // in arglist: [0: ...keep1 | cpos: collect... | cpos+cacount: keep2... ] 588 // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ] 589 // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ] 590 591 // Now build a LambdaForm. 592 MethodType lambdaType = srcType.invokerType(); 593 Name[] names = arguments(2, lambdaType); 594 final int collectNamePos = names.length - 2; 595 final int targetNamePos = names.length - 1; 596 597 Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount); 598 names[collectNamePos] = new Name(collector, (Object[]) collectorArgs); 599 600 // Build argument array for the target. 601 // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ]. 602 // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ]. 603 Name[] targetArgs = new Name[targetType.parameterCount()]; 604 int inputArgPos = 1; // incoming LF args to copy to target 605 int targetArgPos = 0; // fill pointer for targetArgs 606 int chunk = collectArgPos; // |headArgs| 607 System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk); 608 inputArgPos += chunk; 609 targetArgPos += chunk; 610 if (collectValType != void.class) { 611 targetArgs[targetArgPos++] = names[collectNamePos]; 612 } 613 chunk = collectArgCount; 614 if (retainOriginalArgs) { 615 System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk); 616 targetArgPos += chunk; // optionally pass on the collected chunk 617 } 618 inputArgPos += chunk; 619 chunk = targetArgs.length - targetArgPos; // all the rest 620 System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk); 621 assert(inputArgPos + chunk == collectNamePos); // use of rest of input args also 622 names[targetNamePos] = new Name(target, (Object[]) targetArgs); 623 624 LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names); 625 return SimpleMethodHandle.make(srcType, form); 626 } 627 628 @LambdaForm.Hidden 629 static 630 MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) { 631 return testResult ? target : fallback; 632 } 633 634 static 635 MethodHandle makeGuardWithTest(MethodHandle test, 636 MethodHandle target, 637 MethodHandle fallback) { 638 MethodType basicType = target.type().basicType(); 639 MethodHandle invokeBasic = MethodHandles.basicInvoker(basicType); 640 int arity = basicType.parameterCount(); 641 int extraNames = 3; 642 MethodType lambdaType = basicType.invokerType(); 643 Name[] names = arguments(extraNames, lambdaType); 644 645 Object[] testArgs = Arrays.copyOfRange(names, 1, 1 + arity, Object[].class); 646 Object[] targetArgs = Arrays.copyOfRange(names, 0, 1 + arity, Object[].class); 647 648 // call test 649 names[arity + 1] = new Name(test, testArgs); 650 651 // call selectAlternative 652 Object[] selectArgs = { names[arity + 1], target, fallback }; 653 names[arity + 2] = new Name(Lazy.NF_selectAlternative, selectArgs); 654 targetArgs[0] = names[arity + 2]; 655 656 // call target or fallback 657 names[arity + 3] = new Name(new NamedFunction(invokeBasic), targetArgs); 658 659 LambdaForm form = new LambdaForm("guard", lambdaType.parameterCount(), names); 660 return SimpleMethodHandle.make(target.type(), form); 661 } 662 663 /** 664 * The LambaForm shape for catchException combinator is the following: 665 * <blockquote><pre>{@code 666 * guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{ 667 * t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L); 668 * t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L); 669 * t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L); 670 * t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L); 671 * t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L); 672 * t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L); 673 * t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L); 674 * t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I} 675 * }</pre></blockquote> 676 * 677 * argL0 and argL2 are target and catcher method handles. argL1 is exception class. 678 * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[] 679 * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()). 680 * 681 * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms 682 * among catchException combinators with the same basic type. 683 */ 684 private static LambdaForm makeGuardWithCatchForm(MethodType basicType) { 685 MethodType lambdaType = basicType.invokerType(); 686 687 LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC); 688 if (lform != null) { 689 return lform; 690 } 691 final int THIS_MH = 0; // the BMH_LLLLL 692 final int ARG_BASE = 1; // start of incoming arguments 693 final int ARG_LIMIT = ARG_BASE + basicType.parameterCount(); 694 695 int nameCursor = ARG_LIMIT; 696 final int GET_TARGET = nameCursor++; 697 final int GET_CLASS = nameCursor++; 698 final int GET_CATCHER = nameCursor++; 699 final int GET_COLLECT_ARGS = nameCursor++; 700 final int GET_UNBOX_RESULT = nameCursor++; 701 final int BOXED_ARGS = nameCursor++; 702 final int TRY_CATCH = nameCursor++; 703 final int UNBOX_RESULT = nameCursor++; 704 705 Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType); 706 707 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL(); 708 names[GET_TARGET] = new Name(data.getterFunction(0), names[THIS_MH]); 709 names[GET_CLASS] = new Name(data.getterFunction(1), names[THIS_MH]); 710 names[GET_CATCHER] = new Name(data.getterFunction(2), names[THIS_MH]); 711 names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]); 712 names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]); 713 714 // FIXME: rework argument boxing/result unboxing logic for LF interpretation 715 716 // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...); 717 MethodType collectArgsType = basicType.changeReturnType(Object.class); 718 MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType); 719 Object[] args = new Object[invokeBasic.type().parameterCount()]; 720 args[0] = names[GET_COLLECT_ARGS]; 721 System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE); 722 names[BOXED_ARGS] = new Name(new NamedFunction(invokeBasic), args); 723 724 // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L); 725 Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]}; 726 names[TRY_CATCH] = new Name(Lazy.NF_guardWithCatch, gwcArgs); 727 728 // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L); 729 MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class)); 730 Object[] unboxArgs = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]}; 731 names[UNBOX_RESULT] = new Name(new NamedFunction(invokeBasicUnbox), unboxArgs); 732 733 lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names); 734 735 return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform); 736 } 737 738 static 739 MethodHandle makeGuardWithCatch(MethodHandle target, 740 Class<? extends Throwable> exType, 741 MethodHandle catcher) { 742 MethodType type = target.type(); 743 LambdaForm form = makeGuardWithCatchForm(type.basicType()); 744 745 // Prepare auxiliary method handles used during LambdaForm interpreation. 746 // Box arguments and wrap them into Object[]: ValueConversions.array(). 747 MethodType varargsType = type.changeReturnType(Object[].class); 748 MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType); 749 // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). 750 MethodHandle unboxResult; 751 if (type.returnType().isPrimitive()) { 752 unboxResult = ValueConversions.unbox(type.returnType()); 753 } else { 754 unboxResult = ValueConversions.identity(); 755 } 756 757 BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL(); 758 BoundMethodHandle mh; 759 try { 760 mh = (BoundMethodHandle) 761 data.constructor[0].invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher, 762 (Object) collectArgs, (Object) unboxResult); 763 } catch (Throwable ex) { 764 throw uncaughtException(ex); 765 } 766 assert(mh.type() == type); 767 return mh; 768 } 769 770 /** 771 * Intrinsified during LambdaForm compilation 772 * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}). 773 */ 774 @LambdaForm.Hidden 775 static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher, 776 Object... av) throws Throwable { 777 // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case. 778 try { 779 return target.asFixedArity().invokeWithArguments(av); 780 } catch (Throwable t) { 781 if (!exType.isInstance(t)) throw t; 782 return catcher.asFixedArity().invokeWithArguments(prepend(t, av)); 783 } 784 } 785 786 /** Prepend an element {@code elem} to an {@code array}. */ 787 @LambdaForm.Hidden 788 private static Object[] prepend(Object elem, Object[] array) { 789 Object[] newArray = new Object[array.length+1]; 790 newArray[0] = elem; 791 System.arraycopy(array, 0, newArray, 1, array.length); 792 return newArray; 793 } 794 795 static 796 MethodHandle throwException(MethodType type) { 797 assert(Throwable.class.isAssignableFrom(type.parameterType(0))); 798 int arity = type.parameterCount(); 799 if (arity > 1) { 800 return throwException(type.dropParameterTypes(1, arity)).dropArguments(type, 1, arity-1); 801 } 802 return makePairwiseConvert(Lazy.NF_throwException.resolvedHandle(), type, 2); 803 } 804 805 static <T extends Throwable> Empty throwException(T t) throws T { throw t; } 806 807 static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2]; 808 static MethodHandle fakeMethodHandleInvoke(MemberName method) { 809 int idx; 810 assert(method.isMethodHandleInvoke()); 811 switch (method.getName()) { 812 case "invoke": idx = 0; break; 813 case "invokeExact": idx = 1; break; 814 default: throw new InternalError(method.getName()); 815 } 816 MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx]; 817 if (mh != null) return mh; 818 MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class, 819 MethodHandle.class, Object[].class); 820 mh = throwException(type); 821 mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle")); 822 if (!method.getInvocationType().equals(mh.type())) 823 throw new InternalError(method.toString()); 824 mh = mh.withInternalMemberName(method); 825 mh = mh.asVarargsCollector(Object[].class); 826 assert(method.isVarargs()); 827 FAKE_METHOD_HANDLE_INVOKE[idx] = mh; 828 return mh; 829 } 830 831 /** 832 * Create an alias for the method handle which, when called, 833 * appears to be called from the same class loader and protection domain 834 * as hostClass. 835 * This is an expensive no-op unless the method which is called 836 * is sensitive to its caller. A small number of system methods 837 * are in this category, including Class.forName and Method.invoke. 838 */ 839 static 840 MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) { 841 return BindCaller.bindCaller(mh, hostClass); 842 } 843 844 // Put the whole mess into its own nested class. 845 // That way we can lazily load the code and set up the constants. 846 private static class BindCaller { 847 static 848 MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) { 849 // Do not use this function to inject calls into system classes. 850 if (hostClass == null 851 || (hostClass.isArray() || 852 hostClass.isPrimitive() || 853 hostClass.getName().startsWith("java.") || 854 hostClass.getName().startsWith("sun."))) { 855 throw new InternalError(); // does not happen, and should not anyway 856 } 857 // For simplicity, convert mh to a varargs-like method. 858 MethodHandle vamh = prepareForInvoker(mh); 859 // Cache the result of makeInjectedInvoker once per argument class. 860 MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass); 861 return restoreToType(bccInvoker.bindTo(vamh), mh.type(), mh.internalMemberName(), hostClass); 862 } 863 864 private static MethodHandle makeInjectedInvoker(Class<?> hostClass) { 865 Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null); 866 if (hostClass.getClassLoader() != bcc.getClassLoader()) 867 throw new InternalError(hostClass.getName()+" (CL)"); 868 try { 869 if (hostClass.getProtectionDomain() != bcc.getProtectionDomain()) 870 throw new InternalError(hostClass.getName()+" (PD)"); 871 } catch (SecurityException ex) { 872 // Self-check was blocked by security manager. This is OK. 873 // In fact the whole try body could be turned into an assertion. 874 } 875 try { 876 MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class)); 877 init.invokeExact(); // force initialization of the class 878 } catch (Throwable ex) { 879 throw uncaughtException(ex); 880 } 881 MethodHandle bccInvoker; 882 try { 883 MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class); 884 bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT); 885 } catch (ReflectiveOperationException ex) { 886 throw uncaughtException(ex); 887 } 888 // Test the invoker, to ensure that it really injects into the right place. 889 try { 890 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass); 891 Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc}); 892 } catch (Throwable ex) { 893 throw new InternalError(ex); 894 } 895 return bccInvoker; 896 } 897 private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() { 898 @Override protected MethodHandle computeValue(Class<?> hostClass) { 899 return makeInjectedInvoker(hostClass); 900 } 901 }; 902 903 // Adapt mh so that it can be called directly from an injected invoker: 904 private static MethodHandle prepareForInvoker(MethodHandle mh) { 905 mh = mh.asFixedArity(); 906 MethodType mt = mh.type(); 907 int arity = mt.parameterCount(); 908 MethodHandle vamh = mh.asType(mt.generic()); 909 vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames 910 vamh = vamh.asSpreader(Object[].class, arity); 911 vamh.internalForm().compileToBytecode(); // eliminate LFI stack frames 912 return vamh; 913 } 914 915 // Undo the adapter effect of prepareForInvoker: 916 private static MethodHandle restoreToType(MethodHandle vamh, MethodType type, 917 MemberName member, 918 Class<?> hostClass) { 919 MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount()); 920 mh = mh.asType(type); 921 mh = new WrappedMember(mh, type, member, hostClass); 922 return mh; 923 } 924 925 private static final MethodHandle MH_checkCallerClass; 926 static { 927 final Class<?> THIS_CLASS = BindCaller.class; 928 assert(checkCallerClass(THIS_CLASS, THIS_CLASS)); 929 try { 930 MH_checkCallerClass = IMPL_LOOKUP 931 .findStatic(THIS_CLASS, "checkCallerClass", 932 MethodType.methodType(boolean.class, Class.class, Class.class)); 933 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS)); 934 } catch (Throwable ex) { 935 throw new InternalError(ex); 936 } 937 } 938 939 @CallerSensitive 940 private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) { 941 // This method is called via MH_checkCallerClass and so it's 942 // correct to ask for the immediate caller here. 943 Class<?> actual = Reflection.getCallerClass(); 944 if (actual != expected && actual != expected2) 945 throw new InternalError("found "+actual.getName()+", expected "+expected.getName() 946 +(expected == expected2 ? "" : ", or else "+expected2.getName())); 947 return true; 948 } 949 950 private static final byte[] T_BYTES; 951 static { 952 final Object[] values = {null}; 953 AccessController.doPrivileged(new PrivilegedAction<Void>() { 954 public Void run() { 955 try { 956 Class<T> tClass = T.class; 957 String tName = tClass.getName(); 958 String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class"; 959 java.net.URLConnection uconn = tClass.getResource(tResource).openConnection(); 960 int len = uconn.getContentLength(); 961 byte[] bytes = new byte[len]; 962 try (java.io.InputStream str = uconn.getInputStream()) { 963 int nr = str.read(bytes); 964 if (nr != len) throw new java.io.IOException(tResource); 965 } 966 values[0] = bytes; 967 } catch (java.io.IOException ex) { 968 throw new InternalError(ex); 969 } 970 return null; 971 } 972 }); 973 T_BYTES = (byte[]) values[0]; 974 } 975 976 // The following class is used as a template for Unsafe.defineAnonymousClass: 977 private static class T { 978 static void init() { } // side effect: initializes this class 979 static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable { 980 return vamh.invokeExact(args); 981 } 982 } 983 } 984 985 986 /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */ 987 static class WrappedMember extends MethodHandle { 988 private final MethodHandle target; 989 private final MemberName member; 990 private final Class<?> callerClass; 991 992 private WrappedMember(MethodHandle target, MethodType type, MemberName member, Class<?> callerClass) { 993 super(type, reinvokerForm(target)); 994 this.target = target; 995 this.member = member; 996 this.callerClass = callerClass; 997 } 998 999 @Override 1000 MethodHandle reinvokerTarget() { 1001 return target; 1002 } 1003 @Override 1004 public MethodHandle asTypeUncached(MethodType newType) { 1005 // This MH is an alias for target, except for the MemberName 1006 // Drop the MemberName if there is any conversion. 1007 return asTypeCache = target.asType(newType); 1008 } 1009 @Override 1010 MemberName internalMemberName() { 1011 return member; 1012 } 1013 @Override 1014 Class<?> internalCallerClass() { 1015 return callerClass; 1016 } 1017 @Override 1018 boolean isInvokeSpecial() { 1019 return target.isInvokeSpecial(); 1020 } 1021 @Override 1022 MethodHandle viewAsType(MethodType newType) { 1023 return new WrappedMember(target, newType, member, callerClass); 1024 } 1025 } 1026 1027 static MethodHandle makeWrappedMember(MethodHandle target, MemberName member) { 1028 if (member.equals(target.internalMemberName())) 1029 return target; 1030 return new WrappedMember(target, target.type(), member, null); 1031 } 1032 1033 /// Collection of multiple arguments. 1034 1035 private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) { 1036 MethodType type = MethodType.genericMethodType(nargs) 1037 .changeReturnType(rtype) 1038 .insertParameterTypes(0, ptypes); 1039 try { 1040 return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, name, type); 1041 } catch (ReflectiveOperationException ex) { 1042 return null; 1043 } 1044 } 1045 1046 private static final Object[] NO_ARGS_ARRAY = {}; 1047 private static Object[] makeArray(Object... args) { return args; } 1048 private static Object[] array() { return NO_ARGS_ARRAY; } 1049 private static Object[] array(Object a0) 1050 { return makeArray(a0); } 1051 private static Object[] array(Object a0, Object a1) 1052 { return makeArray(a0, a1); } 1053 private static Object[] array(Object a0, Object a1, Object a2) 1054 { return makeArray(a0, a1, a2); } 1055 private static Object[] array(Object a0, Object a1, Object a2, Object a3) 1056 { return makeArray(a0, a1, a2, a3); } 1057 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1058 Object a4) 1059 { return makeArray(a0, a1, a2, a3, a4); } 1060 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1061 Object a4, Object a5) 1062 { return makeArray(a0, a1, a2, a3, a4, a5); } 1063 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1064 Object a4, Object a5, Object a6) 1065 { return makeArray(a0, a1, a2, a3, a4, a5, a6); } 1066 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1067 Object a4, Object a5, Object a6, Object a7) 1068 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); } 1069 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1070 Object a4, Object a5, Object a6, Object a7, 1071 Object a8) 1072 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); } 1073 private static Object[] array(Object a0, Object a1, Object a2, Object a3, 1074 Object a4, Object a5, Object a6, Object a7, 1075 Object a8, Object a9) 1076 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } 1077 private static MethodHandle[] makeArrays() { 1078 ArrayList<MethodHandle> mhs = new ArrayList<>(); 1079 for (;;) { 1080 MethodHandle mh = findCollector("array", mhs.size(), Object[].class); 1081 if (mh == null) break; 1082 mhs.add(mh); 1083 } 1084 assert(mhs.size() == 11); // current number of methods 1085 return mhs.toArray(new MethodHandle[MAX_ARITY+1]); 1086 } 1087 private static final MethodHandle[] ARRAYS = makeArrays(); 1088 1089 // filling versions of the above: 1090 // using Integer len instead of int len and no varargs to avoid bootstrapping problems 1091 private static Object[] fillNewArray(Integer len, Object[] /*not ...*/ args) { 1092 Object[] a = new Object[len]; 1093 fillWithArguments(a, 0, args); 1094 return a; 1095 } 1096 private static Object[] fillNewTypedArray(Object[] example, Integer len, Object[] /*not ...*/ args) { 1097 Object[] a = Arrays.copyOf(example, len); 1098 fillWithArguments(a, 0, args); 1099 return a; 1100 } 1101 private static void fillWithArguments(Object[] a, int pos, Object... args) { 1102 System.arraycopy(args, 0, a, pos, args.length); 1103 } 1104 // using Integer pos instead of int pos to avoid bootstrapping problems 1105 private static Object[] fillArray(Integer pos, Object[] a, Object a0) 1106 { fillWithArguments(a, pos, a0); return a; } 1107 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1) 1108 { fillWithArguments(a, pos, a0, a1); return a; } 1109 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2) 1110 { fillWithArguments(a, pos, a0, a1, a2); return a; } 1111 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3) 1112 { fillWithArguments(a, pos, a0, a1, a2, a3); return a; } 1113 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1114 Object a4) 1115 { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; } 1116 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1117 Object a4, Object a5) 1118 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; } 1119 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1120 Object a4, Object a5, Object a6) 1121 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; } 1122 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1123 Object a4, Object a5, Object a6, Object a7) 1124 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); return a; } 1125 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1126 Object a4, Object a5, Object a6, Object a7, 1127 Object a8) 1128 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; } 1129 private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3, 1130 Object a4, Object a5, Object a6, Object a7, 1131 Object a8, Object a9) 1132 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; } 1133 private static MethodHandle[] makeFillArrays() { 1134 ArrayList<MethodHandle> mhs = new ArrayList<>(); 1135 mhs.add(null); // there is no empty fill; at least a0 is required 1136 for (;;) { 1137 MethodHandle mh = findCollector("fillArray", mhs.size(), Object[].class, Integer.class, Object[].class); 1138 if (mh == null) break; 1139 mhs.add(mh); 1140 } 1141 assert(mhs.size() == 11); // current number of methods 1142 return mhs.toArray(new MethodHandle[0]); 1143 } 1144 private static final MethodHandle[] FILL_ARRAYS = makeFillArrays(); 1145 1146 private static Object[] copyAsReferenceArray(Class<? extends Object[]> arrayType, Object... a) { 1147 return Arrays.copyOf(a, a.length, arrayType); 1148 } 1149 private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) { 1150 Object a = w.makeArray(boxes.length); 1151 w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length); 1152 return a; 1153 } 1154 1155 /** Return a method handle that takes the indicated number of Object 1156 * arguments and returns an Object array of them, as if for varargs. 1157 */ 1158 static MethodHandle varargsArray(int nargs) { 1159 MethodHandle mh = ARRAYS[nargs]; 1160 if (mh != null) return mh; 1161 mh = findCollector("array", nargs, Object[].class); 1162 if (mh != null) return ARRAYS[nargs] = mh; 1163 mh = buildVarargsArray(Lazy.MH_fillNewArray, Lazy.MH_arrayIdentity, nargs); 1164 assert(assertCorrectArity(mh, nargs)); 1165 return ARRAYS[nargs] = mh; 1166 } 1167 1168 private static boolean assertCorrectArity(MethodHandle mh, int arity) { 1169 assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh; 1170 return true; 1171 } 1172 1173 // Array identity function (used as Lazy.MH_arrayIdentity). 1174 static <T> T[] identity(T[] x) { 1175 return x; 1176 } 1177 1178 private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) { 1179 // Build up the result mh as a sequence of fills like this: 1180 // finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23)) 1181 // The various fill(_,10*I,___*[J]) are reusable. 1182 int leftLen = Math.min(nargs, LEFT_ARGS); // absorb some arguments immediately 1183 int rightLen = nargs - leftLen; 1184 MethodHandle leftCollector = newArray.bindTo(nargs); 1185 leftCollector = leftCollector.asCollector(Object[].class, leftLen); 1186 MethodHandle mh = finisher; 1187 if (rightLen > 0) { 1188 MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen); 1189 if (mh == Lazy.MH_arrayIdentity) 1190 mh = rightFiller; 1191 else 1192 mh = MethodHandles.collectArguments(mh, 0, rightFiller); 1193 } 1194 if (mh == Lazy.MH_arrayIdentity) 1195 mh = leftCollector; 1196 else 1197 mh = MethodHandles.collectArguments(mh, 0, leftCollector); 1198 return mh; 1199 } 1200 1201 private static final int LEFT_ARGS = (FILL_ARRAYS.length - 1); 1202 private static final MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY+1]; 1203 /** fill_array_to_right(N).invoke(a, argL..arg[N-1]) 1204 * fills a[L]..a[N-1] with corresponding arguments, 1205 * and then returns a. The value L is a global constant (LEFT_ARGS). 1206 */ 1207 private static MethodHandle fillToRight(int nargs) { 1208 MethodHandle filler = FILL_ARRAY_TO_RIGHT[nargs]; 1209 if (filler != null) return filler; 1210 filler = buildFiller(nargs); 1211 assert(assertCorrectArity(filler, nargs - LEFT_ARGS + 1)); 1212 return FILL_ARRAY_TO_RIGHT[nargs] = filler; 1213 } 1214 private static MethodHandle buildFiller(int nargs) { 1215 if (nargs <= LEFT_ARGS) 1216 return Lazy.MH_arrayIdentity; // no args to fill; return the array unchanged 1217 // we need room for both mh and a in mh.invoke(a, arg*[nargs]) 1218 final int CHUNK = LEFT_ARGS; 1219 int rightLen = nargs % CHUNK; 1220 int midLen = nargs - rightLen; 1221 if (rightLen == 0) { 1222 midLen = nargs - (rightLen = CHUNK); 1223 if (FILL_ARRAY_TO_RIGHT[midLen] == null) { 1224 // build some precursors from left to right 1225 for (int j = LEFT_ARGS % CHUNK; j < midLen; j += CHUNK) 1226 if (j > LEFT_ARGS) fillToRight(j); 1227 } 1228 } 1229 if (midLen < LEFT_ARGS) rightLen = nargs - (midLen = LEFT_ARGS); 1230 assert(rightLen > 0); 1231 MethodHandle midFill = fillToRight(midLen); // recursive fill 1232 MethodHandle rightFill = FILL_ARRAYS[rightLen].bindTo(midLen); // [midLen..nargs-1] 1233 assert(midFill.type().parameterCount() == 1 + midLen - LEFT_ARGS); 1234 assert(rightFill.type().parameterCount() == 1 + rightLen); 1235 1236 // Combine the two fills: 1237 // right(mid(a, x10..x19), x20..x23) 1238 // The final product will look like this: 1239 // right(mid(newArrayLeft(24, x0..x9), x10..x19), x20..x23) 1240 if (midLen == LEFT_ARGS) 1241 return rightFill; 1242 else 1243 return MethodHandles.collectArguments(rightFill, 0, midFill); 1244 } 1245 1246 // Type-polymorphic version of varargs maker. 1247 private static final ClassValue<MethodHandle[]> TYPED_COLLECTORS 1248 = new ClassValue<MethodHandle[]>() { 1249 @Override 1250 protected MethodHandle[] computeValue(Class<?> type) { 1251 return new MethodHandle[256]; 1252 } 1253 }; 1254 1255 static final int MAX_JVM_ARITY = 255; // limit imposed by the JVM 1256 1257 /** Return a method handle that takes the indicated number of 1258 * typed arguments and returns an array of them. 1259 * The type argument is the array type. 1260 */ 1261 static MethodHandle varargsArray(Class<?> arrayType, int nargs) { 1262 Class<?> elemType = arrayType.getComponentType(); 1263 if (elemType == null) throw new IllegalArgumentException("not an array: "+arrayType); 1264 // FIXME: Need more special casing and caching here. 1265 if (nargs >= MAX_JVM_ARITY/2 - 1) { 1266 int slots = nargs; 1267 final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1; // 1 for receiver MH 1268 if (arrayType == double[].class || arrayType == long[].class) 1269 slots *= 2; 1270 if (slots > MAX_ARRAY_SLOTS) 1271 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs); 1272 } 1273 if (elemType == Object.class) 1274 return varargsArray(nargs); 1275 // other cases: primitive arrays, subtypes of Object[] 1276 MethodHandle cache[] = TYPED_COLLECTORS.get(elemType); 1277 MethodHandle mh = nargs < cache.length ? cache[nargs] : null; 1278 if (mh != null) return mh; 1279 if (elemType.isPrimitive()) { 1280 MethodHandle builder = Lazy.MH_fillNewArray; 1281 MethodHandle producer = buildArrayProducer(arrayType); 1282 mh = buildVarargsArray(builder, producer, nargs); 1283 } else { 1284 @SuppressWarnings("unchecked") 1285 Class<? extends Object[]> objArrayType = (Class<? extends Object[]>) arrayType; 1286 Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType); 1287 MethodHandle builder = Lazy.MH_fillNewTypedArray.bindTo(example); 1288 MethodHandle producer = Lazy.MH_arrayIdentity; 1289 mh = buildVarargsArray(builder, producer, nargs); 1290 } 1291 mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType))); 1292 assert(assertCorrectArity(mh, nargs)); 1293 if (nargs < cache.length) 1294 cache[nargs] = mh; 1295 return mh; 1296 } 1297 1298 private static MethodHandle buildArrayProducer(Class<?> arrayType) { 1299 Class<?> elemType = arrayType.getComponentType(); 1300 if (elemType.isPrimitive()) 1301 return Lazy.MH_copyAsPrimitiveArray.bindTo(Wrapper.forPrimitiveType(elemType)); 1302 else 1303 return Lazy.MH_copyAsReferenceArray.bindTo(arrayType); 1304 } 1305 }