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

[Mono.Android] handle exceptions in RegisterNativeMembers #6672

Merged
Merged
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
92 changes: 48 additions & 44 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,57 +378,61 @@ static bool CallRegisterMethodByIndex (JniNativeMethodRegistrationArguments argu

public override void RegisterNativeMembers (JniType nativeClass, Type type, string? methods)
{
if (FastRegisterNativeMembers (nativeClass, type, methods))
return;
try {
if (FastRegisterNativeMembers (nativeClass, type, methods))
return;

if (string.IsNullOrEmpty (methods)) {
if (jniAddNativeMethodRegistrationAttributePresent)
base.RegisterNativeMembers (nativeClass, type, methods);
return;
}
if (string.IsNullOrEmpty (methods)) {
if (jniAddNativeMethodRegistrationAttributePresent)
base.RegisterNativeMembers (nativeClass, type, methods);
return;
}

string[] members = methods!.Split ('\n');
if (members.Length < 2) {
if (jniAddNativeMethodRegistrationAttributePresent)
base.RegisterNativeMembers (nativeClass, type, methods);
return;
}
string[] members = methods!.Split ('\n');
if (members.Length < 2) {
if (jniAddNativeMethodRegistrationAttributePresent)
base.RegisterNativeMembers (nativeClass, type, methods);
return;
}

JniNativeMethodRegistration[] natives = new JniNativeMethodRegistration [members.Length-1];
for (int i = 0; i < members.Length; ++i) {
string method = members [i];
if (string.IsNullOrEmpty (method))
continue;
string[] toks = members [i].Split (new[]{':'}, 4);
Delegate callback;
if (toks [2] == "__export__") {
var mname = toks [0].Substring (2);
MethodInfo? minfo = null;
foreach (var mi in type.GetMethods (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
if (mi.Name == mname && JavaNativeTypeManager.GetJniSignature (mi) == toks [1]) {
minfo = mi;
break;
JniNativeMethodRegistration[] natives = new JniNativeMethodRegistration [members.Length-1];
for (int i = 0; i < members.Length; ++i) {
string method = members [i];
if (string.IsNullOrEmpty (method))
continue;
string[] toks = members [i].Split (new[]{':'}, 4);
Delegate callback;
if (toks [2] == "__export__") {
var mname = toks [0].Substring (2);
MethodInfo? minfo = null;
foreach (var mi in type.GetMethods (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
if (mi.Name == mname && JavaNativeTypeManager.GetJniSignature (mi) == toks [1]) {
minfo = mi;
break;
}

if (minfo == null)
throw new InvalidOperationException (String.Format ("Specified managed method '{0}' was not found. Signature: {1}", mname, toks [1]));
callback = CreateDynamicCallback (minfo);
} else {
Type callbackDeclaringType = type;
if (toks.Length == 4) {
callbackDeclaringType = Type.GetType (toks [3], throwOnError: true)!;
}

if (minfo == null)
throw new InvalidOperationException (String.Format ("Specified managed method '{0}' was not found. Signature: {1}", mname, toks [1]));
callback = CreateDynamicCallback (minfo);
} else {
Type callbackDeclaringType = type;
if (toks.Length == 4) {
callbackDeclaringType = Type.GetType (toks [3], throwOnError: true)!;
}
while (callbackDeclaringType.ContainsGenericParameters) {
callbackDeclaringType = callbackDeclaringType.BaseType!;
while (callbackDeclaringType.ContainsGenericParameters) {
callbackDeclaringType = callbackDeclaringType.BaseType!;
}
GetCallbackHandler connector = (GetCallbackHandler) Delegate.CreateDelegate (typeof (GetCallbackHandler),
callbackDeclaringType, toks [2]);
callback = connector ();
}
GetCallbackHandler connector = (GetCallbackHandler) Delegate.CreateDelegate (typeof (GetCallbackHandler),
callbackDeclaringType, toks [2]);
callback = connector ();
natives [i] = new JniNativeMethodRegistration (toks [0], toks [1], callback);
}
natives [i] = new JniNativeMethodRegistration (toks [0], toks [1], callback);
}

JniEnvironment.Types.RegisterNatives (nativeClass.PeerReference, natives, natives.Length);
JniEnvironment.Types.RegisterNatives (nativeClass.PeerReference, natives, natives.Length);
} catch (Exception e) {
JniEnvironment.Runtime.RaisePendingException (e);
}
}
}

Expand Down