diff --git a/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs b/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs index 23bbae730..1f9391e9c 100644 --- a/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs +++ b/src/Java.Interop/Java.Interop/JniEnvironment.Types.cs @@ -24,11 +24,15 @@ static partial class Types { }; static readonly JniMethodInfo Class_getName; + static readonly JniMethodInfo Class_forName; + static readonly JniObjectReference Class_reference; static Types () { using (var t = new JniType ("java/lang/Class")) { + Class_reference = t.PeerReference.NewGlobalRef (); Class_getName = t.GetInstanceMethod ("getName", "()Ljava/lang/String;"); + Class_forName = t.GetStaticMethod ("forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"); } } @@ -57,12 +61,14 @@ static unsafe JniObjectReference TryFindClass (string classname, bool throwOnErr LogCreateLocalRef (findClassThrown); var pendingException = info.Runtime.GetExceptionForThrowable (ref findClassThrown, JniObjectReferenceOptions.CopyAndDispose); - if (info.Runtime.ClassLoader_LoadClass != null) { + if (Class_forName.IsValid) { var java = info.ToJavaName (classname); - var __args = stackalloc JniArgumentValue [1]; + var __args = stackalloc JniArgumentValue [3]; __args [0] = new JniArgumentValue (java); + __args [1] = new JniArgumentValue (true); // initialize the class + __args [2] = new JniArgumentValue (info.Runtime.ClassLoader); - c = RawCallObjectMethodA (info.EnvironmentPointer, out thrown, info.Runtime.ClassLoader.Handle, info.Runtime.ClassLoader_LoadClass.ID, (IntPtr) __args); + c = RawCallStaticObjectMethodA (info.EnvironmentPointer, out thrown, Class_reference.Handle, Class_forName.ID, (IntPtr) __args); JniObjectReference.Dispose (ref java); if (thrown == IntPtr.Zero) { (pendingException as IJavaPeerable)?.Dispose (); @@ -169,12 +175,12 @@ static void RawExceptionClear (IntPtr env) #endif // FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS } - static IntPtr RawCallObjectMethodA (IntPtr env, out IntPtr thrown, IntPtr instance, IntPtr jmethodID, IntPtr args) + static IntPtr RawCallStaticObjectMethodA (IntPtr env, out IntPtr thrown, IntPtr clazz, IntPtr jmethodID, IntPtr args) { #if FEATURE_JNIENVIRONMENT_JI_PINVOKES - return NativeMethods.java_interop_jnienv_call_object_method_a (env, out thrown, instance, jmethodID, args); + return NativeMethods.java_interop_jnienv_call_static_object_method_a (env, out thrown, clazz, instance, jmethodID, args); #elif FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS - var r = JniNativeMethods.CallObjectMethodA (env, instance, jmethodID, args); + var r = JniNativeMethods.CallStaticObjectMethodA (env, clazz, jmethodID, args); thrown = JniNativeMethods.ExceptionOccurred (env); return r; #else // FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS diff --git a/src/Java.Interop/Java.Interop/JniRuntime.cs b/src/Java.Interop/Java.Interop/JniRuntime.cs index 6af5e5c1a..4f53620e4 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.cs @@ -59,6 +59,7 @@ public partial class CreationOptions { public IntPtr EnvironmentPointer {get; set;} public JniObjectReference ClassLoader {get; set;} + [Obsolete ("No longer supported; Class.forName() is now used instead")] public IntPtr ClassLoader_LoadClass_id {get; set;} public JniObjectReferenceManager? ObjectReferenceManager {get; set;} @@ -152,7 +153,6 @@ public static void SetCurrent (JniRuntime newCurrent) bool DestroyRuntimeOnDispose; internal JniObjectReference ClassLoader; - internal JniMethodInfo? ClassLoader_LoadClass; public IntPtr InvocationPointer {get; private set;} @@ -206,25 +206,14 @@ protected JniRuntime (CreationOptions options) JniEnvironment.SetEnvironmentInfo (env); ClassLoader = options.ClassLoader; - if (options.ClassLoader_LoadClass_id != IntPtr.Zero) { - ClassLoader_LoadClass = new JniMethodInfo (options.ClassLoader_LoadClass_id, isStatic: false); - } - if (ClassLoader.IsValid) { ClassLoader = ClassLoader.NewGlobalRef (); - } - - if (!ClassLoader.IsValid || ClassLoader_LoadClass == null) { + } else { using (var t = new JniType ("java/lang/ClassLoader")) { - if (!ClassLoader.IsValid) { - var m = t.GetStaticMethod ("getSystemClassLoader", "()Ljava/lang/ClassLoader;"); - var loader = JniEnvironment.StaticMethods.CallStaticObjectMethod (t.PeerReference, m); - ClassLoader = loader.NewGlobalRef (); - JniObjectReference.Dispose (ref loader); - } - if (ClassLoader_LoadClass == null) { - ClassLoader_LoadClass = t.GetInstanceMethod ("loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); - } + var m = t.GetStaticMethod ("getSystemClassLoader", "()Ljava/lang/ClassLoader;"); + var loader = JniEnvironment.StaticMethods.CallStaticObjectMethod (t.PeerReference, m); + ClassLoader = loader.NewGlobalRef (); + JniObjectReference.Dispose (ref loader); } }