GetCLRRecord() static private method

static private GetCLRRecord ( Type iface ) : RegistryRecord
iface System.Type
return RegistryRecord
Ejemplo n.º 1
0
        public static JniLocalHandle ArrayStrongCp2J <TBoth>(JNIEnv env, TBoth obj)
            where TBoth : class
        //should be where TBoth : IJvmProxy[]
        {
            if (obj == null)
            {
                return(JniLocalHandle.Zero);
            }
#if  DEBUG
            if (!typeof(TBoth).IsArray)
            {
                throw new ArgumentException("Must be array type");
            }
#endif
            var            array       = (Array)(object)obj;
            int            length      = array.Length;
            Type           elementType = typeof(TBoth).GetElementType();
            Class          elemClazz   = Registry.GetCLRRecord(elementType).JVMInterface;
            JniLocalHandle res         = env.NewObjectArrayPtr(length, elemClazz.jvmHandle, null);
            for (int i = 0; i < length; i++)
            {
                JniHandle item = StrongCp2J((IJvmProxy)array.GetValue(i));
                env.SetObjectArrayElement(res, i, item);
            }
            return(res);
        }
Ejemplo n.º 2
0
        public static JniHandle StrongC2Jp <TBoth>(JNIEnv env, TBoth obj)
        {
            // ReSharper disable CompareNonConstrainedGenericWithNull
            if (obj == null)
            {
                return(JniLocalHandle.Zero);
            }
            // ReSharper restore CompareNonConstrainedGenericWithNull
            RegistryRecord record = Registry.GetCLRRecord(obj.GetType());

            return(record.CreateJVMProxy(env, obj));
        }
Ejemplo n.º 3
0
        public static JniHandle StrongC2JDelegate(JNIEnv env, Delegate obj)
        {
            if (obj == null)
            {
                return(JniLocalHandle.Zero);
            }
            IJvmProxy proxy = obj.Target as IJvmProxy;

            if (proxy != null)
            {
                return(proxy.JvmHandle);
            }
            RegistryRecord record = Registry.GetCLRRecord(obj.GetType());

            return(record.CreateJVMProxy(env, obj));
        }
Ejemplo n.º 4
0
        public static TRes FullJ2C <TRes>(JNIEnv env, JniLocalHandle obj)
        {
            if (JniLocalHandle.IsNull(obj))
            {
                return(default(TRes));
            }
            Type reqType = typeof(TRes);

            if (reqType.IsPrimitive)
            {
                return((TRes)PrimJ2C(obj, env, reqType));
            }
#if DEBUG
            if (reqType.IsArray)
            {
                throw new InvalidOperationException("Call ArrayFullJ2C<TRes, TElem> instead");
            }
#endif
            Class clazz = env.GetObjectClass(obj);
            if (reqType == typeof(string) && clazz == String._class)
            {
                return((TRes)(object)StrongJ2CString(env, obj));
            }

            //now we deal only with JVM instances
            RegistryRecord record = Registry.GetJVMRecord(clazz);
            if (reqType.IsAssignableFrom(record.CLRInterface))
            {
                return((TRes)record.CreateCLRProxy(env, obj));
            }
            record = Registry.GetCLRRecord(reqType);
            if (Bridge.Setup.Debug)
            {
                if (!record.JVMInterface.isAssignableFrom(clazz))
                {
                    throw new InvalidCastException("Can't cast JVM instance" + clazz + " to " + reqType);
                }
            }
            return((TRes)record.CreateCLRProxy(env, obj));
        }
Ejemplo n.º 5
0
        public static TRes StrongJ2CpDelegate <TRes>(JNIEnv env, JniLocalHandle obj)
            where TRes : class //Delegate
        {
#if DEBUG
            if (!typeof(Delegate).IsAssignableFrom(typeof(TRes)))
            {
                throw new ArgumentException("expected delegate");
            }
#endif
            if (JniHandle.IsNull(obj))
            {
                return(default(TRes));
            }
            object res = __IClrProxy.GetObject(env, obj);
            if (res == null)
            {
                //that's delegate implemented in Java
                RegistryRecord delRecord = Registry.GetCLRRecord(typeof(TRes));
                IJvmProxy      jvmProxy  = delRecord.CreateCLRProxy(env, obj);
                return((TRes)(object)Delegate.CreateDelegate(typeof(TRes), jvmProxy, delRecord.JVMDelegateInvoke));
            }
            return((TRes)res);
        }
