Ejemplo n.º 1
0
        public static T DynamicBinderMemberLookup <T>(int kind, string clazz, string name, string sig, [email protected] callerID)
            where T : class             /* delegate */
        {
#if FIRST_PASS
            return(null);
#else
            try
            {
                java.lang.invoke.MethodHandle mh = DynamicLoadMethodHandleImpl(kind, clazz, name, sig, callerID);
                return(GetDelegateForInvokeExact <T>(mh.asType(MethodHandleUtil.GetDelegateMethodType(typeof(T)))));
            }
            catch (java.lang.IncompatibleClassChangeError x)
            {
                if (x.getCause() is java.lang.NoSuchMethodException)
                {
                    throw new java.lang.NoSuchMethodError(x.getCause().Message);
                }
                if (x.getCause() is java.lang.NoSuchFieldException)
                {
                    throw new java.lang.NoSuchFieldError(x.getCause().Message);
                }
                if (x.getCause() is java.lang.IllegalAccessException)
                {
                    throw new java.lang.IllegalAccessError(x.getCause().Message);
                }
                throw;
            }
#endif
        }
Ejemplo n.º 2
0
        public static T GetDelegateForInvoke <T>(global::java.lang.invoke.MethodHandle h, ref InvokeCache <T> cache)
            where T : class
        {
#if FIRST_PASS
            return(null);
#else
            if (cache.type == h.type() && cache.del != null)
            {
                return(cache.del);
            }
            T del = h.vmtarget as T;
            if (del == null)
            {
                global::java.lang.invoke.MethodHandle adapter = global::java.lang.invoke.MethodHandles.exactInvoker(h.type());
                if (h.isVarargsCollector())
                {
                    adapter = adapter.asVarargsCollector(h.type().parameterType(h.type().parameterCount() - 1));
                }
                del = (T)adapter.asType(MethodHandleUtil.GetDelegateMethodType(typeof(T))).vmtarget;
                if (Interlocked.CompareExchange(ref cache.type, h.type(), null) == null)
                {
                    cache.del = del;
                }
            }
            return(del);
#endif
        }
Ejemplo n.º 3
0
        public static java.lang.invoke.MethodType LoadMethodType <T>()
            where T : class             // Delegate
        {
#if FIRST_PASS
            return(null);
#else
            return(MethodHandleUtil.GetDelegateMethodType(typeof(T)));
#endif
        }
Ejemplo n.º 4
0
        public static Delegate DynamicCreateDelegate(object obj, Type delegateType, string name, string sig)
        {
#if !WINRT
#if FIRST_PASS
            return(null);
#else
            TypeWrapper   tw = TypeWrapper.FromClass(ikvm.runtime.Util.getClassFromObject(obj));
            MethodWrapper mw = tw.GetMethodWrapper(name, sig, true);
            if (mw == null || mw.IsStatic || !mw.IsPublic)
            {
#if NO_REF_EMIT
                java.lang.invoke.MethodType methodType = MethodHandleUtil.GetDelegateMethodType(delegateType);
                if (methodType.parameterCount() > MethodHandleUtil.MaxArity)
                {
                    throw new NotImplementedException();
                }
                java.lang.invoke.MethodHandle exception = java.lang.invoke.MethodHandles.publicLookup()
                                                          .findConstructor(mw == null || mw.IsStatic ? typeof(java.lang.AbstractMethodError) : typeof(java.lang.IllegalAccessError),
                                                                           java.lang.invoke.MethodType.methodType(typeof(void), typeof(string)))
                                                          .bindTo(tw.Name + ".Invoke" + sig);
                return(Delegate.CreateDelegate(delegateType,
                                               java.lang.invoke.MethodHandles.dropArguments(
                                                   java.lang.invoke.MethodHandles.foldArguments(java.lang.invoke.MethodHandles.throwException(methodType.returnType(), exception.type().returnType()), exception),
                                                   0, methodType.parameterArray()).vmtarget, "Invoke"));
#else
                MethodInfo      invoke         = delegateType.GetMethod("Invoke");
                ParameterInfo[] parameters     = invoke.GetParameters();
                Type[]          parameterTypes = new Type[parameters.Length + 1];
                parameterTypes[0] = typeof(object);
                for (int i = 0; i < parameters.Length; i++)
                {
                    parameterTypes[i + 1] = parameters[i].ParameterType;
                }
                System.Reflection.Emit.DynamicMethod dm = new System.Reflection.Emit.DynamicMethod("Invoke", invoke.ReturnType, parameterTypes);
                CodeEmitter ilgen = CodeEmitter.Create(dm);
                ilgen.Emit(System.Reflection.Emit.OpCodes.Ldstr, tw.Name + ".Invoke" + sig);
                ClassLoaderWrapper.GetBootstrapClassLoader()
                .LoadClassByDottedName(mw == null || mw.IsStatic ? "java.lang.AbstractMethodError" : "java.lang.IllegalAccessError")
                .GetMethodWrapper("<init>", "(Ljava.lang.String;)V", false)
                .EmitNewobj(ilgen);
                ilgen.Emit(System.Reflection.Emit.OpCodes.Throw);
                ilgen.DoEmit();
                return(dm.CreateDelegate(delegateType, obj));
#endif
            }
            else
            {
                mw.ResolveMethod();
                return(Delegate.CreateDelegate(delegateType, obj, (MethodInfo)mw.GetMethod()));
            }
#endif
#else
            throw new NotImplementedException();
#endif
        }
