예제 #1
0
        internal static Exception?GetExceptionForLastThrowable()
        {
            var e = JniEnvironment.Exceptions.ExceptionOccurred();

            if (!e.IsValid)
            {
                return(null);
            }
            // JniEnvironment.Errors.ExceptionDescribe ();
            JniEnvironment.Exceptions.ExceptionClear();
            JniEnvironment.LogCreateLocalRef(e);
            return(Runtime.GetExceptionForThrowable(ref e, JniObjectReferenceOptions.CopyAndDispose));
        }
예제 #2
0
        internal static Exception?GetExceptionForLastThrowable(IntPtr thrown)
        {
            if (thrown == IntPtr.Zero)
            {
                return(null);
            }
            var e = new JniObjectReference(thrown, JniObjectReferenceType.Local);

            // JniEnvironment.Errors.ExceptionDescribe ();
            JniEnvironment.Exceptions.ExceptionClear();
            JniEnvironment.LogCreateLocalRef(e);
            return(Runtime.GetExceptionForThrowable(ref e, JniObjectReferenceOptions.CopyAndDispose));
        }
예제 #3
0
            public static unsafe JniObjectReference FindClass(string classname)
            {
                if (classname == null)
                {
                    throw new ArgumentNullException("classname");
                }
                if (classname.Length == 0)
                {
                    throw new ArgumentException("'classname' cannot be a zero-length string.", nameof(classname));
                }

                var info = JniEnvironment.CurrentInfo;

#if FEATURE_JNIENVIRONMENT_JI_PINVOKES
                IntPtr thrown;
                var    c = NativeMethods.java_interop_jnienv_find_class(info.EnvironmentPointer, out thrown, classname);
                if (thrown == IntPtr.Zero)
                {
                    var r = new JniObjectReference(c, JniObjectReferenceType.Local);
                    JniEnvironment.LogCreateLocalRef(r);
                    return(r);
                }
                NativeMethods.java_interop_jnienv_exception_clear(info.EnvironmentPointer);
                var e = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                LogCreateLocalRef(e);

                var java   = info.ToJavaName(classname);
                var __args = stackalloc JniArgumentValue [1];
                __args [0] = new JniArgumentValue(java);

                IntPtr ignoreThrown;
                c = NativeMethods.java_interop_jnienv_call_object_method_a(info.EnvironmentPointer, out ignoreThrown, info.Runtime.ClassLoader.Handle, info.Runtime.ClassLoader_LoadClass.ID, (IntPtr)__args);
                JniObjectReference.Dispose(ref java);
                if (ignoreThrown == IntPtr.Zero)
                {
                    JniObjectReference.Dispose(ref e);
                    var r = new JniObjectReference(c, JniObjectReferenceType.Local);
                    JniEnvironment.LogCreateLocalRef(r);
                    return(r);
                }
                NativeMethods.java_interop_jnienv_exception_clear(info.EnvironmentPointer);
                NativeMethods.java_interop_jnienv_delete_local_ref(info.EnvironmentPointer, ignoreThrown);
                throw info.Runtime.GetExceptionForThrowable(ref e, JniObjectReferenceOptions.CopyAndDispose);
#endif  // !FEATURE_JNIENVIRONMENT_JI_PINVOKES
#if FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
                var c      = info.Invoker.FindClass(info.EnvironmentPointer, classname);
                var thrown = info.Invoker.ExceptionOccurred(info.EnvironmentPointer);
                if (thrown.IsInvalid)
                {
                    JniEnvironment.LogCreateLocalRef(c);
                    return(new JniObjectReference(c, JniObjectReferenceType.Local));
                }
                info.Invoker.ExceptionClear(info.EnvironmentPointer);
                LogCreateLocalRef(thrown);

                var java   = info.ToJavaName(classname);
                var __args = stackalloc JniArgumentValue [1];
                __args [0] = new JniArgumentValue(java);

                c = info.Invoker.CallObjectMethodA(info.EnvironmentPointer, info.Runtime.ClassLoader.SafeHandle, info.Runtime.ClassLoader_LoadClass.ID, __args);
                JniObjectReference.Dispose(ref java);
                var ignoreThrown = info.Invoker.ExceptionOccurred(info.EnvironmentPointer);
                if (ignoreThrown.IsInvalid)
                {
                    thrown.Dispose();
                    JniEnvironment.LogCreateLocalRef(c);
                    return(new JniObjectReference(c, JniObjectReferenceType.Local));
                }
                info.Invoker.ExceptionClear(info.EnvironmentPointer);
                LogCreateLocalRef(ignoreThrown);
                ignoreThrown.Dispose();
                var e = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                throw info.Runtime.GetExceptionForThrowable(ref e, JniObjectReferenceOptions.CopyAndDispose);
#endif  // !FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
            }
