/// <summary> /// Create a LF which simply reinvokes a target of the given basic type. </summary> internal static LambdaForm MakeReinvokerForm(MethodHandle target, int whichCache, Object constraint, String debugString, bool forceInline, NamedFunction getTargetFn, NamedFunction preActionFn) { MethodType mtype = target.Type().BasicType(); bool customized = (whichCache <0 || mtype.ParameterSlotCount()> MethodType.MAX_MH_INVOKER_ARITY); bool hasPreAction = (preActionFn != null); LambdaForm form; if (!customized) { form = mtype.Form().CachedLambdaForm(whichCache); if (form != null) { return(form); } } const int THIS_DMH = 0; const int ARG_BASE = 1; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int ARG_LIMIT = ARG_BASE + mtype.parameterCount(); int ARG_LIMIT = ARG_BASE + mtype.ParameterCount(); int nameCursor = ARG_LIMIT; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int PRE_ACTION = hasPreAction ? nameCursor++ : -1; int PRE_ACTION = hasPreAction ? nameCursor++: -1; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int NEXT_MH = customized ? -1 : nameCursor++; int NEXT_MH = customized ? -1 : nameCursor++; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int REINVOKE = nameCursor++; int REINVOKE = nameCursor++; LambdaForm.Name[] names = LambdaForm.Arguments(nameCursor - ARG_LIMIT, mtype.InvokerType()); assert(names.Length == nameCursor); names[THIS_DMH] = names[THIS_DMH].WithConstraint(constraint); Object[] targetArgs; if (hasPreAction) { names[PRE_ACTION] = new LambdaForm.Name(preActionFn, names[THIS_DMH]); } if (customized) { targetArgs = Arrays.CopyOfRange(names, ARG_BASE, ARG_LIMIT, typeof(Object[])); names[REINVOKE] = new LambdaForm.Name(target, targetArgs); // the invoker is the target itself } else { names[NEXT_MH] = new LambdaForm.Name(getTargetFn, names[THIS_DMH]); targetArgs = Arrays.CopyOfRange(names, THIS_DMH, ARG_LIMIT, typeof(Object[])); targetArgs[0] = names[NEXT_MH]; // overwrite this MH with next MH names[REINVOKE] = new LambdaForm.Name(mtype, targetArgs); } form = new LambdaForm(debugString, ARG_LIMIT, names, forceInline); if (!customized) { form = mtype.Form().SetCachedLambdaForm(whichCache, form); } return(form); }
internal virtual void CheckTargetChange(MethodHandle oldTarget, MethodHandle newTarget) { MethodType oldType = oldTarget.Type(); MethodType newType = newTarget.Type(); // null check! if (!newType.Equals(oldType)) { throw WrongTargetType(newTarget, oldType); } }
private static MethodHandle BindCaller(MethodHandle target, Class hostClass) { MethodHandle cbmh = MethodHandleImpl.BindCaller(target, hostClass); if (target.VarargsCollector) { MethodType type = cbmh.Type(); int arity = type.ParameterCount(); return(cbmh.AsVarargsCollector(type.ParameterType(arity - 1))); } return(cbmh); }
/*non-public*/ internal static String AddTypeString(Object obj, MethodHandle target) { String str = Convert.ToString(obj); if (target == null) { return(str); } int paren = str.IndexOf('('); if (paren >= 0) { str = str.Substring(0, paren); } return(str + target.Type()); }
/*non-public*/ internal static String GetNameString(MethodHandle target, MethodType type) { if (type == null) { type = target.Type(); } MemberName name = null; if (target != null) { name = target.InternalMemberName(); } if (name == null) { return("invoke" + type); } return(name.Name + type); }
/*non-public*/ internal static String GetNameString(MethodHandle target, MethodHandle typeHolder) { return(GetNameString(target, typeHolder == null ? (MethodType)null : typeHolder.Type())); }
internal virtual LambdaForm CollectArgumentArrayForm(int pos, MethodHandle arrayCollector) { MethodType collectorType = arrayCollector.Type(); int collectorArity = collectorType.ParameterCount(); assert(arrayCollector.IntrinsicName() == Intrinsic.NEW_ARRAY); Class arrayType = collectorType.ReturnType(); Class elementType = arrayType.ComponentType; BasicType argType = basicType(elementType); int argTypeKey = argType.ordinal(); if (argType.basicTypeClass() != elementType) { // return null if it requires more metadata (like String[].class) if (!elementType.Primitive) { return(null); } argTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal(); } assert(collectorType.ParameterList().Equals(Collections.NCopies(collectorArity, elementType))); Transform.Kind kind = Transform.Kind.COLLECT_ARGS_TO_ARRAY; Transform key = Transform.Of(kind, pos, collectorArity, argTypeKey); LambdaForm form = GetInCache(key); if (form != null) { assert(form.Arity_Renamed == LambdaForm.Arity_Renamed - 1 + collectorArity); return(form); } LambdaFormBuffer buf = Buffer(); buf.StartEdit(); assert(pos + 1 <= LambdaForm.Arity_Renamed); assert(pos > 0); // cannot filter the MH arg itself Name[] newParams = new Name[collectorArity]; for (int i = 0; i < collectorArity; i++) { newParams[i] = new Name(pos + i, argType); } Name callCombiner = new Name(arrayCollector, (Object[])newParams); //... // insert the new expression int exprPos = LambdaForm.Arity(); buf.InsertExpression(exprPos, callCombiner); // insert new arguments int argPos = pos + 1; // skip result parameter foreach (Name newParam in newParams) { buf.InsertParameter(argPos++, newParam); } assert(buf.LastIndexOf(callCombiner) == exprPos + newParams.Length); buf.ReplaceParameterByCopy(pos, exprPos + newParams.Length); form = buf.EndEdit(); return(PutInCache(key, form)); }
protected internal DelegatingMethodHandle(MethodHandle target) : this(target.Type(), target) { }
// this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite: internal static CallSite MakeSite(MethodHandle bootstrapMethod, String name, MethodType type, Object info, Class callerClass) // Callee information: // Extra arguments for BSM, if any: // Caller information: { MethodHandles.Lookup caller = IMPL_LOOKUP.@in(callerClass); CallSite site; try { Object binding; info = MaybeReBox(info); if (info == null) { binding = bootstrapMethod.invoke(caller, name, type); } else if (!info.GetType().IsArray) { binding = bootstrapMethod.invoke(caller, name, type, info); } else { Object[] argv = (Object[])info; MaybeReBoxElements(argv); switch (argv.Length) { case 0: binding = bootstrapMethod.invoke(caller, name, type); break; case 1: binding = bootstrapMethod.invoke(caller, name, type, argv[0]); break; case 2: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1]); break; case 3: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2]); break; case 4: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2], argv[3]); break; case 5: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2], argv[3], argv[4]); break; case 6: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); break; default: const int NON_SPREAD_ARG_COUNT = 3; // (caller, name, type) if (NON_SPREAD_ARG_COUNT + argv.Length > MethodType.MAX_MH_ARITY) { throw new BootstrapMethodError("too many bootstrap method arguments"); } MethodType bsmType = bootstrapMethod.Type(); MethodType invocationType = MethodType.GenericMethodType(NON_SPREAD_ARG_COUNT + argv.Length); MethodHandle typedBSM = bootstrapMethod.AsType(invocationType); MethodHandle spreader = invocationType.Invokers().SpreadInvoker(NON_SPREAD_ARG_COUNT); binding = spreader.invokeExact(typedBSM, (Object)caller, (Object)name, (Object)type, argv); break; } } //System.out.println("BSM for "+name+type+" => "+binding); if (binding is CallSite) { site = (CallSite)binding; } else { throw new ClassCastException("bootstrap method failed to produce a CallSite"); } if (!site.Target.Type().Equals(type)) { throw WrongTargetType(site.Target, type); } } catch (Throwable ex) { BootstrapMethodError bex; if (ex is BootstrapMethodError) { bex = (BootstrapMethodError)ex; } else { bex = new BootstrapMethodError("call site initialization exception", ex); } throw bex; } return(site); }
/// <summary> /// Returns the type of this call site's target. /// Although targets may change, any call site's type is permanent, and can never change to an unequal type. /// The {@code setTarget} method enforces this invariant by refusing any new target that does /// not have the previous target's type. </summary> /// <returns> the type of the current target, which is also the type of any future target </returns> public virtual MethodType Type() { // warning: do not call getTarget here, because CCS.getTarget can throw IllegalStateException return(Target_Renamed.Type()); }
/// <summary> /// Make a call site object equipped with an initial target method handle. </summary> /// <param name="target"> the method handle which will be the initial target of the call site </param> /// <exception cref="NullPointerException"> if the proposed target is null </exception> /*package-private*/ internal CallSite(MethodHandle target) { target.Type(); // null check this.Target_Renamed = target; }
/// <summary> /// Produces an instance of the given single-method interface which redirects /// its calls to the given method handle. /// <para> /// A single-method interface is an interface which declares a uniquely named method. /// When determining the uniquely named method of a single-method interface, /// the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode}) /// are disregarded. For example, <seealso cref="java.util.Comparator"/> is a single-method interface, /// even though it re-declares the {@code Object.equals} method. /// </para> /// <para> /// The interface must be public. No additional access checks are performed. /// </para> /// <para> /// The resulting instance of the required type will respond to /// invocation of the type's uniquely named method by calling /// the given target on the incoming arguments, /// and returning or throwing whatever the target /// returns or throws. The invocation will be as if by /// {@code target.invoke}. /// The target's type will be checked before the /// instance is created, as if by a call to {@code asType}, /// which may result in a {@code WrongMethodTypeException}. /// </para> /// <para> /// The uniquely named method is allowed to be multiply declared, /// with distinct type descriptors. (E.g., it can be overloaded, /// or can possess bridge methods.) All such declarations are /// connected directly to the target method handle. /// Argument and return types are adjusted by {@code asType} /// for each individual declaration. /// </para> /// <para> /// The wrapper instance will implement the requested interface /// and its super-types, but no other single-method interfaces. /// This means that the instance will not unexpectedly /// pass an {@code instanceof} test for any unrequested type. /// <p style="font-size:smaller;"> /// <em>Implementation Note:</em> /// Therefore, each instance must implement a unique single-method interface. /// Implementations may not bundle together /// multiple single-method interfaces onto single implementation classes /// in the style of <seealso cref="java.awt.AWTEventMulticaster"/>. /// </para> /// <para> /// The method handle may throw an <em>undeclared exception</em>, /// which means any checked exception (or other checked throwable) /// not declared by the requested type's single abstract method. /// If this happens, the throwable will be wrapped in an instance of /// <seealso cref="java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException"/> /// and thrown in that wrapped form. /// </para> /// <para> /// Like <seealso cref="java.lang.Integer#valueOf Integer.valueOf"/>, /// {@code asInterfaceInstance} is a factory method whose results are defined /// by their behavior. /// It is not guaranteed to return a new instance for every call. /// </para> /// <para> /// Because of the possibility of <seealso cref="java.lang.reflect.Method#isBridge bridge methods"/> /// and other corner cases, the interface may also have several abstract methods /// with the same name but having distinct descriptors (types of returns and parameters). /// In this case, all the methods are bound in common to the one given target. /// The type check and effective {@code asType} conversion is applied to each /// method type descriptor, and all abstract methods are bound to the target in common. /// Beyond this type check, no further checks are made to determine that the /// abstract methods are related in any way. /// </para> /// <para> /// Future versions of this API may accept additional types, /// such as abstract classes with single abstract methods. /// Future versions of this API may also equip wrapper instances /// with one or more additional public "marker" interfaces. /// </para> /// <para> /// If a security manager is installed, this method is caller sensitive. /// During any invocation of the target method handle via the returned wrapper, /// the original creator of the wrapper (the caller) will be visible /// to context checks requested by the security manager. /// /// </para> /// </summary> /// @param <T> the desired type of the wrapper, a single-method interface </param> /// <param name="intfc"> a class object representing {@code T} </param> /// <param name="target"> the method handle to invoke from the wrapper </param> /// <returns> a correctly-typed wrapper for the given target </returns> /// <exception cref="NullPointerException"> if either argument is null </exception> /// <exception cref="IllegalArgumentException"> if the {@code intfc} is not a /// valid argument to this method </exception> /// <exception cref="WrongMethodTypeException"> if the target cannot /// be converted to the type required by the requested interface </exception> // Other notes to implementors: // <p> // No stable mapping is promised between the single-method interface and // the implementation class C. Over time, several implementation // classes might be used for the same type. // <p> // If the implementation is able // to prove that a wrapper of the required type // has already been created for a given // method handle, or for another method handle with the // same behavior, the implementation may return that wrapper in place of // a new wrapper. // <p> // This method is designed to apply to common use cases // where a single method handle must interoperate with // an interface that implements a function-like // API. Additional variations, such as single-abstract-method classes with // private constructors, or interfaces with multiple but related // entry points, must be covered by hand-written or automatically // generated adapter classes. // //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @CallerSensitive public static <T> T asInterfaceInstance(final Class intfc, final MethodHandle target) //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: public static T asInterfaceInstance <T>(Class intfc, MethodHandle target) { if (!intfc.Interface || !Modifier.IsPublic(intfc.Modifiers)) { throw newIllegalArgumentException("not a public interface", intfc.Name); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final MethodHandle mh; MethodHandle mh; if (System.SecurityManager != null) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Class caller = sun.reflect.Reflection.getCallerClass(); Class caller = Reflection.CallerClass; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ClassLoader ccl = caller != null ? caller.getClassLoader() : null; ClassLoader ccl = caller != null ? caller.ClassLoader : null; ReflectUtil.checkProxyPackageAccess(ccl, intfc); mh = ccl != null?BindCaller(target, caller) : target; } else { mh = target; } ClassLoader proxyLoader = intfc.ClassLoader; if (proxyLoader == null) { ClassLoader cl = Thread.CurrentThread.ContextClassLoader; // avoid use of BCP proxyLoader = cl != null ? cl : ClassLoader.SystemClassLoader; } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Method[] methods = getSingleNameMethods(intfc); Method[] methods = GetSingleNameMethods(intfc); if (methods == null) { throw newIllegalArgumentException("not a single-method interface", intfc.Name); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final MethodHandle[] vaTargets = new MethodHandle[methods.length]; MethodHandle[] vaTargets = new MethodHandle[methods.Length]; for (int i = 0; i < methods.Length; i++) { Method sm = methods[i]; MethodType smMT = MethodType.MethodType(sm.ReturnType, sm.ParameterTypes); MethodHandle checkTarget = mh.AsType(smMT); // make throw WMT checkTarget = checkTarget.AsType(checkTarget.Type().ChangeReturnType(typeof(Object))); vaTargets[i] = checkTarget.AsSpreader(typeof(Object[]), smMT.ParameterCount()); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final InvocationHandler ih = new InvocationHandler() InvocationHandler ih = new InvocationHandlerAnonymousInnerClassHelper(intfc, target, methods, vaTargets); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Object proxy; Object proxy; if (System.SecurityManager != null) { // sun.invoke.WrapperInstance is a restricted interface not accessible // by any non-null class loader. //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final ClassLoader loader = proxyLoader; ClassLoader loader = proxyLoader; proxy = AccessController.doPrivileged(new PrivilegedActionAnonymousInnerClassHelper(intfc, ih, loader)); } else { proxy = Proxy.NewProxyInstance(proxyLoader, new Class[] { intfc, typeof(WrapperInstance) }, ih); } return(intfc.Cast(proxy)); }
/// <summary> /// Check the meta-factory arguments for errors </summary> /// <exception cref="LambdaConversionException"> if there are improper conversions </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: void validateMetafactoryArgs() throws LambdaConversionException internal virtual void ValidateMetafactoryArgs() { switch (ImplKind) { case MethodHandleInfo.REF_invokeInterface: case MethodHandleInfo.REF_invokeVirtual: case MethodHandleInfo.REF_invokeStatic: case MethodHandleInfo.REF_newInvokeSpecial: case MethodHandleInfo.REF_invokeSpecial: break; default: throw new LambdaConversionException(string.Format("Unsupported MethodHandle kind: {0}", ImplInfo)); } // Check arity: optional-receiver + captured + SAM == impl //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int implArity = implMethodType.parameterCount(); int implArity = ImplMethodType.ParameterCount(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int receiverArity = implIsInstanceMethod ? 1 : 0; int receiverArity = ImplIsInstanceMethod ? 1 : 0; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int capturedArity = invokedType.parameterCount(); int capturedArity = InvokedType.ParameterCount(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int samArity = samMethodType.parameterCount(); int samArity = SamMethodType.ParameterCount(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int instantiatedArity = instantiatedMethodType.parameterCount(); int instantiatedArity = InstantiatedMethodType.ParameterCount(); if (implArity + receiverArity != capturedArity + samArity) { throw new LambdaConversionException(string.Format("Incorrect number of parameters for {0} method {1}; {2:D} captured parameters, {3:D} functional interface method parameters, {4:D} implementation parameters", ImplIsInstanceMethod ? "instance" : "static", ImplInfo, capturedArity, samArity, implArity)); } if (instantiatedArity != samArity) { throw new LambdaConversionException(string.Format("Incorrect number of parameters for {0} method {1}; {2:D} instantiated parameters, {3:D} functional interface method parameters", ImplIsInstanceMethod ? "instance" : "static", ImplInfo, instantiatedArity, samArity)); } foreach (MethodType bridgeMT in AdditionalBridges) { if (bridgeMT.ParameterCount() != samArity) { throw new LambdaConversionException(string.Format("Incorrect number of parameters for bridge signature {0}; incompatible with {1}", bridgeMT, SamMethodType)); } } // If instance: first captured arg (receiver) must be subtype of class where impl method is defined //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int capturedStart; int capturedStart; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int samStart; int samStart; if (ImplIsInstanceMethod) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Class receiverClass; Class receiverClass; // implementation is an instance method, adjust for receiver in captured variables / SAM arguments if (capturedArity == 0) { // receiver is function parameter capturedStart = 0; samStart = 1; receiverClass = InstantiatedMethodType.ParameterType(0); } else { // receiver is a captured variable capturedStart = 1; samStart = 0; receiverClass = InvokedType.ParameterType(0); } // check receiver type if (!receiverClass.IsSubclassOf(ImplDefiningClass)) { throw new LambdaConversionException(string.Format("Invalid receiver type {0}; not a subtype of implementation type {1}", receiverClass, ImplDefiningClass)); } Class implReceiverClass = ImplMethod.Type().ParameterType(0); if (implReceiverClass != ImplDefiningClass && !receiverClass.IsSubclassOf(implReceiverClass)) { throw new LambdaConversionException(string.Format("Invalid receiver type {0}; not a subtype of implementation receiver type {1}", receiverClass, implReceiverClass)); } } else { // no receiver capturedStart = 0; samStart = 0; } // Check for exact match on non-receiver captured arguments //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int implFromCaptured = capturedArity - capturedStart; int implFromCaptured = capturedArity - capturedStart; for (int i = 0; i < implFromCaptured; i++) { Class implParamType = ImplMethodType.ParameterType(i); Class capturedParamType = InvokedType.ParameterType(i + capturedStart); if (!capturedParamType.Equals(implParamType)) { throw new LambdaConversionException(string.Format("Type mismatch in captured lambda parameter {0:D}: expecting {1}, found {2}", i, capturedParamType, implParamType)); } } // Check for adaptation match on SAM arguments //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int samOffset = samStart - implFromCaptured; int samOffset = samStart - implFromCaptured; for (int i = implFromCaptured; i < implArity; i++) { Class implParamType = ImplMethodType.ParameterType(i); Class instantiatedParamType = InstantiatedMethodType.ParameterType(i + samOffset); if (!IsAdaptableTo(instantiatedParamType, implParamType, true)) { throw new LambdaConversionException(string.Format("Type mismatch for lambda argument {0:D}: {1} is not convertible to {2}", i, instantiatedParamType, implParamType)); } } // Adaptation match: return type Class expectedType = InstantiatedMethodType.ReturnType(); Class actualReturnType = (ImplKind == MethodHandleInfo.REF_newInvokeSpecial) ? ImplDefiningClass : ImplMethodType.ReturnType(); Class samReturnType = SamMethodType.ReturnType(); if (!IsAdaptableToAsReturn(actualReturnType, expectedType)) { throw new LambdaConversionException(string.Format("Type mismatch for lambda return: {0} is not convertible to {1}", actualReturnType, expectedType)); } if (!IsAdaptableToAsReturnStrict(expectedType, samReturnType)) { throw new LambdaConversionException(string.Format("Type mismatch for lambda expected return: {0} is not convertible to {1}", expectedType, samReturnType)); } foreach (MethodType bridgeMT in AdditionalBridges) { if (!IsAdaptableToAsReturnStrict(expectedType, bridgeMT.ReturnType())) { throw new LambdaConversionException(string.Format("Type mismatch for lambda expected return: {0} is not convertible to {1}", expectedType, bridgeMT.ReturnType())); } } }