public static void LinkIndyCallSite <T>(ref IndyCallSite <T> site, java.lang.invoke.CallSite cs, Exception x)
            where T : class             // Delegate
        {
#if !FIRST_PASS
            IndyCallSite <T> ics;
            if (x != null || cs == null || (ics = cs.ics as IndyCallSite <T>) == null)
            {
                x = MapException <Exception>(x ?? (cs == null
                                        ? (Exception) new java.lang.ClassCastException("bootstrap method failed to produce a CallSite")
                                        : new java.lang.invoke.WrongMethodTypeException()), MapFlags.None);
                java.lang.invoke.MethodType type = LoadMethodType <T>();
                ics = new IndyCallSite <T>((T)
                                           java.lang.invoke.MethodHandles.dropArguments(
                                               java.lang.invoke.MethodHandles.foldArguments(
                                                   java.lang.invoke.MethodHandles.throwException(type.returnType(), typeof(java.lang.BootstrapMethodError)),
                                                   new ConstantMethodHandle((MH <Exception, java.lang.BootstrapMethodError>)CreateBootstrapException).bindTo(x)),
                                               0, type.parameterArray())
                                           .vmtarget,
                                           false);
            }
            IndyCallSite <T> curr = site;
            if (curr.IsBootstrap)
            {
                Interlocked.CompareExchange(ref site, ics, curr);
            }
#endif
        }
Exemple #2
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
        }
Exemple #3
0
        public static void DynamicLinkIndyCallSite <T>(ref IndyCallSite <T> site, java.lang.invoke.CallSite cs, Exception x, string signature, [email protected] callerID)
            where T : class             // Delegate
        {
#if !FIRST_PASS
            // when a CallSite is first constructed, it doesn't call MethodHandleNatives.setCallSiteTargetNormal(),
            // so we have to check if we need to initialize it here (i.e. attach an IndyCallSite<T> to it)
            if (cs != null)
            {
                if (cs.ics == null)
                {
                    Java_java_lang_invoke_MethodHandleNatives.InitializeCallSite(cs);
                }
                lock (cs.ics)
                {
                    cs.ics.SetTarget(cs.target);
                }
            }
            java.lang.invoke.MethodType typeCache = null;
            IndyCallSite <T>            ics;
            if (x != null || cs == null || cs.type() != DynamicLoadMethodType(ref typeCache, signature, callerID))
            {
                x = MapException <Exception>(x ?? (cs == null
                                        ? (Exception) new java.lang.ClassCastException("bootstrap method failed to produce a CallSite")
                                        : new java.lang.invoke.WrongMethodTypeException()), MapFlags.None);
                java.lang.invoke.MethodType   type = LoadMethodType <T>();
                java.lang.invoke.MethodHandle exc  = x is java.lang.BootstrapMethodError
                                        ? java.lang.invoke.MethodHandles.constant(typeof(java.lang.BootstrapMethodError), x)
                                        : java.lang.invoke.MethodHandles.publicLookup().findConstructor(typeof(java.lang.BootstrapMethodError), java.lang.invoke.MethodType.methodType(typeof(void), typeof(string), typeof(Exception)))
                                                     .bindTo("call site initialization exception").bindTo(x);
                ics = new IndyCallSite <T>();
                ((IIndyCallSite)ics).SetTarget(
                    java.lang.invoke.MethodHandles.dropArguments(
                        java.lang.invoke.MethodHandles.foldArguments(
                            java.lang.invoke.MethodHandles.throwException(type.returnType(), typeof(java.lang.BootstrapMethodError)),
                            exc),
                        0, type.parameterArray()));
            }
            else
            {
                ics = new IndyCallSite <T>();
                ((IIndyCallSite)ics).SetTarget(cs.dynamicInvoker().asType(LoadMethodType <T>()));
            }
            IndyCallSite <T> curr = site;
            if (curr.IsBootstrap)
            {
                Interlocked.CompareExchange(ref site, ics, curr);
            }
#endif
        }