Exemplo n.º 1
0
        public override object CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
        {
            var jvm = JniEnvironment.Runtime;

            if (targetType == null || targetType == typeof(object))
            {
                targetType = jvm.ValueManager.GetRuntimeType(reference);
            }
            if (targetType != null)
            {
                var vm = jvm.ValueManager.GetValueMarshaler(targetType);
                if (vm != Instance)
                {
                    return(vm.CreateValue(ref reference, options, targetType));
                }
            }

            var target = jvm.ValueManager.PeekValue(reference);

            if (target != null)
            {
                JniObjectReference.Dispose(ref reference, options);
                return(target);
            }
            // Punt! Hope it's a java.lang.Object
            return(jvm.ValueManager.CreatePeer(ref reference, options, targetType));
        }
Exemplo n.º 2
0
            public object GetValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
            {
                if (!reference.IsValid)
                {
                    return(null);
                }

                var existing = PeekValue(reference);

                if (existing != null && (targetType == null || targetType.GetTypeInfo().IsAssignableFrom(existing.GetType().GetTypeInfo())))
                {
                    JniObjectReference.Dispose(ref reference, options);
                    return(existing);
                }

                if (targetType != null && typeof(IJavaPeerable).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo()))
                {
                    return(JavaPeerableValueMarshaler.Instance.CreateGenericValue(ref reference, options, targetType));
                }

                targetType = targetType ?? GetRuntimeType(reference);
                if (targetType == null)
                {
                    // Let's hope this is an IJavaPeerable!
                    return(JavaPeerableValueMarshaler.Instance.CreateGenericValue(ref reference, options, targetType));
                }
                var marshaler = GetValueMarshaler(targetType);

                return(marshaler.CreateValue(ref reference, options, targetType));
            }
Exemplo n.º 3
0
        internal static object CreateProxy(Type type, IntPtr handle, JniHandleOwnership transfer)
        {
            // Skip Activator.CreateInstance() as that requires public constructors,
            // and we want to hide some constructors for sanity reasons.
            BindingFlags    flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            ConstructorInfo c     = type.GetConstructor(flags, null, XAConstructorSignature, null);

            if (c != null)
            {
                return(c.Invoke(new object [] { handle, transfer }));
            }
            c = type.GetConstructor(flags, null, JIConstructorSignature, null);
            if (c != null)
            {
                JniObjectReference        r = new JniObjectReference(handle);
                JniObjectReferenceOptions o = JniObjectReferenceOptions.Copy;
                var peer = (IJavaPeerable)c.Invoke(new object [] { r, o });
                JNIEnv.DeleteRef(handle, transfer);
                peer.SetJniManagedPeerState(peer.JniManagedPeerState | JniManagedPeerStates.Replaceable);
                return(peer);
            }
            throw new MissingMethodException(
                      "No constructor found for " + type.FullName + "::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)",
                      CreateJavaLocationException());
        }
Exemplo n.º 4
0
 public static unsafe T GetObject <T>(
     ref JniObjectReference jobj,
     JniObjectReferenceOptions options)
     where T : class, IJavaPeerable
 {
     return(!jobj.IsValid ? default : (T)GetObject(ref jobj, options, typeof(T)));
 }
Exemplo n.º 5
0
            public object CreateValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
            {
                if (!reference.IsValid)
                {
                    return(null);
                }

                if (targetType != null && typeof(IJavaPeerable).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo()))
                {
                    return(JavaPeerableValueMarshaler.Instance.CreateGenericValue(ref reference, options, targetType));
                }

                var boxed = PeekBoxedObject(reference);

                if (boxed != null)
                {
                    JniObjectReference.Dispose(ref reference, options);
                    if (targetType != null)
                    {
                        return(Convert.ChangeType(boxed, targetType));
                    }
                    return(boxed);
                }

                targetType = targetType ?? GetRuntimeType(reference);
                if (targetType == null)
                {
                    // Let's hope this is an IJavaPeerable!
                    return(JavaPeerableValueMarshaler.Instance.CreateGenericValue(ref reference, options, targetType));
                }
                var marshaler = GetValueMarshaler(targetType);

                return(marshaler.CreateValue(ref reference, options, targetType));
            }
 public override object CreateValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
 {
     if (!reference.IsValid)
     {
         return(null);
     }
     return(CreateGenericValue(ref reference, options, targetType));
 }
