Skip to content

Commit d940181

Browse files
committed
Make GetCustomAttribute calls optional in JniTypeManager
Optionally do not look for the `JniAddNativeMethodRegistrationAttribute`. It turns out that this attribute is not commonly present in Xamarin.Android applications and so all of the calls to `GetCustomAttribute` failed to find it, incurring unnecessary performance penalty on the application. Make the code consult a global flag (in case of Xamarin.Android it is set on the build time) which tells it whether or not to skip querying for the above custom attribute. The flag is `true` by default to maintain backward compatibility.
1 parent 4f47ec8 commit d940181

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ static bool TryRegisterNativeMembers (JniType nativeClass, Type marshalType, str
333333

334334
static bool FindAndCallRegisterMethod (Type marshalType, JniNativeMethodRegistrationArguments arguments)
335335
{
336+
if (!JniTypeManagerOptions.JniAddNativeMethodRegistrationAttributePresent)
337+
return false;
338+
336339
bool found = false;
337340

338341
foreach (var methodInfo in marshalType.GetRuntimeMethods ()) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Java.Interop
2+
{
3+
// These settings affect all instances of JniTypeManager
4+
public static class JniTypeManagerOptions
5+
{
6+
// If `false`, reflection checks for the presence of the `JniAddNativeMethodRegistrationAttribute` attribute
7+
// throughout the **entire** application will be skipped.
8+
public static bool JniAddNativeMethodRegistrationAttributePresent { get; set; } = true;
9+
}
10+
}

0 commit comments

Comments
 (0)