Skip to content

Commit 6d72140

Browse files
committed
WIP
1 parent 6898c1c commit 6d72140

16 files changed

Lines changed: 574 additions & 197 deletions

File tree

samples/Hello-NativeAOTFromAndroid/NativeAotTypeManager.cs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Java.Interop.Samples.NativeAotFromAndroid;
66

77
partial class NativeAotTypeManager : JniRuntime.JniTypeManager {
88

9+
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
910
internal const DynamicallyAccessedMemberTypes Methods = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
1011
internal const DynamicallyAccessedMemberTypes MethodsAndPrivateNested = Methods | DynamicallyAccessedMemberTypes.NonPublicNestedTypes;
1112

@@ -26,9 +27,27 @@ public override void RegisterNativeMembers (
2627
ReadOnlySpan<char> methods)
2728
{
2829
Console.WriteLine ($"# jonp: RegisterNativeMembers: nativeClass={nativeClass} type=`{type}`");
29-
base.RegisterNativeMembers (nativeClass, type, methods);
30+
if (!methods.IsEmpty)
31+
throw new NotSupportedException ($"Could not register native members for type '{type.FullName}'.");
3032
}
3133

34+
public override void RegisterNativeMembers (
35+
JniType nativeClass,
36+
[DynamicallyAccessedMembers (MethodsAndPrivateNested)]
37+
Type type,
38+
string? methods)
39+
{
40+
RegisterNativeMembers (nativeClass, type, methods.AsSpan ());
41+
}
42+
43+
protected override string? GetSimpleReference (Type type)
44+
{
45+
foreach (var e in typeMappings) {
46+
if (e.Value == type)
47+
return e.Key;
48+
}
49+
return null;
50+
}
3251

3352
protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
3453
{
@@ -37,16 +56,18 @@ protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpl
3756
Console.WriteLine ($"# jonp: GetTypesForSimpleReference: jniSimpleReference=`{jniSimpleReference}` -> `{target}`");
3857
yield return target;
3958
}
40-
foreach (var t in base.GetTypesForSimpleReference (jniSimpleReference)) {
41-
Console.WriteLine ($"# jonp: GetTypesForSimpleReference: jniSimpleReference=`{jniSimpleReference}` -> `{t}`");
42-
yield return t;
43-
}
59+
}
60+
61+
protected override Type? GetTypeForSimpleReference (string jniSimpleReference)
62+
{
63+
if (typeMappings.TryGetValue (jniSimpleReference, out var target))
64+
return target;
65+
return null;
4466
}
4567

4668
protected override IEnumerable<string> GetSimpleReferences (Type type)
4769
{
48-
return base.GetSimpleReferences (type)
49-
.Concat (CreateSimpleReferencesEnumerator (type));
70+
return CreateSimpleReferencesEnumerator (type);
5071
}
5172

5273
IEnumerable<string> CreateSimpleReferencesEnumerator (Type type)
@@ -58,4 +79,39 @@ IEnumerable<string> CreateSimpleReferencesEnumerator (Type type)
5879
yield return e.Key;
5980
}
6081
}
82+
83+
public override IEnumerable<Type> GetTypes (JniTypeSignature typeSignature)
84+
{
85+
if (!typeSignature.IsValid || typeSignature.ArrayRank != 0 || typeSignature.SimpleReference == null)
86+
return [];
87+
return GetTypesForSimpleReference (typeSignature.SimpleReference);
88+
}
89+
90+
public override IEnumerable<ReflectionConstructibleType> GetReflectionConstructibleTypes (JniTypeSignature typeSignature)
91+
{
92+
foreach (var type in GetTypes (typeSignature)) {
93+
yield return new ReflectionConstructibleType (type);
94+
}
95+
}
96+
97+
protected override Type? GetInvokerTypeCore ([DynamicallyAccessedMembers (Constructors)] Type type) => null;
98+
99+
protected override JniTypeSignature GetTypeSignatureCore (Type type)
100+
{
101+
var simpleReference = GetSimpleReference (type);
102+
return simpleReference == null ? default : new JniTypeSignature (simpleReference, 0, false);
103+
}
104+
105+
protected override IEnumerable<JniTypeSignature> GetTypeSignaturesCore (Type type)
106+
{
107+
var signature = GetTypeSignatureCore (type);
108+
if (signature.IsValid)
109+
yield return signature;
110+
}
111+
112+
protected override IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
113+
114+
protected override string? GetReplacementTypeCore (string jniSimpleReference) => null;
115+
116+
protected override JniRuntime.ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
61117
}

samples/Hello-NativeAOTFromJNI/ManagedType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static IntPtr n_GetString (IntPtr jnienv, IntPtr n_self)
3838
}
3939

