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

Bump to dotnet/java-interop/main@7a058c0e #9066

Merged
merged 4 commits into from
Jul 8, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Mono.Android/Android.Runtime/JNIEnvInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal struct JnienvInitializeArgs {
internal static IntPtr java_class_loader;
internal static JniMethodInfo? mid_Class_forName;

static AndroidRuntime? androidRuntime;
internal static AndroidRuntime? androidRuntime;

[UnmanagedCallersOnly]
static unsafe void RegisterJniNatives (IntPtr typeName_ptr, int typeName_len, IntPtr jniClass, IntPtr methods_ptr, int methods_len)
Expand Down
22 changes: 21 additions & 1 deletion src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership

[UnconditionalSuppressMessage ("Trimming", "IL2067", Justification = "TypeManager.CreateProxy() does not statically know the value of the 'type' local variable.")]
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = "TypeManager.CreateProxy() does not statically know the value of the 'type' local variable.")]
internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership transfer, Type? targetType)
internal static IJavaPeerable? CreateInstance (IntPtr handle, JniHandleOwnership transfer, Type? targetType)
{
Type? type = null;
IntPtr class_ptr = JNIEnv.GetObjectClass (handle);
Expand Down Expand Up @@ -318,6 +318,26 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership
type = invokerType;
}

var typeSig = JNIEnvInit.androidRuntime?.TypeManager.GetTypeSignature (type) ?? default;
if (!typeSig.IsValid || typeSig.SimpleReference == null) {
throw new ArgumentException ($"Could not determine Java type corresponding to `{type.AssemblyQualifiedName}`.", nameof (targetType));
}
JniObjectReference typeClass;
try {
typeClass = JniEnvironment.Types.FindClass (typeSig.SimpleReference);
} catch (Exception e) {
throw new ArgumentException ($"Could not find Java class `{typeSig.SimpleReference}`.",
nameof (targetType),
e);
}

var handleClass = JniEnvironment.Types.GetObjectClass (new JniObjectReference (handle));
if (!JniEnvironment.Types.IsAssignableFrom (handleClass, typeClass)) {
Logger.Log (LogLevel.Info, "*jonp*", $"# jonp: can't assign `{GetClassName(handleClass.Handle)}` to `{GetClassName(typeClass.Handle)}`");
JniObjectReference.Dispose (ref handleClass);
JniObjectReference.Dispose (ref typeClass);
return null;
}

IJavaPeerable? result = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public void JavaCast_CheckForManagedSubclasses ()
}
}

[Test]
public void JavaAs ()
{
using var v = new Java.InteropTests.MyJavaInterfaceImpl ();
using var c = v.JavaAs<Java.Lang.ICloneable>();
Assert.IsNotNull (c);
}

static Java.Lang.Object CreateObject ()
{
var ctor = JNIEnv.GetMethodID (Java.Lang.Class.Object, "<init>", "()V");
Expand Down
Loading