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>(java.lang.invoke.MethodHandles.explicitCastArguments(mh, 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 }
public InvocationHandlerAnonymousInnerClassHelper(Type intfc, java.lang.invoke.MethodHandle target, System.Reflection.MethodInfo[] methods, java.lang.invoke.MethodHandle[] vaTargets) { this.Intfc = intfc; this.Target = target; this.Methods = methods; this.VaTargets = vaTargets; }
public static java.lang.invoke.MethodHandle DynamicLoadMethodHandle(ref java.lang.invoke.MethodHandle cache, int kind, string clazz, string name, string sig, [email protected] callerID) { if (cache == null) { Interlocked.CompareExchange(ref cache, DynamicLoadMethodHandleImpl(kind, clazz, name, sig, callerID), null); } return(cache); }
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 }
public static java.lang.invoke.MethodHandle DynamicEraseInvokeExact(java.lang.invoke.MethodHandle mh, java.lang.invoke.MethodType expected, java.lang.invoke.MethodType target) { #if FIRST_PASS return(null); #else if (mh.type() != expected) { throw new java.lang.invoke.WrongMethodTypeException(); } return(java.lang.invoke.MethodHandles.explicitCastArguments(mh, target)); #endif }
public static T GetDelegateForInvokeBasic <T>(java.lang.invoke.MethodHandle h) where T : class { #if FIRST_PASS return(null); #else T del = h.form.vmentry.vmtarget as T; if (del == null) { del = MethodHandleUtil.GetVoidAdapter(h.form.vmentry) as T; } return(del); #endif }
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 }
void IIndyCallSite.SetTarget(java.lang.invoke.MethodHandle mh) { #if !FIRST_PASS target = ByteCodeHelper.GetDelegateForInvokeExact <T>(mh); #endif }