Exemplo n.º 1
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));
        }