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 sun.misc.Unsafe; 29 import java.lang.reflect.Method; 30 import java.util.Arrays; 31 import sun.invoke.util.VerifyAccess; 32 import static java.lang.invoke.MethodHandleNatives.Constants.*; 33 import static java.lang.invoke.LambdaForm.*; 34 import static java.lang.invoke.LambdaForm.BasicType.*; 35 import static java.lang.invoke.MethodTypeForm.*; 36 import static java.lang.invoke.MethodHandleStatics.*; 37 import java.lang.ref.WeakReference; 38 import java.lang.reflect.Field; 39 import sun.invoke.util.ValueConversions; 40 import sun.invoke.util.VerifyType; 41 import sun.invoke.util.Wrapper; 42 43 /** 44 * The flavor of method handle which implements a constant reference 45 * to a class member. 46 * @author jrose 47 */ 48 class DirectMethodHandle extends MethodHandle { 49 final MemberName member; 50 51 // Constructors and factory methods in this class *must* be package scoped or private. 52 private DirectMethodHandle(MethodType mtype, LambdaForm form, MemberName member) { 53 super(mtype, form); 54 if (!member.isResolved()) throw new InternalError(); 55 56 if (member.getDeclaringClass().isInterface() && 57 member.isMethod() && !member.isAbstract()) { 58 // Check for corner case: invokeinterface of Object method 59 MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind()); 60 m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null); 61 if (m != null && m.isPublic()) { 62 assert(member.getReferenceKind() == m.getReferenceKind()); // else this.form is wrong 63 member = m; 64 } 65 } 66 67 this.member = member; 68 } 69 70 // Factory methods: 71 static DirectMethodHandle make(byte refKind, Class<?> receiver, MemberName member) { 72 MethodType mtype = member.getMethodOrFieldType(); 73 if (!member.isStatic()) { 74 if (!member.getDeclaringClass().isAssignableFrom(receiver) || member.isConstructor()) 75 throw new InternalError(member.toString()); 76 mtype = mtype.insertParameterTypes(0, receiver); 77 } 78 if (!member.isField()) { 79 if (refKind == REF_invokeSpecial) { 80 member = member.asSpecial(); 81 LambdaForm lform = preparedLambdaForm(member); 82 return new Special(mtype, lform, member); 83 } else { 84 LambdaForm lform = preparedLambdaForm(member); 85 return new DirectMethodHandle(mtype, lform, member); 86 } 87 } else { 88 LambdaForm lform = preparedFieldLambdaForm(member); 89 if (member.isStatic()) { 90 long offset = MethodHandleNatives.staticFieldOffset(member); 91 Object base = MethodHandleNatives.staticFieldBase(member); 92 return new StaticAccessor(mtype, lform, member, base, offset); 93 } else { 94 long offset = MethodHandleNatives.objectFieldOffset(member); 95 assert(offset == (int)offset); 96 return new Accessor(mtype, lform, member, (int)offset); 97 } 98 } 99 } 100 static DirectMethodHandle make(Class<?> receiver, MemberName member) { 101 byte refKind = member.getReferenceKind(); 102 if (refKind == REF_invokeSpecial) 103 refKind = REF_invokeVirtual; 104 return make(refKind, receiver, member); 105 } 106 static DirectMethodHandle make(MemberName member) { 107 if (member.isConstructor()) 108 return makeAllocator(member); 109 return make(member.getDeclaringClass(), member); 110 } 111 static DirectMethodHandle make(Method method) { 112 return make(method.getDeclaringClass(), new MemberName(method)); 113 } 114 static DirectMethodHandle make(Field field) { 115 return make(field.getDeclaringClass(), new MemberName(field)); 116 } 117 private static DirectMethodHandle makeAllocator(MemberName ctor) { 118 assert(ctor.isConstructor() && ctor.getName().equals("<init>")); 119 Class<?> instanceClass = ctor.getDeclaringClass(); 120 ctor = ctor.asConstructor(); 121 assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor; 122 MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass); 123 LambdaForm lform = preparedLambdaForm(ctor); 124 MemberName init = ctor.asSpecial(); 125 assert(init.getMethodType().returnType() == void.class); 126 return new Constructor(mtype, lform, ctor, init, instanceClass); 127 } 128 129 @Override 130 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 131 assert(this.getClass() == DirectMethodHandle.class); // must override in subclasses 132 return new DirectMethodHandle(mt, lf, member); 133 } 134 135 @Override 136 String internalProperties() { 137 return "\n& DMH.MN="+internalMemberName(); 138 } 139 140 //// Implementation methods. 141 @Override 142 @ForceInline 143 MemberName internalMemberName() { 144 return member; 145 } 146 147 private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory(); 148 149 /** 150 * Create a LF which can invoke the given method. 151 * Cache and share this structure among all methods with 152 * the same basicType and refKind. 153 */ 154 private static LambdaForm preparedLambdaForm(MemberName m) { 155 assert(m.isInvocable()) : m; // call preparedFieldLambdaForm instead 156 MethodType mtype = m.getInvocationType().basicType(); 157 assert(!m.isMethodHandleInvoke() || "invokeBasic".equals(m.getName())) : m; 158 int which; 159 switch (m.getReferenceKind()) { 160 case REF_invokeVirtual: which = LF_INVVIRTUAL; break; 161 case REF_invokeStatic: which = LF_INVSTATIC; break; 162 case REF_invokeSpecial: which = LF_INVSPECIAL; break; 163 case REF_invokeInterface: which = LF_INVINTERFACE; break; 164 case REF_newInvokeSpecial: which = LF_NEWINVSPECIAL; break; 165 default: throw new InternalError(m.toString()); 166 } 167 if (which == LF_INVSTATIC && shouldBeInitialized(m)) { 168 // precompute the barrier-free version: 169 preparedLambdaForm(mtype, which); 170 which = LF_INVSTATIC_INIT; 171 } 172 LambdaForm lform = preparedLambdaForm(mtype, which); 173 maybeCompile(lform, m); 174 assert(lform.methodType().dropParameterTypes(0, 1) 175 .equals(m.getInvocationType().basicType())) 176 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); 177 return lform; 178 } 179 180 private static LambdaForm preparedLambdaForm(MethodType mtype, int which) { 181 LambdaForm lform = mtype.form().cachedLambdaForm(which); 182 if (lform != null) return lform; 183 lform = makePreparedLambdaForm(mtype, which); 184 return mtype.form().setCachedLambdaForm(which, lform); 185 } 186 187 private static LambdaForm makePreparedLambdaForm(MethodType mtype, int which) { 188 boolean needsInit = (which == LF_INVSTATIC_INIT); 189 boolean doesAlloc = (which == LF_NEWINVSPECIAL); 190 String linkerName, lambdaName; 191 switch (which) { 192 case LF_INVVIRTUAL: linkerName = "linkToVirtual"; lambdaName = "DMH.invokeVirtual"; break; 193 case LF_INVSTATIC: linkerName = "linkToStatic"; lambdaName = "DMH.invokeStatic"; break; 194 case LF_INVSTATIC_INIT:linkerName = "linkToStatic"; lambdaName = "DMH.invokeStaticInit"; break; 195 case LF_INVSPECIAL: linkerName = "linkToSpecial"; lambdaName = "DMH.invokeSpecial"; break; 196 case LF_INVINTERFACE: linkerName = "linkToInterface"; lambdaName = "DMH.invokeInterface"; break; 197 case LF_NEWINVSPECIAL: linkerName = "linkToSpecial"; lambdaName = "DMH.newInvokeSpecial"; break; 198 default: throw new InternalError("which="+which); 199 } 200 MethodType mtypeWithArg = mtype.appendParameterTypes(MemberName.class); 201 if (doesAlloc) 202 mtypeWithArg = mtypeWithArg 203 .insertParameterTypes(0, Object.class) // insert newly allocated obj 204 .changeReturnType(void.class); // <init> returns void 205 MemberName linker = new MemberName(MethodHandle.class, linkerName, mtypeWithArg, REF_invokeStatic); 206 try { 207 linker = IMPL_NAMES.resolveOrFail(REF_invokeStatic, linker, null, NoSuchMethodException.class); 208 } catch (ReflectiveOperationException ex) { 209 throw newInternalError(ex); 210 } 211 final int DMH_THIS = 0; 212 final int ARG_BASE = 1; 213 final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); 214 int nameCursor = ARG_LIMIT; 215 final int NEW_OBJ = (doesAlloc ? nameCursor++ : -1); 216 final int GET_MEMBER = nameCursor++; 217 final int LINKER_CALL = nameCursor++; 218 Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); 219 assert(names.length == nameCursor); 220 if (doesAlloc) { 221 // names = { argx,y,z,... new C, init method } 222 names[NEW_OBJ] = new Name(Lazy.NF_allocateInstance, names[DMH_THIS]); 223 names[GET_MEMBER] = new Name(Lazy.NF_constructorMethod, names[DMH_THIS]); 224 } else if (needsInit) { 225 names[GET_MEMBER] = new Name(Lazy.NF_internalMemberNameEnsureInit, names[DMH_THIS]); 226 } else { 227 names[GET_MEMBER] = new Name(Lazy.NF_internalMemberName, names[DMH_THIS]); 228 } 229 assert(findDirectMethodHandle(names[GET_MEMBER]) == names[DMH_THIS]); 230 Object[] outArgs = Arrays.copyOfRange(names, ARG_BASE, GET_MEMBER+1, Object[].class); 231 assert(outArgs[outArgs.length-1] == names[GET_MEMBER]); // look, shifted args! 232 int result = LAST_RESULT; 233 if (doesAlloc) { 234 assert(outArgs[outArgs.length-2] == names[NEW_OBJ]); // got to move this one 235 System.arraycopy(outArgs, 0, outArgs, 1, outArgs.length-2); 236 outArgs[0] = names[NEW_OBJ]; 237 result = NEW_OBJ; 238 } 239 names[LINKER_CALL] = new Name(linker, outArgs); 240 lambdaName += "_" + shortenSignature(basicTypeSignature(mtype)); 241 LambdaForm lform = new LambdaForm(lambdaName, ARG_LIMIT, names, result); 242 // This is a tricky bit of code. Don't send it through the LF interpreter. 243 lform.compileToBytecode(); 244 return lform; 245 } 246 247 static Object findDirectMethodHandle(Name name) { 248 if (name.function == Lazy.NF_internalMemberName || 249 name.function == Lazy.NF_internalMemberNameEnsureInit || 250 name.function == Lazy.NF_constructorMethod) { 251 assert(name.arguments.length == 1); 252 return name.arguments[0]; 253 } 254 return null; 255 } 256 257 private static void maybeCompile(LambdaForm lform, MemberName m) { 258 if (VerifyAccess.isSamePackage(m.getDeclaringClass(), MethodHandle.class)) 259 // Help along bootstrapping... 260 lform.compileToBytecode(); 261 } 262 263 /** Static wrapper for DirectMethodHandle.internalMemberName. */ 264 @ForceInline 265 /*non-public*/ static Object internalMemberName(Object mh) { 266 return ((DirectMethodHandle)mh).member; 267 } 268 269 /** Static wrapper for DirectMethodHandle.internalMemberName. 270 * This one also forces initialization. 271 */ 272 /*non-public*/ static Object internalMemberNameEnsureInit(Object mh) { 273 DirectMethodHandle dmh = (DirectMethodHandle)mh; 274 dmh.ensureInitialized(); 275 return dmh.member; 276 } 277 278 /*non-public*/ static 279 boolean shouldBeInitialized(MemberName member) { 280 switch (member.getReferenceKind()) { 281 case REF_invokeStatic: 282 case REF_getStatic: 283 case REF_putStatic: 284 case REF_newInvokeSpecial: 285 break; 286 default: 287 // No need to initialize the class on this kind of member. 288 return false; 289 } 290 Class<?> cls = member.getDeclaringClass(); 291 if (cls == ValueConversions.class || 292 cls == MethodHandleImpl.class || 293 cls == Invokers.class) { 294 // These guys have lots of <clinit> DMH creation but we know 295 // the MHs will not be used until the system is booted. 296 return false; 297 } 298 if (VerifyAccess.isSamePackage(MethodHandle.class, cls) || 299 VerifyAccess.isSamePackage(ValueConversions.class, cls)) { 300 // It is a system class. It is probably in the process of 301 // being initialized, but we will help it along just to be safe. 302 if (UNSAFE.shouldBeInitialized(cls)) { 303 UNSAFE.ensureClassInitialized(cls); 304 } 305 return false; 306 } 307 return UNSAFE.shouldBeInitialized(cls); 308 } 309 310 private static class EnsureInitialized extends ClassValue<WeakReference<Thread>> { 311 @Override 312 protected WeakReference<Thread> computeValue(Class<?> type) { 313 UNSAFE.ensureClassInitialized(type); 314 if (UNSAFE.shouldBeInitialized(type)) 315 // If the previous call didn't block, this can happen. 316 // We are executing inside <clinit>. 317 return new WeakReference<>(Thread.currentThread()); 318 return null; 319 } 320 static final EnsureInitialized INSTANCE = new EnsureInitialized(); 321 } 322 323 private void ensureInitialized() { 324 if (checkInitialized(member)) { 325 // The coast is clear. Delete the <clinit> barrier. 326 if (member.isField()) 327 updateForm(preparedFieldLambdaForm(member)); 328 else 329 updateForm(preparedLambdaForm(member)); 330 } 331 } 332 private static boolean checkInitialized(MemberName member) { 333 Class<?> defc = member.getDeclaringClass(); 334 WeakReference<Thread> ref = EnsureInitialized.INSTANCE.get(defc); 335 if (ref == null) { 336 return true; // the final state 337 } 338 Thread clinitThread = ref.get(); 339 // Somebody may still be running defc.<clinit>. 340 if (clinitThread == Thread.currentThread()) { 341 // If anybody is running defc.<clinit>, it is this thread. 342 if (UNSAFE.shouldBeInitialized(defc)) 343 // Yes, we are running it; keep the barrier for now. 344 return false; 345 } else { 346 // We are in a random thread. Block. 347 UNSAFE.ensureClassInitialized(defc); 348 } 349 assert(!UNSAFE.shouldBeInitialized(defc)); 350 // put it into the final state 351 EnsureInitialized.INSTANCE.remove(defc); 352 return true; 353 } 354 355 /*non-public*/ static void ensureInitialized(Object mh) { 356 ((DirectMethodHandle)mh).ensureInitialized(); 357 } 358 359 /** This subclass represents invokespecial instructions. */ 360 static class Special extends DirectMethodHandle { 361 private Special(MethodType mtype, LambdaForm form, MemberName member) { 362 super(mtype, form, member); 363 } 364 @Override 365 boolean isInvokeSpecial() { 366 return true; 367 } 368 @Override 369 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 370 return new Special(mt, lf, member); 371 } 372 } 373 374 /** This subclass handles constructor references. */ 375 static class Constructor extends DirectMethodHandle { 376 final MemberName initMethod; 377 final Class<?> instanceClass; 378 379 private Constructor(MethodType mtype, LambdaForm form, MemberName constructor, 380 MemberName initMethod, Class<?> instanceClass) { 381 super(mtype, form, constructor); 382 this.initMethod = initMethod; 383 this.instanceClass = instanceClass; 384 assert(initMethod.isResolved()); 385 } 386 @Override 387 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 388 return new Constructor(mt, lf, member, initMethod, instanceClass); 389 } 390 } 391 392 /*non-public*/ static Object constructorMethod(Object mh) { 393 Constructor dmh = (Constructor)mh; 394 return dmh.initMethod; 395 } 396 397 /*non-public*/ static Object allocateInstance(Object mh) throws InstantiationException { 398 Constructor dmh = (Constructor)mh; 399 return UNSAFE.allocateInstance(dmh.instanceClass); 400 } 401 402 /** This subclass handles non-static field references. */ 403 static class Accessor extends DirectMethodHandle { 404 final Class<?> fieldType; 405 final int fieldOffset; 406 private Accessor(MethodType mtype, LambdaForm form, MemberName member, 407 int fieldOffset) { 408 super(mtype, form, member); 409 this.fieldType = member.getFieldType(); 410 this.fieldOffset = fieldOffset; 411 } 412 413 @Override Object checkCast(Object obj) { 414 return fieldType.cast(obj); 415 } 416 @Override 417 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 418 return new Accessor(mt, lf, member, fieldOffset); 419 } 420 } 421 422 @ForceInline 423 /*non-public*/ static long fieldOffset(Object accessorObj) { 424 // Note: We return a long because that is what Unsafe.getObject likes. 425 // We store a plain int because it is more compact. 426 return ((Accessor)accessorObj).fieldOffset; 427 } 428 429 @ForceInline 430 /*non-public*/ static Object checkBase(Object obj) { 431 // Note that the object's class has already been verified, 432 // since the parameter type of the Accessor method handle 433 // is either member.getDeclaringClass or a subclass. 434 // This was verified in DirectMethodHandle.make. 435 // Therefore, the only remaining check is for null. 436 // Since this check is *not* guaranteed by Unsafe.getInt 437 // and its siblings, we need to make an explicit one here. 438 obj.getClass(); // maybe throw NPE 439 return obj; 440 } 441 442 /** This subclass handles static field references. */ 443 static class StaticAccessor extends DirectMethodHandle { 444 final private Class<?> fieldType; 445 final private Object staticBase; 446 final private long staticOffset; 447 448 private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member, 449 Object staticBase, long staticOffset) { 450 super(mtype, form, member); 451 this.fieldType = member.getFieldType(); 452 this.staticBase = staticBase; 453 this.staticOffset = staticOffset; 454 } 455 456 @Override Object checkCast(Object obj) { 457 return fieldType.cast(obj); 458 } 459 @Override 460 MethodHandle copyWith(MethodType mt, LambdaForm lf) { 461 return new StaticAccessor(mt, lf, member, staticBase, staticOffset); 462 } 463 } 464 465 @ForceInline 466 /*non-public*/ static Object nullCheck(Object obj) { 467 obj.getClass(); 468 return obj; 469 } 470 471 @ForceInline 472 /*non-public*/ static Object staticBase(Object accessorObj) { 473 return ((StaticAccessor)accessorObj).staticBase; 474 } 475 476 @ForceInline 477 /*non-public*/ static long staticOffset(Object accessorObj) { 478 return ((StaticAccessor)accessorObj).staticOffset; 479 } 480 481 @ForceInline 482 /*non-public*/ static Object checkCast(Object mh, Object obj) { 483 return ((DirectMethodHandle) mh).checkCast(obj); 484 } 485 486 Object checkCast(Object obj) { 487 return member.getReturnType().cast(obj); 488 } 489 490 // Caching machinery for field accessors: 491 private static byte 492 AF_GETFIELD = 0, 493 AF_PUTFIELD = 1, 494 AF_GETSTATIC = 2, 495 AF_PUTSTATIC = 3, 496 AF_GETSTATIC_INIT = 4, 497 AF_PUTSTATIC_INIT = 5, 498 AF_LIMIT = 6; 499 // Enumerate the different field kinds using Wrapper, 500 // with an extra case added for checked references. 501 private static int 502 FT_LAST_WRAPPER = Wrapper.values().length-1, 503 FT_UNCHECKED_REF = Wrapper.OBJECT.ordinal(), 504 FT_CHECKED_REF = FT_LAST_WRAPPER+1, 505 FT_LIMIT = FT_LAST_WRAPPER+2; 506 private static int afIndex(byte formOp, boolean isVolatile, int ftypeKind) { 507 return ((formOp * FT_LIMIT * 2) 508 + (isVolatile ? FT_LIMIT : 0) 509 + ftypeKind); 510 } 511 private static final LambdaForm[] ACCESSOR_FORMS 512 = new LambdaForm[afIndex(AF_LIMIT, false, 0)]; 513 private static int ftypeKind(Class<?> ftype) { 514 if (ftype.isPrimitive()) 515 return Wrapper.forPrimitiveType(ftype).ordinal(); 516 else if (VerifyType.isNullReferenceConversion(Object.class, ftype)) 517 return FT_UNCHECKED_REF; 518 else 519 return FT_CHECKED_REF; 520 } 521 522 /** 523 * Create a LF which can access the given field. 524 * Cache and share this structure among all fields with 525 * the same basicType and refKind. 526 */ 527 private static LambdaForm preparedFieldLambdaForm(MemberName m) { 528 Class<?> ftype = m.getFieldType(); 529 boolean isVolatile = m.isVolatile(); 530 byte formOp; 531 switch (m.getReferenceKind()) { 532 case REF_getField: formOp = AF_GETFIELD; break; 533 case REF_putField: formOp = AF_PUTFIELD; break; 534 case REF_getStatic: formOp = AF_GETSTATIC; break; 535 case REF_putStatic: formOp = AF_PUTSTATIC; break; 536 default: throw new InternalError(m.toString()); 537 } 538 if (shouldBeInitialized(m)) { 539 // precompute the barrier-free version: 540 preparedFieldLambdaForm(formOp, isVolatile, ftype); 541 assert((AF_GETSTATIC_INIT - AF_GETSTATIC) == 542 (AF_PUTSTATIC_INIT - AF_PUTSTATIC)); 543 formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC); 544 } 545 LambdaForm lform = preparedFieldLambdaForm(formOp, isVolatile, ftype); 546 maybeCompile(lform, m); 547 assert(lform.methodType().dropParameterTypes(0, 1) 548 .equals(m.getInvocationType().basicType())) 549 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType()); 550 return lform; 551 } 552 private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, Class<?> ftype) { 553 int afIndex = afIndex(formOp, isVolatile, ftypeKind(ftype)); 554 LambdaForm lform = ACCESSOR_FORMS[afIndex]; 555 if (lform != null) return lform; 556 lform = makePreparedFieldLambdaForm(formOp, isVolatile, ftypeKind(ftype)); 557 ACCESSOR_FORMS[afIndex] = lform; // don't bother with a CAS 558 return lform; 559 } 560 561 private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) { 562 boolean isGetter = (formOp & 1) == (AF_GETFIELD & 1); 563 boolean isStatic = (formOp >= AF_GETSTATIC); 564 boolean needsInit = (formOp >= AF_GETSTATIC_INIT); 565 boolean needsCast = (ftypeKind == FT_CHECKED_REF); 566 Wrapper fw = (needsCast ? Wrapper.OBJECT : Wrapper.values()[ftypeKind]); 567 Class<?> ft = fw.primitiveType(); 568 assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind); 569 String tname = fw.primitiveSimpleName(); 570 String ctname = Character.toUpperCase(tname.charAt(0)) + tname.substring(1); 571 if (isVolatile) ctname += "Volatile"; 572 String getOrPut = (isGetter ? "get" : "put"); 573 String linkerName = (getOrPut + ctname); // getObject, putIntVolatile, etc. 574 MethodType linkerType; 575 if (isGetter) 576 linkerType = MethodType.methodType(ft, Object.class, long.class); 577 else 578 linkerType = MethodType.methodType(void.class, Object.class, long.class, ft); 579 MemberName linker = new MemberName(Unsafe.class, linkerName, linkerType, REF_invokeVirtual); 580 try { 581 linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, NoSuchMethodException.class); 582 } catch (ReflectiveOperationException ex) { 583 throw newInternalError(ex); 584 } 585 586 // What is the external type of the lambda form? 587 MethodType mtype; 588 if (isGetter) 589 mtype = MethodType.methodType(ft); 590 else 591 mtype = MethodType.methodType(void.class, ft); 592 mtype = mtype.basicType(); // erase short to int, etc. 593 if (!isStatic) 594 mtype = mtype.insertParameterTypes(0, Object.class); 595 final int DMH_THIS = 0; 596 final int ARG_BASE = 1; 597 final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); 598 // if this is for non-static access, the base pointer is stored at this index: 599 final int OBJ_BASE = isStatic ? -1 : ARG_BASE; 600 // if this is for write access, the value to be written is stored at this index: 601 final int SET_VALUE = isGetter ? -1 : ARG_LIMIT - 1; 602 int nameCursor = ARG_LIMIT; 603 final int F_HOLDER = (isStatic ? nameCursor++ : -1); // static base if any 604 final int F_OFFSET = nameCursor++; // Either static offset or field offset. 605 final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1); 606 final int INIT_BAR = (needsInit ? nameCursor++ : -1); 607 final int PRE_CAST = (needsCast && !isGetter ? nameCursor++ : -1); 608 final int LINKER_CALL = nameCursor++; 609 final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1); 610 final int RESULT = nameCursor-1; // either the call or the cast 611 Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType()); 612 if (needsInit) 613 names[INIT_BAR] = new Name(Lazy.NF_ensureInitialized, names[DMH_THIS]); 614 if (needsCast && !isGetter) 615 names[PRE_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[SET_VALUE]); 616 Object[] outArgs = new Object[1 + linkerType.parameterCount()]; 617 assert(outArgs.length == (isGetter ? 3 : 4)); 618 outArgs[0] = UNSAFE; 619 if (isStatic) { 620 outArgs[1] = names[F_HOLDER] = new Name(Lazy.NF_staticBase, names[DMH_THIS]); 621 outArgs[2] = names[F_OFFSET] = new Name(Lazy.NF_staticOffset, names[DMH_THIS]); 622 } else { 623 outArgs[1] = names[OBJ_CHECK] = new Name(Lazy.NF_checkBase, names[OBJ_BASE]); 624 outArgs[2] = names[F_OFFSET] = new Name(Lazy.NF_fieldOffset, names[DMH_THIS]); 625 } 626 if (!isGetter) { 627 outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]); 628 } 629 for (Object a : outArgs) assert(a != null); 630 names[LINKER_CALL] = new Name(linker, outArgs); 631 if (needsCast && isGetter) 632 names[POST_CAST] = new Name(Lazy.NF_checkCast, names[DMH_THIS], names[LINKER_CALL]); 633 for (Name n : names) assert(n != null); 634 String fieldOrStatic = (isStatic ? "Static" : "Field"); 635 String lambdaName = (linkerName + fieldOrStatic); // significant only for debugging 636 if (needsCast) lambdaName += "Cast"; 637 if (needsInit) lambdaName += "Init"; 638 return new LambdaForm(lambdaName, ARG_LIMIT, names, RESULT); 639 } 640 641 /** 642 * Pre-initialized NamedFunctions for bootstrapping purposes. 643 * Factored in an inner class to delay initialization until first usage. 644 */ 645 private static class Lazy { 646 static final NamedFunction 647 NF_internalMemberName, 648 NF_internalMemberNameEnsureInit, 649 NF_ensureInitialized, 650 NF_fieldOffset, 651 NF_checkBase, 652 NF_staticBase, 653 NF_staticOffset, 654 NF_checkCast, 655 NF_allocateInstance, 656 NF_constructorMethod; 657 static { 658 try { 659 NamedFunction nfs[] = { 660 NF_internalMemberName = new NamedFunction(DirectMethodHandle.class 661 .getDeclaredMethod("internalMemberName", Object.class)), 662 NF_internalMemberNameEnsureInit = new NamedFunction(DirectMethodHandle.class 663 .getDeclaredMethod("internalMemberNameEnsureInit", Object.class)), 664 NF_ensureInitialized = new NamedFunction(DirectMethodHandle.class 665 .getDeclaredMethod("ensureInitialized", Object.class)), 666 NF_fieldOffset = new NamedFunction(DirectMethodHandle.class 667 .getDeclaredMethod("fieldOffset", Object.class)), 668 NF_checkBase = new NamedFunction(DirectMethodHandle.class 669 .getDeclaredMethod("checkBase", Object.class)), 670 NF_staticBase = new NamedFunction(DirectMethodHandle.class 671 .getDeclaredMethod("staticBase", Object.class)), 672 NF_staticOffset = new NamedFunction(DirectMethodHandle.class 673 .getDeclaredMethod("staticOffset", Object.class)), 674 NF_checkCast = new NamedFunction(DirectMethodHandle.class 675 .getDeclaredMethod("checkCast", Object.class, Object.class)), 676 NF_allocateInstance = new NamedFunction(DirectMethodHandle.class 677 .getDeclaredMethod("allocateInstance", Object.class)), 678 NF_constructorMethod = new NamedFunction(DirectMethodHandle.class 679 .getDeclaredMethod("constructorMethod", Object.class)) 680 }; 681 for (NamedFunction nf : nfs) { 682 // Each nf must be statically invocable or we get tied up in our bootstraps. 683 assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf.member)) : nf; 684 nf.resolve(); 685 } 686 } catch (ReflectiveOperationException ex) { 687 throw newInternalError(ex); 688 } 689 } 690 } 691 }