예제 #4
0
            public static unsafe JniObjectReference FindClass(string classname)
            {
                if (classname == null)
                {
                    throw new ArgumentNullException(nameof(classname));
                }
                if (classname.Length == 0)
                {
                    throw new ArgumentException("'classname' cannot be a zero-length string.", nameof(classname));
                }

                var info = JniEnvironment.CurrentInfo;

#if FEATURE_JNIENVIRONMENT_JI_PINVOKES
                IntPtr thrown;
                var    c = NativeMethods.java_interop_jnienv_find_class(info.EnvironmentPointer, out thrown, classname);
                if (thrown == IntPtr.Zero)
                {
                    var r = new JniObjectReference(c, JniObjectReferenceType.Local);
                    JniEnvironment.LogCreateLocalRef(r);
                    return(r);
                }

                // If the Java-side exception stack trace is *lost* a'la 89a5a229,
                // change `false` to `true` and rebuild+re-run.
#if false
                NativeMethods.java_interop_jnienv_exception_describe(info.EnvironmentPointer);
#endif

                NativeMethods.java_interop_jnienv_exception_clear(info.EnvironmentPointer);

                var findClassThrown = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                LogCreateLocalRef(findClassThrown);
                var pendingException = info.Runtime.GetExceptionForThrowable(ref findClassThrown, JniObjectReferenceOptions.CopyAndDispose);

                if (info.Runtime.ClassLoader_LoadClass != null)
                {
                    var java   = info.ToJavaName(classname);
                    var __args = stackalloc JniArgumentValue [1];
                    __args [0] = new JniArgumentValue(java);

                    c = NativeMethods.java_interop_jnienv_call_object_method_a(info.EnvironmentPointer, out thrown, info.Runtime.ClassLoader.Handle, info.Runtime.ClassLoader_LoadClass.ID, (IntPtr)__args);
                    JniObjectReference.Dispose(ref java);
                    if (thrown == IntPtr.Zero)
                    {
                        (pendingException as IJavaPeerable)?.Dispose();
                        var r = new JniObjectReference(c, JniObjectReferenceType.Local);
                        JniEnvironment.LogCreateLocalRef(r);
                        return(r);
                    }
                    NativeMethods.java_interop_jnienv_exception_clear(info.EnvironmentPointer);

                    if (pendingException != null)
                    {
                        NativeMethods.java_interop_jnienv_delete_local_ref(info.EnvironmentPointer, thrown);
                    }
                    else
                    {
                        var loadClassThrown = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                        LogCreateLocalRef(loadClassThrown);
                        pendingException = info.Runtime.GetExceptionForThrowable(ref loadClassThrown, JniObjectReferenceOptions.CopyAndDispose);
                    }
                }

                throw pendingException !;
#endif  // !FEATURE_JNIENVIRONMENT_JI_PINVOKES
#if FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
                var c      = info.Invoker.FindClass(info.EnvironmentPointer, classname);
                var thrown = info.Invoker.ExceptionOccurred(info.EnvironmentPointer);
                if (thrown.IsInvalid)
                {
                    JniEnvironment.LogCreateLocalRef(c);
                    return(new JniObjectReference(c, JniObjectReferenceType.Local));
                }
                info.Invoker.ExceptionClear(info.EnvironmentPointer);
                var findClassThrown = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                LogCreateLocalRef(findClassThrown);
                var pendingException = info.Runtime.GetExceptionForThrowable(ref findClassThrown, JniObjectReferenceOptions.CopyAndDispose);

                var java   = info.ToJavaName(classname);
                var __args = stackalloc JniArgumentValue [1];
                __args [0] = new JniArgumentValue(java);

                c = info.Invoker.CallObjectMethodA(info.EnvironmentPointer, info.Runtime.ClassLoader.SafeHandle, info.Runtime.ClassLoader_LoadClass.ID, __args);
                JniObjectReference.Dispose(ref java);
                thrown = info.Invoker.ExceptionOccurred(info.EnvironmentPointer);
                if (ignoreThrown.IsInvalid)
                {
                    (pendingException as IJavaPeerable)?.Dispose();
                    var r = new JniObjectReference(c, JniObjectReferenceType.Local);
                    JniEnvironment.LogCreateLocalRef(r);
                    return(r);
                }
                info.Invoker.ExceptionClear(info.EnvironmentPointer);
                if (pendingException != null)
                {
                    thrown.Dispose();
                    throw pendingException;
                }
                var loadClassThrown = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                LogCreateLocalRef(loadClassThrown);
                pendingException = info.Runtime.GetExceptionForThrowable(ref loadClassThrown, JniObjectReferenceOptions.CopyAndDispose);
                throw pendingException !;
#endif  // !FEATURE_JNIOBJECTREFERENCE_SAFEHANDLES
            }