You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead of hard-coding the name of a method to register native
members in a given type, introduce new attribute to mark appropriate
methods within that type.
```csharp
// Old-and-busted:
static void __RegisterNativeMembers (JniType type, string members)
{
// ...
type.RegisterNativeMethods (...)
}
// New-Hawtness:
[JniAddNativeMethodRegistrationAttribute]
static void RegisterNativeMembers (JniNativeMethodRegistrationArguments args)
{
args.AddRegistrations (...);
}
// Supporting cast:
public sealed class JniAddNativeMethodRegistrationAttribute : Attribute {
}
public struct JniNativeMethodRegistrationArguments {
public JniNativeMethodRegistrationArguments (ICollection<JniNativeMethodRegistration> registrations, string methods);
public ICollection<JniNativeMethodRegistration> Registrations {get;}
public string Methods {get;}
public void AddRegistrations (IEnumerable<JniNativeMethodRegistration> registrations);
}
```
Responsibility for invoking `JniType.RegisterNativeMembers()` has
been *moved* from `__RegisterNativeMembers` into internal code, and
`JniType.RegisterNativeMembers()` has been made `internal` so that
it can't be called externally.
Rationale: The `__RegisterNativeMembers()` method was "too magical":
it's hard to document, but it has a large responsibility in ensuring
that the Java `native` methods on a type are properly registered.
Introducing a new `[JniAddNativeMethodRegistrationAttribute]` custom
attribute simplifies this, as there is now an in-language construct
which can contain suitable documentation.
Additionally, `JNIEnv::RegisterNativeMethods()` can only be invoked
*once* on a given type. (Not *strictly* true -- you *can* call it
more than once for a given `jclass`! -- but the second and later
invocations will *replace* the contents of the original invocation.)
This complicates *documenting* what
`[JniAddNativeMethodRegistrationAttribute]` can do. There are only
two plausible usage scenarios:
1. A type can contain *only **one*** such method.
2. A type can contain *multiple* such methods.
Absent a truly compelling reason to go with (1), the only way to
support (2) is to prevent application code from calling
`JniType.RegisterNativeMethods()`. This allows
`JniRuntime.JniTypeManager.TryRegisterNativeMembers()` to invoke
all `[JniAddNativeMethodRegistrationAttribute]` methods and collect
the values, then call `JNIEnv::RegisterNativeMethods()` *once*.
`TestType.cs` has been updated to demonstrate using multiple
`[JniAddNativeMethodRegistrationAttribute]` methods.
0 commit comments