Skip to content

Commit 137214a

Browse files
committed
Fixity Fix
dotnet/java-interop#1302 fixed the `Could not determine Java type corresponding to System.Byte[]` crash. What replaced it was: Mono.Android.NET_Tests, Java.InteropTests.JnienvTest.NewObjectArrayWithNonJavaType / Release System.ArgumentException : Could not determine Java type corresponding to System.Type, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e. Arg_ParamName_Name, type The deal is that [`JavaNativeTypeManager.ToJniName(Type)`][0] defaults to using `java/lang/Object` if there is no type mapping. This is "reasonable" because of the "auto-box into `Android.Runtime.JavaObject`" behavior, meaning it *is* possible to create a Java array that "holds" `System.Type` instances. (Because the Java-side array is actually a `java.lang.Object[]`, each instance of which is an `Android.Runtime.JavaObject`, which in turn holds the `System.Type` instance.) However, this is *not* a semantic that `JniRuntime.JniTypeManager.GetTypeSignature()` maintains, and thus this was lost in 1b1f145. Update `JNIEnv.FindClass(Type)` so that if `.GetTypeSignature()` can't find the typemapping, default to `java/lang/Object`. This allows all tests to pass, locally. [0]: https://github.com/dotnet/java-interop/blob/62635a3ffee4c9ec421eb029b86e61267a544f92/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs#L164-L168
1 parent 32e43f0 commit 137214a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,21 @@ public static unsafe IntPtr CreateInstance (Type type, string signature, params
254254

255255
public static IntPtr FindClass (System.Type type)
256256
{
257+
int rank = JavaNativeTypeManager.GetArrayInfo (type, out type);
257258
try {
258259
var sig = JNIEnvInit.androidRuntime?.TypeManager.GetTypeSignature (type) ?? default;
259260
if (!sig.IsValid || sig.SimpleReference == null) {
260-
throw new ArgumentException ($"Could not determine Java type corresponding to `{type.AssemblyQualifiedName}`.", nameof (type));
261+
sig = new JniTypeSignature ("java/lang/Object");
261262
}
263+
sig = sig.AddArrayRank (rank);
262264

263265
JniObjectReference local_ref = JniEnvironment.Types.FindClass (sig.Name);
264-
IntPtr global_ref = NewGlobalRef (local_ref.Handle);
266+
IntPtr global_ref = local_ref.NewGlobalRef ().Handle;
265267
JniObjectReference.Dispose (ref local_ref);
266268
return global_ref;
267269
} catch (Java.Lang.Throwable e) {
268270
if (!((e is Java.Lang.NoClassDefFoundError) || (e is Java.Lang.ClassNotFoundException)))
269271
throw;
270-
int rank = JavaNativeTypeManager.GetArrayInfo (type, out type);
271272
RuntimeNativeMethods.monodroid_log (LogLevel.Warn, LogCategories.Default, $"JNIEnv.FindClass(Type) caught unexpected exception: {e}");
272273
var jni = Java.Interop.TypeManager.GetJniTypeName (type);
273274
if (jni != null) {

0 commit comments

Comments
 (0)