< prev index next >

src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java

Print this page

   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 jdk.internal.misc.Unsafe;
  29 import jdk.internal.vm.annotation.ForceInline;
  30 import jdk.internal.vm.annotation.Stable;
  31 import sun.invoke.util.ValueConversions;
  32 import sun.invoke.util.VerifyAccess;
  33 import sun.invoke.util.Wrapper;
  34 
  35 import java.util.Arrays;
  36 import java.util.Objects;
  37 import java.util.function.Function;
  38 
  39 import static java.lang.invoke.LambdaForm.*;
  40 import static java.lang.invoke.LambdaForm.Kind.*;
  41 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  42 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  43 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  44 import static java.lang.invoke.MethodTypeForm.*;
  45 
  46 /**
  47  * The flavor of method handle which implements a constant reference

  60         if (member.getDeclaringClass().isInterface() &&
  61             member.getReferenceKind() == REF_invokeInterface &&
  62             member.isMethod() && !member.isAbstract()) {
  63             // Check for corner case: invokeinterface of Object method
  64             MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind());
  65             m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null, LM_TRUSTED);
  66             if (m != null && m.isPublic()) {
  67                 assert(member.getReferenceKind() == m.getReferenceKind());  // else this.form is wrong
  68                 member = m;
  69             }
  70         }
  71 
  72         this.member = member;
  73         this.crackable = crackable;
  74     }
  75 
  76     // Factory methods:
  77     static DirectMethodHandle make(byte refKind, Class<?> refc, MemberName member, Class<?> callerClass) {
  78         MethodType mtype = member.getMethodOrFieldType();
  79         if (!member.isStatic()) {
  80             if (!member.getDeclaringClass().isAssignableFrom(refc) || member.isConstructor())
  81                 throw new InternalError(member.toString());
  82             mtype = mtype.insertParameterTypes(0, refc);

  83         }
  84         if (!member.isField()) {
  85             // refKind reflects the original type of lookup via findSpecial or
  86             // findVirtual etc.
  87             return switch (refKind) {
  88                 case REF_invokeSpecial -> {
  89                     member = member.asSpecial();
  90                     // if caller is an interface we need to adapt to get the
  91                     // receiver check inserted
  92                     if (callerClass == null) {
  93                         throw new InternalError("callerClass must not be null for REF_invokeSpecial");
  94                     }
  95                     LambdaForm lform = preparedLambdaForm(member, callerClass.isInterface());
  96                     yield new Special(mtype, lform, member, true, callerClass);
  97                 }
  98                 case REF_invokeInterface -> {
  99                     // for interfaces we always need the receiver typecheck,
 100                     // so we always pass 'true' to ensure we adapt if needed
 101                     // to include the REF_invokeSpecial case
 102                     LambdaForm lform = preparedLambdaForm(member, true);

 110         } else {
 111             LambdaForm lform = preparedFieldLambdaForm(member);
 112             if (member.isStatic()) {
 113                 long offset = MethodHandleNatives.staticFieldOffset(member);
 114                 Object base = MethodHandleNatives.staticFieldBase(member);
 115                 return new StaticAccessor(mtype, lform, member, true, base, offset);
 116             } else {
 117                 long offset = MethodHandleNatives.objectFieldOffset(member);
 118                 assert(offset == (int)offset);
 119                 return new Accessor(mtype, lform, member, true, (int)offset);
 120             }
 121         }
 122     }
 123     static DirectMethodHandle make(Class<?> refc, MemberName member) {
 124         byte refKind = member.getReferenceKind();
 125         if (refKind == REF_invokeSpecial)
 126             refKind =  REF_invokeVirtual;
 127         return make(refKind, refc, member, null /* no callerClass context */);
 128     }
 129     static DirectMethodHandle make(MemberName member) {
 130         if (member.isConstructor())
 131             return makeAllocator(member.getDeclaringClass(), member);
 132         return make(member.getDeclaringClass(), member);
 133     }
 134     static DirectMethodHandle makeAllocator(Class<?> instanceClass, MemberName ctor) {
 135         assert(ctor.isConstructor() && ctor.getName().equals("<init>"));
 136         ctor = ctor.asConstructor();
 137         assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
 138         MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
 139         LambdaForm lform = preparedLambdaForm(ctor);
 140         MemberName init = ctor.asSpecial();
 141         assert(init.getMethodType().returnType() == void.class);
 142         return new Constructor(mtype, lform, ctor, true, init, instanceClass);
 143     }
 144 
 145     @Override
 146     BoundMethodHandle rebind() {
 147         return BoundMethodHandle.makeReinvoker(this);
 148     }
 149 
 150     @Override
 151     MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 152         assert(this.getClass() == DirectMethodHandle.class);  // must override in subclasses
 153         return new DirectMethodHandle(mt, lf, member, crackable);
 154     }
 155 
 156     @Override
 157     MethodHandle viewAsType(MethodType newType, boolean strict) {

 531         // Note: We return a long because that is what Unsafe.getObject likes.
 532         // We store a plain int because it is more compact.
 533         return ((Accessor)accessorObj).fieldOffset;
 534     }
 535 
 536     @ForceInline
 537     /*non-public*/
 538     static Object checkBase(Object obj) {
 539         // Note that the object's class has already been verified,
 540         // since the parameter type of the Accessor method handle
 541         // is either member.getDeclaringClass or a subclass.
 542         // This was verified in DirectMethodHandle.make.
 543         // Therefore, the only remaining check is for null.
 544         // Since this check is *not* guaranteed by Unsafe.getInt
 545         // and its siblings, we need to make an explicit one here.
 546         return Objects.requireNonNull(obj);
 547     }
 548 
 549     /** This subclass handles static field references. */
 550     static final class StaticAccessor extends DirectMethodHandle {
 551         private final Class<?> fieldType;
 552         private final Object   staticBase;
 553         private final long     staticOffset;
 554 
 555         private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
 556                                boolean crackable, Object staticBase, long staticOffset) {
 557             super(mtype, form, member, crackable);
 558             this.fieldType    = member.getFieldType();
 559             this.staticBase   = staticBase;
 560             this.staticOffset = staticOffset;
 561         }
 562 
 563         @Override Object checkCast(Object obj) {
 564             return fieldType.cast(obj);
 565         }
 566         @Override
 567         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 568             return new StaticAccessor(mt, lf, member, crackable, staticBase, staticOffset);
 569         }
 570         @Override
 571         MethodHandle viewAsType(MethodType newType, boolean strict) {
 572             assert(viewAsTypeChecks(newType, strict));
 573             return new StaticAccessor(newType, form, member, false, staticBase, staticOffset);

 581     }
 582 
 583     @ForceInline
 584     /*non-public*/
 585     static Object staticBase(Object accessorObj) {
 586         return ((StaticAccessor)accessorObj).staticBase;
 587     }
 588 
 589     @ForceInline
 590     /*non-public*/
 591     static long staticOffset(Object accessorObj) {
 592         return ((StaticAccessor)accessorObj).staticOffset;
 593     }
 594 
 595     @ForceInline
 596     /*non-public*/
 597     static Object checkCast(Object mh, Object obj) {
 598         return ((DirectMethodHandle) mh).checkCast(obj);
 599     }
 600 















 601     Object checkCast(Object obj) {
 602         return member.getMethodType().returnType().cast(obj);
 603     }
 604 
 605     // Caching machinery for field accessors:
 606     static final byte
 607             AF_GETFIELD        = 0,
 608             AF_PUTFIELD        = 1,
 609             AF_GETSTATIC       = 2,
 610             AF_PUTSTATIC       = 3,
 611             AF_GETSTATIC_INIT  = 4,
 612             AF_PUTSTATIC_INIT  = 5,
 613             AF_LIMIT           = 6;
 614     // Enumerate the different field kinds using Wrapper,
 615     // with an extra case added for checked references.
 616     static final int
 617             FT_LAST_WRAPPER    = Wrapper.COUNT-1,
 618             FT_UNCHECKED_REF   = Wrapper.OBJECT.ordinal(),
 619             FT_CHECKED_REF     = FT_LAST_WRAPPER+1,
 620             FT_LIMIT           = FT_LAST_WRAPPER+2;
 621     private static int afIndex(byte formOp, boolean isVolatile, int ftypeKind) {

 622         return ((formOp * FT_LIMIT * 2)
 623                 + (isVolatile ? FT_LIMIT : 0)


 624                 + ftypeKind);
 625     }
 626     @Stable
 627     private static final LambdaForm[] ACCESSOR_FORMS
 628             = new LambdaForm[afIndex(AF_LIMIT, false, 0)];
 629     static int ftypeKind(Class<?> ftype) {
 630         if (ftype.isPrimitive()) {
 631             return Wrapper.forPrimitiveType(ftype).ordinal();
 632         } else if (ftype.isInterface() || ftype.isAssignableFrom(Object.class)) {
 633             // retyping can be done without a cast
 634             return FT_UNCHECKED_REF;
 635         } else {
 636             return FT_CHECKED_REF;

 637         }
 638     }
 639 
 640     /**
 641      * Create a LF which can access the given field.
 642      * Cache and share this structure among all fields with
 643      * the same basicType and refKind.
 644      */
 645     private static LambdaForm preparedFieldLambdaForm(MemberName m) {
 646         Class<?> ftype = m.getFieldType();
 647         boolean isVolatile = m.isVolatile();
 648         byte formOp = switch (m.getReferenceKind()) {
 649             case REF_getField  -> AF_GETFIELD;
 650             case REF_putField  -> AF_PUTFIELD;
 651             case REF_getStatic -> AF_GETSTATIC;
 652             case REF_putStatic -> AF_PUTSTATIC;
 653             default -> throw new InternalError(m.toString());
 654         };
 655         if (shouldBeInitialized(m)) {
 656             // precompute the barrier-free version:
 657             preparedFieldLambdaForm(formOp, isVolatile, ftype);
 658             assert((AF_GETSTATIC_INIT - AF_GETSTATIC) ==
 659                    (AF_PUTSTATIC_INIT - AF_PUTSTATIC));
 660             formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC);
 661         }
 662         LambdaForm lform = preparedFieldLambdaForm(formOp, isVolatile, ftype);
 663         maybeCompile(lform, m);
 664         assert(lform.methodType().dropParameterTypes(0, 1)
 665                 .equals(m.getInvocationType().basicType()))
 666                 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType());
 667         return lform;
 668     }
 669     private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile, Class<?> ftype) {
 670         int ftypeKind = ftypeKind(ftype);
 671         int afIndex = afIndex(formOp, isVolatile, ftypeKind);


 672         LambdaForm lform = ACCESSOR_FORMS[afIndex];
 673         if (lform != null)  return lform;
 674         lform = makePreparedFieldLambdaForm(formOp, isVolatile, ftypeKind);
 675         ACCESSOR_FORMS[afIndex] = lform;  // don't bother with a CAS
 676         return lform;
 677     }
 678 
 679     private static final Wrapper[] ALL_WRAPPERS = Wrapper.values();
 680 
 681     private static Kind getFieldKind(boolean isGetter, boolean isVolatile, Wrapper wrapper) {


 682         if (isGetter) {
 683             if (isVolatile) {
 684                 switch (wrapper) {
 685                     case BOOLEAN: return GET_BOOLEAN_VOLATILE;
 686                     case BYTE:    return GET_BYTE_VOLATILE;
 687                     case SHORT:   return GET_SHORT_VOLATILE;
 688                     case CHAR:    return GET_CHAR_VOLATILE;
 689                     case INT:     return GET_INT_VOLATILE;
 690                     case LONG:    return GET_LONG_VOLATILE;
 691                     case FLOAT:   return GET_FLOAT_VOLATILE;
 692                     case DOUBLE:  return GET_DOUBLE_VOLATILE;
 693                     case OBJECT:  return GET_REFERENCE_VOLATILE;
 694                 }
 695             } else {
 696                 switch (wrapper) {
 697                     case BOOLEAN: return GET_BOOLEAN;
 698                     case BYTE:    return GET_BYTE;
 699                     case SHORT:   return GET_SHORT;
 700                     case CHAR:    return GET_CHAR;
 701                     case INT:     return GET_INT;
 702                     case LONG:    return GET_LONG;
 703                     case FLOAT:   return GET_FLOAT;
 704                     case DOUBLE:  return GET_DOUBLE;
 705                     case OBJECT:  return GET_REFERENCE;
 706                 }
 707             }
 708         } else {
 709             if (isVolatile) {
 710                 switch (wrapper) {
 711                     case BOOLEAN: return PUT_BOOLEAN_VOLATILE;
 712                     case BYTE:    return PUT_BYTE_VOLATILE;
 713                     case SHORT:   return PUT_SHORT_VOLATILE;
 714                     case CHAR:    return PUT_CHAR_VOLATILE;
 715                     case INT:     return PUT_INT_VOLATILE;
 716                     case LONG:    return PUT_LONG_VOLATILE;
 717                     case FLOAT:   return PUT_FLOAT_VOLATILE;
 718                     case DOUBLE:  return PUT_DOUBLE_VOLATILE;
 719                     case OBJECT:  return PUT_REFERENCE_VOLATILE;
 720                 }
 721             } else {
 722                 switch (wrapper) {
 723                     case BOOLEAN: return PUT_BOOLEAN;
 724                     case BYTE:    return PUT_BYTE;
 725                     case SHORT:   return PUT_SHORT;
 726                     case CHAR:    return PUT_CHAR;
 727                     case INT:     return PUT_INT;
 728                     case LONG:    return PUT_LONG;
 729                     case FLOAT:   return PUT_FLOAT;
 730                     case DOUBLE:  return PUT_DOUBLE;
 731                     case OBJECT:  return PUT_REFERENCE;
 732                 }
 733             }
 734         }
 735         throw new AssertionError("Invalid arguments");
 736     }
 737 
 738     static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftypeKind) {







 739         boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
 740         boolean isStatic  = (formOp >= AF_GETSTATIC);
 741         boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
 742         boolean needsCast = (ftypeKind == FT_CHECKED_REF);

 743         Wrapper fw = (needsCast ? Wrapper.OBJECT : ALL_WRAPPERS[ftypeKind]);
 744         Class<?> ft = fw.primitiveType();
 745         assert(ftypeKind(needsCast ? String.class : ft) == ftypeKind);
 746 
 747         // getObject, putIntVolatile, etc.
 748         Kind kind = getFieldKind(isGetter, isVolatile, fw);
 749 
 750         MethodType linkerType;
 751         if (isGetter)
 752             linkerType = MethodType.methodType(ft, Object.class, long.class);
 753         else
 754             linkerType = MethodType.methodType(void.class, Object.class, long.class, ft);




 755         MemberName linker = new MemberName(Unsafe.class, kind.methodName, linkerType, REF_invokeVirtual);
 756         try {
 757             linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, LM_TRUSTED,
 758                                               NoSuchMethodException.class);
 759         } catch (ReflectiveOperationException ex) {
 760             throw newInternalError(ex);
 761         }
 762 
 763         // What is the external type of the lambda form?
 764         MethodType mtype;
 765         if (isGetter)
 766             mtype = MethodType.methodType(ft);
 767         else
 768             mtype = MethodType.methodType(void.class, ft);
 769         mtype = mtype.basicType();  // erase short to int, etc.
 770         if (!isStatic)
 771             mtype = mtype.insertParameterTypes(0, Object.class);
 772         final int DMH_THIS  = 0;
 773         final int ARG_BASE  = 1;
 774         final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
 775         // if this is for non-static access, the base pointer is stored at this index:
 776         final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
 777         // if this is for write access, the value to be written is stored at this index:
 778         final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
 779         int nameCursor = ARG_LIMIT;
 780         final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
 781         final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
 782         final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
 783         final int U_HOLDER  = nameCursor++;  // UNSAFE holder
 784         final int INIT_BAR  = (needsInit ? nameCursor++ : -1);


 785         final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
 786         final int LINKER_CALL = nameCursor++;


 787         final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
 788         final int RESULT    = nameCursor-1;  // either the call or the cast
 789         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
 790         if (needsInit)
 791             names[INIT_BAR] = new Name(getFunction(NF_ensureInitialized), names[DMH_THIS]);
 792         if (needsCast && !isGetter)
 793             names[PRE_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[SET_VALUE]);




 794         Object[] outArgs = new Object[1 + linkerType.parameterCount()];
 795         assert(outArgs.length == (isGetter ? 3 : 4));
 796         outArgs[0] = names[U_HOLDER] = new Name(getFunction(NF_UNSAFE));
 797         if (isStatic) {
 798             outArgs[1] = names[F_HOLDER]  = new Name(getFunction(NF_staticBase), names[DMH_THIS]);
 799             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_staticOffset), names[DMH_THIS]);
 800         } else {
 801             outArgs[1] = names[OBJ_CHECK] = new Name(getFunction(NF_checkBase), names[OBJ_BASE]);
 802             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_fieldOffset), names[DMH_THIS]);
 803         }





 804         if (!isGetter) {
 805             outArgs[3] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
 806         }
 807         for (Object a : outArgs)  assert(a != null);
 808         names[LINKER_CALL] = new Name(linker, outArgs);
 809         if (needsCast && isGetter)
 810             names[POST_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[LINKER_CALL]);









 811         for (Name n : names)  assert(n != null);
 812 
 813         LambdaForm form;
 814         if (needsCast || needsInit) {
 815             // can't use the pre-generated form when casting and/or initializing
 816             form = LambdaForm.create(ARG_LIMIT, names, RESULT);
 817         } else {
 818             form = LambdaForm.create(ARG_LIMIT, names, RESULT, kind);
 819         }
 820 
 821         if (LambdaForm.debugNames()) {
 822             // add some detail to the lambdaForm debugname,
 823             // significant only for debugging
 824             StringBuilder nameBuilder = new StringBuilder(kind.methodName);
 825             if (isStatic) {
 826                 nameBuilder.append("Static");
 827             } else {
 828                 nameBuilder.append("Field");
 829             }
 830             if (needsCast) {

 836             LambdaForm.associateWithDebugName(form, nameBuilder.toString());
 837         }
 838         return form;
 839     }
 840 
 841     /**
 842      * Pre-initialized NamedFunctions for bootstrapping purposes.
 843      */
 844     static final byte NF_internalMemberName = 0,
 845             NF_internalMemberNameEnsureInit = 1,
 846             NF_ensureInitialized = 2,
 847             NF_fieldOffset = 3,
 848             NF_checkBase = 4,
 849             NF_staticBase = 5,
 850             NF_staticOffset = 6,
 851             NF_checkCast = 7,
 852             NF_allocateInstance = 8,
 853             NF_constructorMethod = 9,
 854             NF_UNSAFE = 10,
 855             NF_checkReceiver = 11,
 856             NF_LIMIT = 12;




 857 
 858     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
 859 
 860     private static NamedFunction getFunction(byte func) {
 861         NamedFunction nf = NFS[func];
 862         if (nf != null) {
 863             return nf;
 864         }
 865         // Each nf must be statically invocable or we get tied up in our bootstraps.
 866         nf = NFS[func] = createFunction(func);
 867         assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf));
 868         return nf;
 869     }
 870 


 871     private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class);
 872 
 873     private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class);
 874 
 875     private static NamedFunction createFunction(byte func) {
 876         try {
 877             switch (func) {
 878                 case NF_internalMemberName:
 879                     return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE);
 880                 case NF_internalMemberNameEnsureInit:
 881                     return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE);
 882                 case NF_ensureInitialized:
 883                     return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class));
 884                 case NF_fieldOffset:
 885                     return getNamedFunction("fieldOffset", LONG_OBJ_TYPE);
 886                 case NF_checkBase:
 887                     return getNamedFunction("checkBase", OBJ_OBJ_TYPE);
 888                 case NF_staticBase:
 889                     return getNamedFunction("staticBase", OBJ_OBJ_TYPE);
 890                 case NF_staticOffset:
 891                     return getNamedFunction("staticOffset", LONG_OBJ_TYPE);
 892                 case NF_checkCast:
 893                     return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class));
 894                 case NF_allocateInstance:
 895                     return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE);
 896                 case NF_constructorMethod:
 897                     return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE);
 898                 case NF_UNSAFE:
 899                     MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getStatic);
 900                     return new NamedFunction(
 901                             MemberName.getFactory().resolveOrFail(REF_getStatic, member,
 902                                                                   DirectMethodHandle.class, LM_TRUSTED,
 903                                                                   NoSuchFieldException.class));
 904                 case NF_checkReceiver:
 905                     member = new MemberName(DirectMethodHandle.class, "checkReceiver", OBJ_OBJ_TYPE, REF_invokeVirtual);
 906                     return new NamedFunction(
 907                             MemberName.getFactory().resolveOrFail(REF_invokeVirtual, member,
 908                                                                   DirectMethodHandle.class, LM_TRUSTED,
 909                                                                   NoSuchMethodException.class));








 910                 default:
 911                     throw newInternalError("Unknown function: " + func);
 912             }
 913         } catch (ReflectiveOperationException ex) {
 914             throw newInternalError(ex);
 915         }
 916     }
 917 
 918     private static NamedFunction getNamedFunction(String name, MethodType type)
 919         throws ReflectiveOperationException
 920     {
 921         MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic);
 922         return new NamedFunction(
 923                 MemberName.getFactory().resolveOrFail(REF_invokeStatic, member,
 924                                                       DirectMethodHandle.class, LM_TRUSTED,
 925                                                       NoSuchMethodException.class));
 926     }
 927 
 928     static {
 929         // The Holder class will contain pre-generated DirectMethodHandles resolved

   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 jdk.internal.value.PrimitiveClass;
  29 import jdk.internal.misc.Unsafe;
  30 import jdk.internal.vm.annotation.ForceInline;
  31 import jdk.internal.vm.annotation.Stable;
  32 import sun.invoke.util.ValueConversions;
  33 import sun.invoke.util.VerifyAccess;
  34 import sun.invoke.util.Wrapper;
  35 
  36 import java.util.Arrays;
  37 import java.util.Objects;
  38 import java.util.function.Function;
  39 
  40 import static java.lang.invoke.LambdaForm.*;
  41 import static java.lang.invoke.LambdaForm.Kind.*;
  42 import static java.lang.invoke.MethodHandleNatives.Constants.*;
  43 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  44 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  45 import static java.lang.invoke.MethodTypeForm.*;
  46 
  47 /**
  48  * The flavor of method handle which implements a constant reference

  61         if (member.getDeclaringClass().isInterface() &&
  62             member.getReferenceKind() == REF_invokeInterface &&
  63             member.isMethod() && !member.isAbstract()) {
  64             // Check for corner case: invokeinterface of Object method
  65             MemberName m = new MemberName(Object.class, member.getName(), member.getMethodType(), member.getReferenceKind());
  66             m = MemberName.getFactory().resolveOrNull(m.getReferenceKind(), m, null, LM_TRUSTED);
  67             if (m != null && m.isPublic()) {
  68                 assert(member.getReferenceKind() == m.getReferenceKind());  // else this.form is wrong
  69                 member = m;
  70             }
  71         }
  72 
  73         this.member = member;
  74         this.crackable = crackable;
  75     }
  76 
  77     // Factory methods:
  78     static DirectMethodHandle make(byte refKind, Class<?> refc, MemberName member, Class<?> callerClass) {
  79         MethodType mtype = member.getMethodOrFieldType();
  80         if (!member.isStatic()) {
  81             if (!member.getDeclaringClass().isAssignableFrom(refc) || member.isObjectConstructor())
  82                 throw new InternalError(member.toString());
  83             Class<?> receiverType = PrimitiveClass.isPrimitiveClass(refc) ? PrimitiveClass.asValueType(refc) : refc;
  84             mtype = mtype.insertParameterTypes(0, receiverType);
  85         }
  86         if (!member.isField()) {
  87             // refKind reflects the original type of lookup via findSpecial or
  88             // findVirtual etc.
  89             return switch (refKind) {
  90                 case REF_invokeSpecial -> {
  91                     member = member.asSpecial();
  92                     // if caller is an interface we need to adapt to get the
  93                     // receiver check inserted
  94                     if (callerClass == null) {
  95                         throw new InternalError("callerClass must not be null for REF_invokeSpecial");
  96                     }
  97                     LambdaForm lform = preparedLambdaForm(member, callerClass.isInterface());
  98                     yield new Special(mtype, lform, member, true, callerClass);
  99                 }
 100                 case REF_invokeInterface -> {
 101                     // for interfaces we always need the receiver typecheck,
 102                     // so we always pass 'true' to ensure we adapt if needed
 103                     // to include the REF_invokeSpecial case
 104                     LambdaForm lform = preparedLambdaForm(member, true);

 112         } else {
 113             LambdaForm lform = preparedFieldLambdaForm(member);
 114             if (member.isStatic()) {
 115                 long offset = MethodHandleNatives.staticFieldOffset(member);
 116                 Object base = MethodHandleNatives.staticFieldBase(member);
 117                 return new StaticAccessor(mtype, lform, member, true, base, offset);
 118             } else {
 119                 long offset = MethodHandleNatives.objectFieldOffset(member);
 120                 assert(offset == (int)offset);
 121                 return new Accessor(mtype, lform, member, true, (int)offset);
 122             }
 123         }
 124     }
 125     static DirectMethodHandle make(Class<?> refc, MemberName member) {
 126         byte refKind = member.getReferenceKind();
 127         if (refKind == REF_invokeSpecial)
 128             refKind =  REF_invokeVirtual;
 129         return make(refKind, refc, member, null /* no callerClass context */);
 130     }
 131     static DirectMethodHandle make(MemberName member) {
 132         if (member.isObjectConstructor() && member.getMethodType().returnType() == void.class)
 133             return makeAllocator(member.getDeclaringClass(), member);
 134         return make(member.getDeclaringClass(), member);
 135     }
 136     static DirectMethodHandle makeAllocator(Class<?> instanceClass, MemberName ctor) {
 137         assert(ctor.isObjectConstructor() && !ctor.getDeclaringClass().isValue()) : ctor;
 138         ctor = ctor.asObjectConstructor();
 139         assert(ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
 140         MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
 141         LambdaForm lform = preparedLambdaForm(ctor);
 142         MemberName init = ctor.asSpecial();
 143         assert(init.getMethodType().returnType() == void.class);
 144         return new Constructor(mtype, lform, ctor, true, init, instanceClass);
 145     }
 146 
 147     @Override
 148     BoundMethodHandle rebind() {
 149         return BoundMethodHandle.makeReinvoker(this);
 150     }
 151 
 152     @Override
 153     MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 154         assert(this.getClass() == DirectMethodHandle.class);  // must override in subclasses
 155         return new DirectMethodHandle(mt, lf, member, crackable);
 156     }
 157 
 158     @Override
 159     MethodHandle viewAsType(MethodType newType, boolean strict) {

 533         // Note: We return a long because that is what Unsafe.getObject likes.
 534         // We store a plain int because it is more compact.
 535         return ((Accessor)accessorObj).fieldOffset;
 536     }
 537 
 538     @ForceInline
 539     /*non-public*/
 540     static Object checkBase(Object obj) {
 541         // Note that the object's class has already been verified,
 542         // since the parameter type of the Accessor method handle
 543         // is either member.getDeclaringClass or a subclass.
 544         // This was verified in DirectMethodHandle.make.
 545         // Therefore, the only remaining check is for null.
 546         // Since this check is *not* guaranteed by Unsafe.getInt
 547         // and its siblings, we need to make an explicit one here.
 548         return Objects.requireNonNull(obj);
 549     }
 550 
 551     /** This subclass handles static field references. */
 552     static final class StaticAccessor extends DirectMethodHandle {
 553         final Class<?> fieldType;
 554         final Object staticBase;
 555         final long staticOffset;
 556 
 557         private StaticAccessor(MethodType mtype, LambdaForm form, MemberName member,
 558                                boolean crackable, Object staticBase, long staticOffset) {
 559             super(mtype, form, member, crackable);
 560             this.fieldType    = member.getFieldType();
 561             this.staticBase   = staticBase;
 562             this.staticOffset = staticOffset;
 563         }
 564 
 565         @Override Object checkCast(Object obj) {
 566             return fieldType.cast(obj);
 567         }
 568         @Override
 569         MethodHandle copyWith(MethodType mt, LambdaForm lf) {
 570             return new StaticAccessor(mt, lf, member, crackable, staticBase, staticOffset);
 571         }
 572         @Override
 573         MethodHandle viewAsType(MethodType newType, boolean strict) {
 574             assert(viewAsTypeChecks(newType, strict));
 575             return new StaticAccessor(newType, form, member, false, staticBase, staticOffset);

 583     }
 584 
 585     @ForceInline
 586     /*non-public*/
 587     static Object staticBase(Object accessorObj) {
 588         return ((StaticAccessor)accessorObj).staticBase;
 589     }
 590 
 591     @ForceInline
 592     /*non-public*/
 593     static long staticOffset(Object accessorObj) {
 594         return ((StaticAccessor)accessorObj).staticOffset;
 595     }
 596 
 597     @ForceInline
 598     /*non-public*/
 599     static Object checkCast(Object mh, Object obj) {
 600         return ((DirectMethodHandle) mh).checkCast(obj);
 601     }
 602 
 603     @ForceInline
 604     /*non-public*/ static Class<?> fieldType(Object accessorObj) {
 605         return ((Accessor) accessorObj).fieldType;
 606     }
 607 
 608     @ForceInline
 609     /*non-public*/ static Class<?> staticFieldType(Object accessorObj) {
 610         return ((StaticAccessor) accessorObj).fieldType;
 611     }
 612 
 613     @ForceInline
 614     /*non-public*/ static Object zeroInstanceIfNull(Class<?> fieldType, Object obj) {
 615         return obj != null ? obj : UNSAFE.uninitializedDefaultValue(fieldType);
 616     }
 617 
 618     Object checkCast(Object obj) {
 619         return member.getMethodType().returnType().cast(obj);
 620     }
 621 
 622     // Caching machinery for field accessors:
 623     static final byte
 624             AF_GETFIELD        = 0,
 625             AF_PUTFIELD        = 1,
 626             AF_GETSTATIC       = 2,
 627             AF_PUTSTATIC       = 3,
 628             AF_GETSTATIC_INIT  = 4,
 629             AF_PUTSTATIC_INIT  = 5,
 630             AF_LIMIT           = 6;
 631     // Enumerate the different field kinds using Wrapper,
 632     // with an extra case added for checked references and value field access
 633     static final int
 634             FT_LAST_WRAPPER     = Wrapper.COUNT-1,
 635             FT_UNCHECKED_REF    = Wrapper.OBJECT.ordinal(),
 636             FT_CHECKED_REF      = FT_LAST_WRAPPER+1,
 637             FT_CHECKED_VALUE    = FT_LAST_WRAPPER+2,  // flattened and non-flattened and null-restricted
 638             FT_LIMIT            = FT_LAST_WRAPPER+6;
 639     private static int afIndex(byte formOp, boolean isVolatile, boolean isFlatValue, boolean isNullRestricted, int ftypeKind) {
 640         return ((formOp * FT_LIMIT * 2)
 641                 + (isVolatile ? FT_LIMIT : 0)
 642                 + (isFlatValue ? 1 : 0)
 643                 + (isNullRestricted ? 1 : 0)
 644                 + ftypeKind);
 645     }
 646     @Stable
 647     private static final LambdaForm[] ACCESSOR_FORMS
 648             = new LambdaForm[afIndex(AF_LIMIT, false, false, false, 0)];
 649     static int ftypeKind(Class<?> ftype, boolean isValue) {
 650         if (ftype.isPrimitive()) {
 651             return Wrapper.forPrimitiveType(ftype).ordinal();
 652         } else if (ftype.isInterface() || ftype.isAssignableFrom(Object.class)) {
 653             // retyping can be done without a cast
 654             return FT_UNCHECKED_REF;
 655         } else {
 656             // null check for value type in addition to check cast
 657             return isValue ? FT_CHECKED_VALUE : FT_CHECKED_REF;
 658         }
 659     }
 660 
 661     /**
 662      * Create a LF which can access the given field.
 663      * Cache and share this structure among all fields with
 664      * the same basicType and refKind.
 665      */
 666     private static LambdaForm preparedFieldLambdaForm(MemberName m) {
 667         Class<?> ftype = m.getFieldType();

 668         byte formOp = switch (m.getReferenceKind()) {
 669             case REF_getField  -> AF_GETFIELD;
 670             case REF_putField  -> AF_PUTFIELD;
 671             case REF_getStatic -> AF_GETSTATIC;
 672             case REF_putStatic -> AF_PUTSTATIC;
 673             default -> throw new InternalError(m.toString());
 674         };
 675         if (shouldBeInitialized(m)) {
 676             // precompute the barrier-free version:
 677             preparedFieldLambdaForm(formOp, m.isVolatile(), m.isInlineableField(), m.isFlat(), m.isNullRestricted(), ftype);
 678             assert((AF_GETSTATIC_INIT - AF_GETSTATIC) ==
 679                    (AF_PUTSTATIC_INIT - AF_PUTSTATIC));
 680             formOp += (AF_GETSTATIC_INIT - AF_GETSTATIC);
 681         }
 682         LambdaForm lform = preparedFieldLambdaForm(formOp, m.isVolatile(), m.isInlineableField(), m.isFlat(), m.isNullRestricted(), ftype);
 683         maybeCompile(lform, m);
 684         assert(lform.methodType().dropParameterTypes(0, 1)
 685                 .equals(m.getInvocationType().basicType()))
 686                 : Arrays.asList(m, m.getInvocationType().basicType(), lform, lform.methodType());
 687         return lform;
 688     }
 689 
 690     private static LambdaForm preparedFieldLambdaForm(byte formOp, boolean isVolatile,
 691                                                       boolean isValue, boolean isFlat, boolean isNullRestricted, Class<?> ftype) {
 692         int ftypeKind = ftypeKind(ftype, isValue);
 693         int afIndex = afIndex(formOp, isVolatile, isFlat, isNullRestricted, ftypeKind);
 694         LambdaForm lform = ACCESSOR_FORMS[afIndex];
 695         if (lform != null)  return lform;
 696         lform = makePreparedFieldLambdaForm(formOp, isVolatile, isValue, isFlat, isNullRestricted, ftypeKind);
 697         ACCESSOR_FORMS[afIndex] = lform;  // don't bother with a CAS
 698         return lform;
 699     }
 700 
 701     private static final Wrapper[] ALL_WRAPPERS = Wrapper.values();
 702 
 703     private static Kind getFieldKind(boolean isGetter, boolean isVolatile,
 704                                      boolean isFlatValue, boolean isNullRestricted,
 705                                      Wrapper wrapper) {
 706         if (isGetter) {
 707             if (isVolatile) {
 708                 switch (wrapper) {
 709                     case BOOLEAN: return GET_BOOLEAN_VOLATILE;
 710                     case BYTE:    return GET_BYTE_VOLATILE;
 711                     case SHORT:   return GET_SHORT_VOLATILE;
 712                     case CHAR:    return GET_CHAR_VOLATILE;
 713                     case INT:     return GET_INT_VOLATILE;
 714                     case LONG:    return GET_LONG_VOLATILE;
 715                     case FLOAT:   return GET_FLOAT_VOLATILE;
 716                     case DOUBLE:  return GET_DOUBLE_VOLATILE;
 717                     case OBJECT:  return isFlatValue ? GET_VALUE_VOLATILE : GET_REFERENCE_VOLATILE;
 718                 }
 719             } else {
 720                 switch (wrapper) {
 721                     case BOOLEAN: return GET_BOOLEAN;
 722                     case BYTE:    return GET_BYTE;
 723                     case SHORT:   return GET_SHORT;
 724                     case CHAR:    return GET_CHAR;
 725                     case INT:     return GET_INT;
 726                     case LONG:    return GET_LONG;
 727                     case FLOAT:   return GET_FLOAT;
 728                     case DOUBLE:  return GET_DOUBLE;
 729                     case OBJECT:  return isFlatValue ? GET_VALUE : GET_REFERENCE;
 730                 }
 731             }
 732         } else {
 733             if (isVolatile) {
 734                 switch (wrapper) {
 735                     case BOOLEAN: return PUT_BOOLEAN_VOLATILE;
 736                     case BYTE:    return PUT_BYTE_VOLATILE;
 737                     case SHORT:   return PUT_SHORT_VOLATILE;
 738                     case CHAR:    return PUT_CHAR_VOLATILE;
 739                     case INT:     return PUT_INT_VOLATILE;
 740                     case LONG:    return PUT_LONG_VOLATILE;
 741                     case FLOAT:   return PUT_FLOAT_VOLATILE;
 742                     case DOUBLE:  return PUT_DOUBLE_VOLATILE;
 743                     case OBJECT:  return isFlatValue ? PUT_VALUE_VOLATILE : PUT_REFERENCE_VOLATILE;
 744                 }
 745             } else {
 746                 switch (wrapper) {
 747                     case BOOLEAN: return PUT_BOOLEAN;
 748                     case BYTE:    return PUT_BYTE;
 749                     case SHORT:   return PUT_SHORT;
 750                     case CHAR:    return PUT_CHAR;
 751                     case INT:     return PUT_INT;
 752                     case LONG:    return PUT_LONG;
 753                     case FLOAT:   return PUT_FLOAT;
 754                     case DOUBLE:  return PUT_DOUBLE;
 755                     case OBJECT:  return isFlatValue ? PUT_VALUE : PUT_REFERENCE;
 756                 }
 757             }
 758         }
 759         throw new AssertionError("Invalid arguments");
 760     }
 761 
 762     /** invoked by GenerateJLIClassesHelper */
 763     static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile, int ftype) {
 764         return makePreparedFieldLambdaForm(formOp, isVolatile, false, false, false, ftype);
 765     }
 766 
 767     private static LambdaForm makePreparedFieldLambdaForm(byte formOp, boolean isVolatile,
 768                                                           boolean isValue, boolean isFlat,
 769                                                           boolean isNullRestricted, int ftypeKind) {
 770         boolean isGetter  = (formOp & 1) == (AF_GETFIELD & 1);
 771         boolean isStatic  = (formOp >= AF_GETSTATIC);
 772         boolean needsInit = (formOp >= AF_GETSTATIC_INIT);
 773         boolean needsCast = (ftypeKind == FT_CHECKED_REF || ftypeKind == FT_CHECKED_VALUE);
 774         boolean needsZeroInstance = isNullRestricted && isValue && !isFlat;
 775         Wrapper fw = (needsCast ? Wrapper.OBJECT : ALL_WRAPPERS[ftypeKind]);
 776         Class<?> ft = fw.primitiveType();
 777         assert(needsCast ? true : ftypeKind(ft, isValue) == ftypeKind);
 778 
 779         // getObject, putIntVolatile, etc.
 780         Kind kind = getFieldKind(isGetter, isVolatile, isFlat, isNullRestricted, fw);
 781 
 782         MethodType linkerType;
 783         boolean hasValueTypeArg = isGetter ? isValue : isFlat;
 784         if (isGetter) {
 785             linkerType = isValue ? MethodType.methodType(ft, Object.class, long.class, Class.class)
 786                                  : MethodType.methodType(ft, Object.class, long.class);
 787         } else {
 788             linkerType = isFlat ? MethodType.methodType(void.class, Object.class, long.class, Class.class, ft)
 789                                 : MethodType.methodType(void.class, Object.class, long.class, ft);
 790         }
 791         MemberName linker = new MemberName(Unsafe.class, kind.methodName, linkerType, REF_invokeVirtual);
 792         try {
 793             linker = IMPL_NAMES.resolveOrFail(REF_invokeVirtual, linker, null, LM_TRUSTED,
 794                                               NoSuchMethodException.class);
 795         } catch (ReflectiveOperationException ex) {
 796             throw newInternalError(ex);
 797         }
 798 
 799         // What is the external type of the lambda form?
 800         MethodType mtype;
 801         if (isGetter)
 802             mtype = MethodType.methodType(ft);
 803         else
 804             mtype = MethodType.methodType(void.class, ft);
 805         mtype = mtype.basicType();  // erase short to int, etc.
 806         if (!isStatic)
 807             mtype = mtype.insertParameterTypes(0, Object.class);
 808         final int DMH_THIS  = 0;
 809         final int ARG_BASE  = 1;
 810         final int ARG_LIMIT = ARG_BASE + mtype.parameterCount();
 811         // if this is for non-static access, the base pointer is stored at this index:
 812         final int OBJ_BASE  = isStatic ? -1 : ARG_BASE;
 813         // if this is for write access, the value to be written is stored at this index:
 814         final int SET_VALUE  = isGetter ? -1 : ARG_LIMIT - 1;
 815         int nameCursor = ARG_LIMIT;
 816         final int F_HOLDER  = (isStatic ? nameCursor++ : -1);  // static base if any
 817         final int F_OFFSET  = nameCursor++;  // Either static offset or field offset.
 818         final int OBJ_CHECK = (OBJ_BASE >= 0 ? nameCursor++ : -1);
 819         final int U_HOLDER  = nameCursor++;  // UNSAFE holder
 820         final int INIT_BAR  = (needsInit ? nameCursor++ : -1);
 821         final int VALUE_TYPE = (hasValueTypeArg ? nameCursor++ : -1);
 822         final int NULL_CHECK  = (isNullRestricted && !isGetter ? nameCursor++ : -1);
 823         final int PRE_CAST  = (needsCast && !isGetter ? nameCursor++ : -1);
 824         final int LINKER_CALL = nameCursor++;
 825         final int FIELD_TYPE = (needsZeroInstance && isGetter ? nameCursor++ : -1);
 826         final int ZERO_INSTANCE = (needsZeroInstance && isGetter ? nameCursor++ : -1);
 827         final int POST_CAST = (needsCast && isGetter ? nameCursor++ : -1);
 828         final int RESULT    = nameCursor-1;  // either the call, zero instance, or the cast
 829         Name[] names = arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
 830         if (needsInit)
 831             names[INIT_BAR] = new Name(getFunction(NF_ensureInitialized), names[DMH_THIS]);
 832         if (!isGetter) {
 833             if (isNullRestricted)
 834                 names[NULL_CHECK] = new Name(getFunction(NF_nullCheck), names[SET_VALUE]);
 835             if (needsCast)
 836                 names[PRE_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[SET_VALUE]);
 837         }
 838         Object[] outArgs = new Object[1 + linkerType.parameterCount()];
 839         assert (outArgs.length == (isGetter ? 3 : 4) + (hasValueTypeArg ? 1 : 0));
 840         outArgs[0] = names[U_HOLDER] = new Name(getFunction(NF_UNSAFE));
 841         if (isStatic) {
 842             outArgs[1] = names[F_HOLDER]  = new Name(getFunction(NF_staticBase), names[DMH_THIS]);
 843             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_staticOffset), names[DMH_THIS]);
 844         } else {
 845             outArgs[1] = names[OBJ_CHECK] = new Name(getFunction(NF_checkBase), names[OBJ_BASE]);
 846             outArgs[2] = names[F_OFFSET]  = new Name(getFunction(NF_fieldOffset), names[DMH_THIS]);
 847         }
 848         int x = 3;
 849         if (hasValueTypeArg) {
 850             outArgs[x++] = names[VALUE_TYPE] = isStatic ? new Name(getFunction(NF_staticFieldType), names[DMH_THIS])
 851                                                         : new Name(getFunction(NF_fieldType), names[DMH_THIS]);
 852         }
 853         if (!isGetter) {
 854             outArgs[x] = (needsCast ? names[PRE_CAST] : names[SET_VALUE]);
 855         }
 856         for (Object a : outArgs)  assert(a != null);
 857         names[LINKER_CALL] = new Name(linker, outArgs);
 858         if (isGetter) {
 859             int argIndex = LINKER_CALL;
 860             if (needsZeroInstance) {
 861                 names[FIELD_TYPE] = isStatic ? new Name(getFunction(NF_staticFieldType), names[DMH_THIS])
 862                                              : new Name(getFunction(NF_fieldType), names[DMH_THIS]);
 863                 names[ZERO_INSTANCE] = new Name(getFunction(NF_zeroInstance), names[FIELD_TYPE], names[LINKER_CALL]);
 864                 argIndex = ZERO_INSTANCE;
 865             }
 866             if (needsCast)
 867                 names[POST_CAST] = new Name(getFunction(NF_checkCast), names[DMH_THIS], names[argIndex]);
 868         }
 869         for (Name n : names)  assert(n != null);
 870 
 871         LambdaForm form;
 872         if (needsCast || needsInit) {
 873             // can't use the pre-generated form when casting and/or initializing
 874             form = LambdaForm.create(ARG_LIMIT, names, RESULT);
 875         } else {
 876             form = LambdaForm.create(ARG_LIMIT, names, RESULT, kind);
 877         }
 878 
 879         if (LambdaForm.debugNames()) {
 880             // add some detail to the lambdaForm debugname,
 881             // significant only for debugging
 882             StringBuilder nameBuilder = new StringBuilder(kind.methodName);
 883             if (isStatic) {
 884                 nameBuilder.append("Static");
 885             } else {
 886                 nameBuilder.append("Field");
 887             }
 888             if (needsCast) {

 894             LambdaForm.associateWithDebugName(form, nameBuilder.toString());
 895         }
 896         return form;
 897     }
 898 
 899     /**
 900      * Pre-initialized NamedFunctions for bootstrapping purposes.
 901      */
 902     static final byte NF_internalMemberName = 0,
 903             NF_internalMemberNameEnsureInit = 1,
 904             NF_ensureInitialized = 2,
 905             NF_fieldOffset = 3,
 906             NF_checkBase = 4,
 907             NF_staticBase = 5,
 908             NF_staticOffset = 6,
 909             NF_checkCast = 7,
 910             NF_allocateInstance = 8,
 911             NF_constructorMethod = 9,
 912             NF_UNSAFE = 10,
 913             NF_checkReceiver = 11,
 914             NF_fieldType = 12,
 915             NF_staticFieldType = 13,
 916             NF_zeroInstance = 14,
 917             NF_nullCheck = 15,
 918             NF_LIMIT = 16;
 919 
 920     private static final @Stable NamedFunction[] NFS = new NamedFunction[NF_LIMIT];
 921 
 922     private static NamedFunction getFunction(byte func) {
 923         NamedFunction nf = NFS[func];
 924         if (nf != null) {
 925             return nf;
 926         }
 927         // Each nf must be statically invocable or we get tied up in our bootstraps.
 928         nf = NFS[func] = createFunction(func);
 929         assert(InvokerBytecodeGenerator.isStaticallyInvocable(nf));
 930         return nf;
 931     }
 932 
 933     private static final MethodType CLS_OBJ_TYPE = MethodType.methodType(Class.class, Object.class);
 934 
 935     private static final MethodType OBJ_OBJ_TYPE = MethodType.methodType(Object.class, Object.class);
 936 
 937     private static final MethodType LONG_OBJ_TYPE = MethodType.methodType(long.class, Object.class);
 938 
 939     private static NamedFunction createFunction(byte func) {
 940         try {
 941             switch (func) {
 942                 case NF_internalMemberName:
 943                     return getNamedFunction("internalMemberName", OBJ_OBJ_TYPE);
 944                 case NF_internalMemberNameEnsureInit:
 945                     return getNamedFunction("internalMemberNameEnsureInit", OBJ_OBJ_TYPE);
 946                 case NF_ensureInitialized:
 947                     return getNamedFunction("ensureInitialized", MethodType.methodType(void.class, Object.class));
 948                 case NF_fieldOffset:
 949                     return getNamedFunction("fieldOffset", LONG_OBJ_TYPE);
 950                 case NF_checkBase:
 951                     return getNamedFunction("checkBase", OBJ_OBJ_TYPE);
 952                 case NF_staticBase:
 953                     return getNamedFunction("staticBase", OBJ_OBJ_TYPE);
 954                 case NF_staticOffset:
 955                     return getNamedFunction("staticOffset", LONG_OBJ_TYPE);
 956                 case NF_checkCast:
 957                     return getNamedFunction("checkCast", MethodType.methodType(Object.class, Object.class, Object.class));
 958                 case NF_allocateInstance:
 959                     return getNamedFunction("allocateInstance", OBJ_OBJ_TYPE);
 960                 case NF_constructorMethod:
 961                     return getNamedFunction("constructorMethod", OBJ_OBJ_TYPE);
 962                 case NF_UNSAFE:
 963                     MemberName member = new MemberName(MethodHandleStatics.class, "UNSAFE", Unsafe.class, REF_getStatic);
 964                     return new NamedFunction(
 965                             MemberName.getFactory().resolveOrFail(REF_getStatic, member,
 966                                                                   DirectMethodHandle.class, LM_TRUSTED,
 967                                                                   NoSuchFieldException.class));
 968                 case NF_checkReceiver:
 969                     member = new MemberName(DirectMethodHandle.class, "checkReceiver", OBJ_OBJ_TYPE, REF_invokeVirtual);
 970                     return new NamedFunction(
 971                         MemberName.getFactory().resolveOrFail(REF_invokeVirtual, member,
 972                                                               DirectMethodHandle.class, LM_TRUSTED,
 973                                                               NoSuchMethodException.class));
 974                 case NF_fieldType:
 975                     return getNamedFunction("fieldType", CLS_OBJ_TYPE);
 976                 case NF_staticFieldType:
 977                     return getNamedFunction("staticFieldType", CLS_OBJ_TYPE);
 978                 case NF_zeroInstance:
 979                     return getNamedFunction("zeroInstanceIfNull", MethodType.methodType(Object.class, Class.class, Object.class));
 980                 case NF_nullCheck:
 981                     return getNamedFunction("nullCheck", OBJ_OBJ_TYPE);
 982                 default:
 983                     throw newInternalError("Unknown function: " + func);
 984             }
 985         } catch (ReflectiveOperationException ex) {
 986             throw newInternalError(ex);
 987         }
 988     }
 989 
 990     private static NamedFunction getNamedFunction(String name, MethodType type)
 991         throws ReflectiveOperationException
 992     {
 993         MemberName member = new MemberName(DirectMethodHandle.class, name, type, REF_invokeStatic);
 994         return new NamedFunction(
 995                 MemberName.getFactory().resolveOrFail(REF_invokeStatic, member,
 996                                                       DirectMethodHandle.class, LM_TRUSTED,
 997                                                       NoSuchMethodException.class));
 998     }
 999 
1000     static {
1001         // The Holder class will contain pre-generated DirectMethodHandles resolved
< prev index next >