Exemplo n.º 7
0
 public JavaException(ref JniObjectReference reference, JniObjectReferenceOptions transfer)
     : base(_GetMessage(ref reference, transfer), _GetCause(ref reference, transfer))
 {
     Construct(ref reference, transfer);
     if (PeerReference.IsValid)
     {
         javaStackTrace = _GetJavaStack(PeerReference);
     }
 }
Exemplo n.º 8
0
        public override Char CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type?targetType)
        {
            if (!reference.IsValid)
            {
                return(default(Char));
            }

            return(JniCharacter.GetValueFromJni(ref reference, options, targetType));
        }
        public override Boolean CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
        {
            if (!reference.IsValid)
            {
                return(default(Boolean));
            }

            return(JniBoolean.GetValueFromJni(ref reference, options, targetType));
        }
Exemplo n.º 10
0
        public override Int32 CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
        {
            if (!reference.IsValid)
            {
                return(default(Int32));
            }

            return(JniInteger.GetValueFromJni(ref reference, options, targetType));
        }
Exemplo n.º 11
0
        public override Int64?CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
        {
            if (!reference.IsValid)
            {
                return(null);
            }

            return(JniLong.GetValueFromJni(ref reference, options, targetType: null));
        }
Exemplo n.º 12
0
        public override Single CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
        {
            if (!reference.IsValid)
            {
                return(default(Single));
            }

            return(JniFloat.GetValueFromJni(ref reference, options, targetType));
        }
Exemplo n.º 13
0
 internal static Char GetValueFromJni(ref JniObjectReference self, JniObjectReferenceOptions transfer, Type?targetType)
 {
     Debug.Assert(targetType == null || targetType == typeof(Char), "Expected targetType==typeof(Char); was: " + targetType);
     TypeRef.GetCachedInstanceMethod(ref charValue, "charValue", "()C");
     try {
         return(JniEnvironment.InstanceMethods.CallCharMethod(self, charValue));
     } finally {
         JniObjectReference.Dispose(ref self, transfer);
     }
 }
Exemplo n.º 14
0
 internal static Int64 GetValueFromJni(ref JniObjectReference self, JniObjectReferenceOptions transfer, Type targetType)
 {
     Debug.Assert(targetType == null || targetType == typeof(Int64), "Expected targetType==typeof(Int64); was: " + targetType);
     TypeRef.GetCachedInstanceMethod(ref longValue, "longValue", "()J");
     try {
         return(JniEnvironment.InstanceMethods.CallLongMethod(self, longValue));
     } finally {
         JniObjectReference.Dispose(ref self, transfer);
     }
 }
Exemplo n.º 15
0
 internal static Boolean GetValueFromJni(ref JniObjectReference self, JniObjectReferenceOptions transfer, Type targetType)
 {
     Debug.Assert(targetType == null || targetType == typeof(Boolean), "Expected targetType==typeof(Boolean); was: " + targetType);
     TypeRef.GetCachedInstanceMethod(ref booleanValue, "booleanValue", "()Z");
     try {
         return(JniEnvironment.InstanceMethods.CallBooleanMethod(self, booleanValue));
     } finally {
         JniObjectReference.Dispose(ref self, transfer);
     }
 }
