Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java.Interop] Use Class.forName() as fallback to load Java classes #1326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Java.Interop/Java.Interop/JniEnvironment.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
}
}

Expand Down Expand Up @@ -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 ();
Expand Down Expand Up @@ -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
Expand Down
23 changes: 6 additions & 17 deletions src/Java.Interop/Java.Interop/JniRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
Expand Down Expand Up @@ -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;}

Expand Down Expand Up @@ -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);
}
}

Expand Down