4040
[JniAddNativeMethodRegistration]
41-
static void RegisterNativeMembers (JniNativeMethodRegistrationArguments args)
41+
internal static void RegisterNativeMembers (JniNativeMethodRegistrationArguments args)
4242
{
4343
args.AddRegistrations (new [] {
4444
new JniNativeMethodRegistration ("n_GetString", "()Ljava/lang/String;", new _JniMarshal_PP_L (n_GetString)),
Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
using Java.Interop;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace Hello_NativeAOTFromJNI;
45

56
class NativeAotTypeManager : JniRuntime.JniTypeManager {
7+
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
8+
internal const DynamicallyAccessedMemberTypes Methods = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
9+
internal const DynamicallyAccessedMemberTypes MethodsAndPrivateNested = Methods | DynamicallyAccessedMemberTypes.NonPublicNestedTypes;
610

711
#pragma warning disable IL2026
812
Dictionary<string, Type> typeMappings = new () {
913
[Example.ManagedType.JniTypeName] = typeof (Example.ManagedType),
14+
[Java.Lang.Object.JniTypeName] = typeof (Java.Lang.Object),
15+
[Java.Lang.String.JniTypeName] = typeof (Java.Lang.String),
1016
};
1117
#pragma warning restore IL2026
1218

19+
protected override string? GetSimpleReference (Type type)
20+
{
21+
foreach (var e in typeMappings) {
22+
if (e.Value == type)
23+
return e.Key;
24+
}
25+
return null;
26+
}
27+
1328

1429
protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
1530
{
1631
if (typeMappings.TryGetValue (jniSimpleReference, out var target))
1732
yield return target;
18-
foreach (var t in base.GetTypesForSimpleReference (jniSimpleReference))
19-
yield return t;
33+
}
34+
35+
protected override Type? GetTypeForSimpleReference (string jniSimpleReference)
36+
{
37+
if (typeMappings.TryGetValue (jniSimpleReference, out var target))
38+
return target;
39+
return null;
2040
}
2141

2242
protected override IEnumerable<string> GetSimpleReferences (Type type)
2343
{
24-
return base.GetSimpleReferences (type)
25-
.Concat (CreateSimpleReferencesEnumerator (type));
44+
return CreateSimpleReferencesEnumerator (type);
2645
}
2746

2847
IEnumerable<string> CreateSimpleReferencesEnumerator (Type type)
@@ -34,4 +53,66 @@ IEnumerable<string> CreateSimpleReferencesEnumerator (Type type)
3453
yield return e.Key;
3554
}
3655
}
56+
57+
public override IEnumerable<Type> GetTypes (JniTypeSignature typeSignature)
58+
{
59+
if (!typeSignature.IsValid || typeSignature.ArrayRank != 0 || typeSignature.SimpleReference == null)
60+
return [];
61+
return GetTypesForSimpleReference (typeSignature.SimpleReference);
62+
}
63+
64+
public override IEnumerable<ReflectionConstructibleType> GetReflectionConstructibleTypes (JniTypeSignature typeSignature)
65+
{
66+
foreach (var type in GetTypes (typeSignature)) {
67+
yield return new ReflectionConstructibleType (type);
68+
}
69+
}
70+
71+
protected override Type? GetInvokerTypeCore ([DynamicallyAccessedMembers (Constructors)] Type type) => null;
72+
73+
protected override JniTypeSignature GetTypeSignatureCore (Type type)
74+
{
75+
var simpleReference = GetSimpleReference (type);
76+
return simpleReference == null ? default : new JniTypeSignature (simpleReference, 0, false);
77+
}
78+
79+
protected override IEnumerable<JniTypeSignature> GetTypeSignaturesCore (Type type)
80+
{
81+
var signature = GetTypeSignatureCore (type);
82+
if (signature.IsValid)
83+
yield return signature;
84+
}
85+
86+
protected override IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
87+
88+
protected override string? GetReplacementTypeCore (string jniSimpleReference) => null;
89+
90+
protected override JniRuntime.ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
91+
92+
public override void RegisterNativeMembers (
93+
JniType nativeClass,
94+
[DynamicallyAccessedMembers (MethodsAndPrivateNested)]
95+
Type type,
96+
ReadOnlySpan<char> methods)
97+
{
98+
if (type != typeof (Example.ManagedType)) {
99+
if (!methods.IsEmpty)
100+
throw new NotSupportedException ($"Could not register native members for type '{type.FullName}'.");
101+
return;
102+
}
103+
104+
var registrations = new List<JniNativeMethodRegistration> ();
105+
Example.ManagedType.RegisterNativeMembers (new JniNativeMethodRegistrationArguments (registrations, null));
106+
if (registrations.Count > 0)
107+
nativeClass.RegisterNativeMethods (registrations.ToArray ());
108+
}
109+
110+
public override void RegisterNativeMembers (
111+
JniType nativeClass,
112+
[DynamicallyAccessedMembers (MethodsAndPrivateNested)]
113+
Type type,
114+
string? methods)
115+
{
116+
RegisterNativeMembers (nativeClass, type, methods.AsSpan ());
117+
}
37118
}

src/Java.Interop/GlobalSuppressions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniRuntime.JniValueManager")]
2626
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniEnvironment.References")]
2727
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniRuntime.JniTypeManager")]
28+
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniRuntime.DynamicJniTypeManager")]
2829
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniPeerMembers.JniInstanceMethods")]
2930
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniPeerMembers.JniInstanceFields")]
3031
[assembly: SuppressMessage ("Design", "CA1034:Nested types should not be visible", Justification = "Deliberate choice to 'hide' these types from code completion for `Java.Interop.`.", Scope = "type", Target = "~T:Java.Interop.JniRuntime.CreationOptions")]

src/Java.Interop/Java.Interop/JavaPrimitiveArrays.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ namespace Java.Interop {
1313

1414
partial class JniRuntime {
1515

16-
partial class JniTypeManager {
17-
16+
partial class DynamicJniTypeManager {
1817
readonly struct JniPrimitiveArrayInfo {
1918
public readonly JniTypeSignature JniTypeSignature;
2019
public readonly Type PrimitiveType;

src/Java.Interop/Java.Interop/JavaPrimitiveArrays.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Java.Interop {
3030
#>
3131
partial class JniRuntime {
3232

33-
partial class JniTypeManager {
33+
partial class DynamicJniTypeManager {
3434

3535
readonly struct JniPrimitiveArrayInfo {
3636
public readonly JniTypeSignature JniTypeSignature;

0 commit comments

Comments
 (0)