Ejemplo n.º 5
0
        public static T GetDelegateForInvoke <T>(global::java.lang.invoke.MethodHandle h, java.lang.invoke.MethodType realType, ref InvokeCache <T> cache)
            where T : class
        {
#if FIRST_PASS
            return(null);
#else
            T del;
            if (cache.Type == h.type() && (del = (h.isVarargsCollector() ? cache.varArg : cache.fixedArg)) != null)
            {
                return(del);
            }
            del = h.form.vmentry.vmtarget as T;
            if (del == null)
            {
                global::java.lang.invoke.MethodHandle adapter = global::java.lang.invoke.MethodHandles.exactInvoker(h.type());
                if (h.isVarargsCollector())
                {
                    adapter = adapter.asVarargsCollector(h.type().parameterType(h.type().parameterCount() - 1));
                }
                // if realType is set, the delegate contains erased unloadable types
                if (realType != null)
                {
                    adapter = adapter.asType(realType.insertParameterTypes(0, [email protected] <java.lang.invoke.MethodHandle> .Value)).asFixedArity();
                }
                adapter = adapter.asType(MethodHandleUtil.GetDelegateMethodType(typeof(T)));
                del     = GetDelegateForInvokeExact <T>(adapter);
                if (cache.TrySetType(h.type()))
                {
                    if (h.isVarargsCollector())
                    {
                        cache.varArg = del;
                    }
                    else
                    {
                        cache.fixedArg = del;
                    }
                }
            }
            return(del);
#endif
        }
Ejemplo n.º 6
0
        public static T GetDelegateForInvoke <T>(global::java.lang.invoke.MethodHandle h, ref InvokeCache <T> cache)
            where T : class
        {
#if FIRST_PASS
            return(null);
#else
            T del;
            if (cache.Type == h.type() && (del = (h.isVarargsCollector() ? cache.varArg : cache.fixedArg)) != null)
            {
                return(del);
            }
            del = h.form.vmentry.vmtarget as T;
            if (del == null)
            {
                global::java.lang.invoke.MethodHandle adapter = global::java.lang.invoke.MethodHandles.exactInvoker(h.type());
                if (h.isVarargsCollector())
                {
                    adapter = adapter.asVarargsCollector(h.type().parameterType(h.type().parameterCount() - 1));
                }
                adapter = adapter.asType(MethodHandleUtil.GetDelegateMethodType(typeof(T)));
                del     = GetDelegateForInvokeExact <T>(adapter);
                if (cache.TrySetType(h.type()))
                {
                    if (h.isVarargsCollector())
                    {
                        cache.varArg = del;
                    }
                    else
                    {
                        cache.fixedArg = del;
                    }
                }
            }
            return(del);
#endif
        }
Ejemplo n.º 7
0
 internal ConstantMethodHandle(Delegate del)
     : base(MethodHandleUtil.GetDelegateMethodType(del.GetType()))
 {
     this.vmtarget = del;
 }