예제 #1
0
    public static object createDelegate(MethodType newType, MethodHandle mh, int argnum, object argument)
    {
#if FIRST_PASS
        return(null);
#else
        Delegate del = (Delegate)mh.vmtarget;
        if (argnum == 0 &&
            del.Target == null
            // we don't have to check for instance methods on a Value Type, because DirectMethodHandle can't use a direct delegate for that anyway
            && (!del.Method.IsStatic || !del.Method.GetParameters()[0].ParameterType.IsValueType) &&
            !ReflectUtil.IsDynamicMethod(del.Method))
        {
            return(Delegate.CreateDelegate(MethodHandleUtil.CreateDelegateType(newType), argument, del.Method));
        }
        else
        {
            // slow path where we're generating a DynamicMethod
            if (mh.type().parameterType(argnum).isPrimitive())
            {
                argument = JVM.Unbox(argument);
            }
            MethodHandleUtil.DynamicMethodBuilder dm = new MethodHandleUtil.DynamicMethodBuilder("BoundMethodHandle", newType, mh, argument);
            for (int i = 0, count = mh.type().parameterCount(), pos = 0; i < count; i++)
            {
                if (i == argnum)
                {
                    dm.LoadValue();
                }
                else
                {
                    dm.Ldarg(pos++);
                }
            }
            dm.CallTarget();
            dm.Ret();
            return(dm.CreateDelegate());
        }
#endif
    }