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