Exemplo n.º 16
0
 internal static Single GetValueFromJni(ref JniObjectReference self, JniObjectReferenceOptions transfer, Type targetType)
 {
     Debug.Assert(targetType == null || targetType == typeof(Single), "Expected targetType==typeof(Single); was: " + targetType);
     TypeRef.GetCachedInstanceMethod(ref floatValue, "floatValue", "()F");
     try {
         return(JniEnvironment.InstanceMethods.CallFloatMethod(self, floatValue));
     } finally {
         JniObjectReference.Dispose(ref self, transfer);
     }
 }
Exemplo n.º 17
0
 internal static IJavaPeerable GetObject(
     ref JniObjectReference jobj,
     JniObjectReferenceOptions options,
     Type targetType = null)
 {
     if (!jobj.IsValid)
     {
         return(null);
     }
     return(JniRuntime.CurrentRuntime.ValueManager.GetValue <IJavaPeerable>(ref jobj, options, targetType));
 }
Exemplo n.º 18
0
 void Initialize(ref JniObjectReference peerReference, JniObjectReferenceOptions transfer)
 {
     if (!peerReference.IsValid)
     {
         throw new ArgumentException("handle must be valid.", nameof(peerReference));
     }
     try {
         this.peerReference = peerReference.NewGlobalRef();
     } finally {
         JniObjectReference.Dispose(ref peerReference, transfer);
     }
 }
Exemplo n.º 19
0
        public override IJavaPeerable?CreatePeer(ref JniObjectReference reference, JniObjectReferenceOptions options, Type?targetType)
        {
            if (!reference.IsValid)
            {
                return(null);
            }

            var peer = Java.Interop.TypeManager.CreateInstance(reference.Handle, JniHandleOwnership.DoNotTransfer, targetType) as IJavaPeerable;

            JniObjectReference.Dispose(ref reference, options);
            return(peer);
        }
Exemplo n.º 20
0
        static Exception _GetCause(ref JniObjectReference reference, JniObjectReferenceOptions transfer)
        {
            if (transfer == JniObjectReferenceOptions.None)
            {
                return(null);
            }

            var m = _members.InstanceMethods.GetMethodInfo("getCause.()Ljava/lang/Throwable;");
            var e = JniEnvironment.InstanceMethods.CallObjectMethod(reference, m);

            return(JniEnvironment.Runtime.GetExceptionForThrowable(ref e, JniObjectReferenceOptions.CopyAndDispose));
        }
Exemplo n.º 21
0
        static string _GetMessage(ref JniObjectReference reference, JniObjectReferenceOptions transfer)
        {
            if (transfer == JniObjectReferenceOptions.None)
            {
                return(null);
            }

            var m = _members.InstanceMethods.GetMethodInfo("getMessage.()Ljava/lang/String;");
            var s = JniEnvironment.InstanceMethods.CallObjectMethod(reference, m);

            return(JniEnvironment.Strings.ToString(ref s, JniObjectReferenceOptions.CopyAndDispose));
        }
Exemplo n.º 22
0
        protected void SetPeerReference(ref JniObjectReference reference, JniObjectReferenceOptions options)
        {
            if (options == JniObjectReferenceOptions.None)
            {
                ((IJavaPeerable)this).SetPeerReference(new JniObjectReference());
                return;
            }

#if FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
            this.reference = reference;
#endif  // FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
#if FEATURE_JNIOBJECTREFERENCE_INTPTRS
            this.handle      = reference.Handle;
            this.handle_type = reference.Type;
#endif  // FEATURE_JNIOBJECTREFERENCE_INTPTRS

            JniObjectReference.Dispose(ref reference, options);
        }
Exemplo n.º 23
0
        public static void Dispose(ref JniObjectReference reference, JniObjectReferenceOptions options)
        {
            if (options == JniObjectReferenceOptions.None)
            {
                return;
            }

            if (!reference.IsValid)
            {
                return;
            }

            if ((options & DisposeSource) == 0)
            {
                return;
            }

            Dispose(ref reference);
        }
