Esempio n. 1
0
 public override int GetHashCode()
 {
     return(JniArgumentValue.GetHashCode());
 }
            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);
                }

                NativeMethods.java_interop_jnienv_exception_clear(info.EnvironmentPointer);
                var e = new JniObjectReference(thrown, JniObjectReferenceType.Local);
                LogCreateLocalRef(e);

                if (info.Runtime.ClassLoader_LoadClass != null)
                {
                    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
            }