Example #1
0
        public static TRes Cast <TRes>(object obj)
        {
            Type reqType = typeof(TRes);

            if (IsCLRInstance(obj))
            {
                throw new JNIException("Can't cast CLR instance of " + obj.GetType() + " to " + reqType);
            }
            IJvmProxy proxy = obj as IJvmProxy;

            if (!reqType.IsInterface && !typeof(IObject).IsAssignableFrom(reqType))
            {
                throw new JNIException("Can't cast to CLR class");
            }
            RegistryRecord record = Registry.GetCLRRecord(reqType);
            Class          clazz  = proxy.getClass();

            if (!record.JVMInterface.isAssignableFrom(clazz))
            {
                throw new InvalidCastException("Can't cast JVM instance of " + clazz + " to " + record.JVMInterface + "\n (" + clazz.getClassLoader() + "->" + record.JVMInterface.getClassLoader() + ")");
            }
            IJvmProxy res = record.CopyCLRProxy(JNIEnv.ThreadEnv, proxy.JvmHandle);

            return((TRes)res);
        }
Example #2
0
        public void SetField <T>(IJvmProxy obj, string fieldName, string sig, T value)
        {
            FieldId id = GetFieldID(obj.getClass(), fieldName, sig);

            if (id != null)
            {
                if (typeof(IObject).IsAssignableFrom(typeof(T)))
                {
                    SetObjectField(obj, id, (Object)(object)value);
                }
                else if (typeof(string).IsAssignableFrom(typeof(T)))
                {
                    SetObjectField(obj, id, (String)(string)(object)value);
                }
                else if (typeof(int).IsAssignableFrom(typeof(T)))
                {
                    SetIntField(obj, id, (int)(object)value);
                }
                else if (typeof(bool).IsAssignableFrom(typeof(T)))
                {
                    SetBooleanField(obj, id, (bool)(object)value);
                }
                else if (typeof(long).IsAssignableFrom(typeof(T)))
                {
                    SetLongField(obj, id, (long)(object)value);
                }
                else if (typeof(double).IsAssignableFrom(typeof(T)))
                {
                    SetDoubleField(obj, id, (double)(object)value);
                }
                else if (typeof(byte).IsAssignableFrom(typeof(T)))
                {
                    SetByteField(obj, id, (byte)(object)value);
                }
                else if (typeof(short).IsAssignableFrom(typeof(T)))
                {
                    SetShortField(obj, id, (short)(object)value);
                }
                else if (typeof(float).IsAssignableFrom(typeof(T)))
                {
                    SetFloatField(obj, id, (float)(object)id);
                }
                else if (typeof(char).IsAssignableFrom(typeof(T)))
                {
                    SetCharField(obj, id, (char)(object)id);
                }
                else if (typeof(byte).IsAssignableFrom(typeof(T)))
                {
                    SetByteField(obj, id, (byte)(object)id);
                }
                else
                {
                    throw new NotImplementedException();
                }
                return;
            }
            throw new ArgumentException();
        }
Example #3
0
        public TRes GetField <TRes>(IJvmProxy obj, string fieldName, string sig)
        {
            FieldId id = GetFieldID(obj.getClass(), fieldName, sig);

            if (id != null)
            {
                if (typeof(IObject).IsAssignableFrom(typeof(TRes)))
                {
                    return(GetObjectField <TRes>(obj, id));
                }
                if (typeof(string).IsAssignableFrom(typeof(TRes)))
                {
                    return(GetObjectField <TRes>(obj, id));
                }
                if (typeof(int).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetIntField(obj, id));
                }
                if (typeof(bool).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetBooleanField(obj, id));
                }
                if (typeof(long).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetLongField(obj, id));
                }
                if (typeof(double).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetDoubleField(obj, id));
                }
                if (typeof(byte).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetByteField(obj, id));
                }
                if (typeof(short).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetShortField(obj, id));
                }
                if (typeof(float).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetFloatField(obj, id));
                }
                if (typeof(char).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetCharField(obj, id));
                }
                if (typeof(byte).IsAssignableFrom(typeof(TRes)))
                {
                    return((TRes)(object)GetByteField(obj, id));
                }
                throw new NotImplementedException();
            }
            throw new ArgumentException();
        }