Exemplo n.º 24
0
            public T CreateValue <T> (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
            {
                if (disposed)
                {
                    throw new ObjectDisposedException(GetType().Name);
                }

                if (!reference.IsValid)
                {
                    return(default(T));
                }

                if (targetType != null && !typeof(T).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo()))
                {
                    throw new ArgumentException(
                              string.Format("Requested runtime '{0}' value of '{1}' is not compatible with requested compile-time type T of '{2}'.",
                                            nameof(targetType),
                                            targetType,
                                            typeof(T)),
                              nameof(targetType));
                }

                var boxed = PeekBoxedObject(reference);

                if (boxed != null)
                {
                    JniObjectReference.Dispose(ref reference, options);
                    return((T)Convert.ChangeType(boxed, targetType ?? typeof(T)));
                }

                targetType = targetType ?? typeof(T);

                if (typeof(IJavaPeerable).GetTypeInfo().IsAssignableFrom(targetType.GetTypeInfo()))
                {
                    return((T)JavaPeerableValueMarshaler.Instance.CreateGenericValue(ref reference, options, targetType));
                }

                var marshaler = GetValueMarshaler <T> ();

                return(marshaler.CreateGenericValue(ref reference, options, targetType));
            }
Exemplo n.º 25
0
 internal static object _GetArray <T>(ref JniObjectReference handle, JniObjectReferenceOptions options)
 {
     if (!handle.IsValid)
     {
         return(null);
     }
     if (typeof(T) == typeof(int))
     {
         return(new JavaInt32Array(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(sbyte))
     {
         return(new JavaSByteArray(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(double))
     {
         return(new JavaDoubleArray(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(float))
     {
         return(new JavaSingleArray(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(short))
     {
         return(new JavaInt16Array(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(long))
     {
         return(new JavaInt64Array(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(char))
     {
         return(new JavaCharArray(ref handle, options).ToArray());
     }
     else if (typeof(T) == typeof(bool))
     {
         return(new JavaBooleanArray(ref handle, options).ToArray());
     }
     return(new JavaObjectArray <T>(ref handle, options).ToArray());
 }
Exemplo n.º 26
0
 public ExportTest(ref JniObjectReference reference, JniObjectReferenceOptions transfer)
     : base(ref reference, transfer)
 {
 }
Exemplo n.º 27
0
 public override Exception GetExceptionForThrowable(ref JniObjectReference value, JniObjectReferenceOptions transfer)
 {
     var throwable = Java.Lang.Object.GetObject<Java.Lang.Throwable>(value.Handle, JniHandleOwnership.DoNotTransfer);
     JniObjectReference.Dispose (ref value, transfer);
     var p = throwable as JavaProxyThrowable;
     if (p != null)
         return p.InnerException;
     return throwable;
 }
Exemplo n.º 28
0
 public override MyColor CreateGenericValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 29
0
 protected SomeObject(ref JniObjectReference reference, JniObjectReferenceOptions options) : base(ref reference, options)
 {
 }
Exemplo n.º 30
0
        public override Exception?GetExceptionForThrowable(ref JniObjectReference reference, JniObjectReferenceOptions options)
        {
            if (!reference.IsValid)
            {
                return(null);
            }
            var peeked    = JNIEnv.AndroidValueManager?.PeekPeer(reference);
            var peekedExc = peeked as Exception;

            if (peekedExc == null)
            {
                var throwable = Java.Lang.Object.GetObject <Java.Lang.Throwable> (reference.Handle, JniHandleOwnership.DoNotTransfer);
                JniObjectReference.Dispose(ref reference, options);
                return(throwable);
            }
            JniObjectReference.Dispose(ref reference, options);
            var unwrapped = JNIEnv.AndroidValueManager?.UnboxException(peeked !);

            if (unwrapped != null)
            {
                return(unwrapped);
            }
            return(peekedExc);
        }
Exemplo n.º 31
0
 public abstract object                  CreateValue(ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null);