Ejemplo n.º 6
0
        public static TRes FullJ2C <TRes>(JNIEnv env, JniLocalHandle obj)
        {
            if (JniLocalHandle.IsNull(obj))
            {
                return(default(TRes));
            }
            Type reqType = typeof(TRes);

            if (reqType.IsPrimitive)
            {
                return((TRes)PrimJ2C(obj, env, reqType));
            }
#if DEBUG
            if (reqType.IsArray)
            {
                throw new InvalidOperationException("Call ArrayFullJ2C<TRes, TElem> instead");
            }
#endif
            Class clazz = env.GetObjectClass(obj);
            if (reqType == typeof(string) && clazz == String._class)
            {
                return((TRes)(object)StrongJp2CString(env, obj));
            }

            if (IClrProxy_._class.isAssignableFrom(clazz))
            {
                if (!reqType.IsInterface && typeof(IJvmProxy).IsAssignableFrom(reqType))
                {
                    //now we double wrap
                    return((TRes)__IClrProxy.CreateProxy(env, obj));
                }
                object res = __IClrProxy.GetObject(env, obj);
                if (res == null && Delegate_._class.isAssignableFrom(clazz))
                {
                    //that's delegate implemented in Java
                    RegistryRecord delRecord = Registry.GetJVMRecord(clazz);
                    IJvmProxy      jvmProxy  = delRecord.CreateCLRProxy(env, obj);
                    Delegate       del       = Delegate.CreateDelegate(delRecord.CLRInterface, jvmProxy, delRecord.JVMDelegateInvoke);
                    return((TRes)(object)del);
                }
                if (Bridge.Setup.Debug)
                {
                    Type realType = res.GetType();
                    if (!reqType.IsAssignableFrom(realType))
                    {
                        throw new InvalidCastException("Can't cast CLR instance" + realType + " to " + reqType);
                    }
                }
                return((TRes)res);
            }

            //now we deal only with JVM instances
            RegistryRecord record = Registry.GetJVMRecord(clazz);
            if (reqType.IsAssignableFrom(record.CLRInterface))
            {
                return((TRes)record.CreateCLRProxy(env, obj));
            }
            record = Registry.GetCLRRecord(reqType);
            if (Bridge.Setup.Debug)
            {
                if (!record.JVMInterface.isAssignableFrom(clazz))
                {
                    throw new InvalidCastException("Can't cast JVM instance" + clazz + " to " + reqType);
                }
            }
            return((TRes)record.CreateCLRProxy(env, obj));
        }
Ejemplo n.º 7
0
        public static JniHandle FullC2J <TBoth>(JNIEnv env, TBoth obj)
        {
            // ReSharper disable CompareNonConstrainedGenericWithNull
            if (obj == null)
            {
                return(JniLocalHandle.Zero);
            }
            // ReSharper restore CompareNonConstrainedGenericWithNull
            Type reqType = typeof(TBoth);

            if (reqType.IsPrimitive)
            {
                return(PrimC2J(env, obj, reqType));
            }
#if DEBUG
            if (reqType.IsArray)
            {
                throw new InvalidOperationException("Call ArrayFullJ2C<TRes, TElem> instead");
            }
#endif
            IJvmProxy proxy;
            Delegate  del = obj as Delegate;
            if (del != null)
            {
                proxy = del.Target as IJvmProxy;
            }
            else
            {
                proxy = obj as IJvmProxy;
            }

            if (proxy != null)
            {
                if (!reqType.IsInterface && !typeof(IJvmProxy).IsAssignableFrom(reqType))
                {
                    //now we do double wrap
                    RegistryRecord recordW = Registry.GetCLRRecord(typeof(IJvmProxy));
                    return(recordW.CreateJVMProxy(env, obj));
                }
                if (Bridge.Setup.Debug)
                {
                    Class          clazzT  = env.GetObjectClass(proxy.JvmHandle);
                    RegistryRecord recordT = Registry.GetCLRRecord(reqType);
                    if (!recordT.JVMInterface.isAssignableFrom(clazzT))
                    {
                        throw new InvalidCastException("Can't cast JVM instance " + clazzT + " to " + reqType);
                    }
                }
                return(proxy.JvmHandle);
            }

            Type realType = obj.GetType();
            if (reqType == typeof(String) && realType == typeof(string))
            {
                return(StrongC2JString(env, (string)(object)obj));
            }

            //Now we deal only with CLR instances
            RegistryRecord record = Registry.GetCLRRecord(realType);
            if (reqType.IsAssignableFrom(record.CLRInterface))
            {
                return(record.CreateJVMProxy(env, obj));
            }
            record = Registry.GetCLRRecord(reqType);
            if (Bridge.Setup.Debug)
            {
                if (!record.CLRInterface.IsAssignableFrom(realType))
                {
                    throw new InvalidCastException("Can't cast JVM instance" + realType + " to " + reqType);
                }
            }
            return(record.CreateJVMProxy(env, obj));
        }