Skip to content
9 changes: 1 addition & 8 deletions samples/Hello-NativeAOTFromAndroid/NativeAotTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ namespace Java.Interop.Samples.NativeAotFromAndroid;
// suppressions lived (buried) inside JniTypeManager itself, justified "NotUsedInAndroid"; #1441 just
// moved that responsibility to callers via [RequiresDynamicCode]/[RequiresUnreferencedCode].
partial class NativeAotTypeManager : JniRuntime.ReflectionJniTypeManager {

const DynamicallyAccessedMemberTypes MethodsConstructors =
DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.NonPublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;

Dictionary<string, Type> typeMappings = new () {
["android/app/Activity"] = typeof (Android.App.Activity),
["android/content/Context"] = typeof (Android.Content.Context),
Expand All @@ -31,15 +25,14 @@ partial class NativeAotTypeManager : JniRuntime.ReflectionJniTypeManager {
["my/MainActivity"] = typeof (MainActivity),
};

[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "Sample only (see class comment): this assembly is rooted via TrimmerRootAssembly and the members reflected over during registration are preserved by the [DynamicallyAccessedMembers] annotations on the RegisterNativeMembers(Type) -> FindAndCallRegisterMethod path, so trimming does not remove what reflection needs.")]
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "Sample only (see class comment): this assembly is rooted via TrimmerRootAssembly. The reflection-based registration path is not trim-clean, and this sample intentionally suppresses the warning rather than proving each reflected member to the trimmer.")]
[UnconditionalSuppressMessage ("AOT", "IL3050", Justification = "Sample only (see class comment): built-in member registration calls CreateDelegate on compile-time-known static methods (no MakeGenericType / expression compilation), so no runtime code generation is required.")]
public NativeAotTypeManager ()
{
}

// GetType() dispatches through GetTypeForSimpleReference (singular), so the sample's own type
// map has to be applied here; the base ReflectionJniTypeManager only knows the built-in types.
[return: DynamicallyAccessedMembers (MethodsConstructors)]
protected override Type? GetTypeForSimpleReference (string jniSimpleReference)
{
if (typeMappings.TryGetValue (jniSimpleReference, out var target))
Comment thread
simonrozsival marked this conversation as resolved.
Expand Down
8 changes: 1 addition & 7 deletions samples/Hello-NativeAOTFromJNI/NativeAotTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ namespace Hello_NativeAOTFromJNI;
// moved that responsibility to callers via [RequiresDynamicCode]/[RequiresUnreferencedCode].
class NativeAotTypeManager : JniRuntime.ReflectionJniTypeManager {

const DynamicallyAccessedMemberTypes MethodsConstructors =
DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.NonPublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;

[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "Sample only (see class comment): this assembly is rooted via TrimmerRootAssembly and the members reflected over during registration are preserved by the [DynamicallyAccessedMembers] annotations on the RegisterNativeMembers(Type) -> FindAndCallRegisterMethod path, so trimming does not remove what reflection needs.")]
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "Sample only (see class comment): this assembly is rooted via TrimmerRootAssembly. The reflection-based registration path is not trim-clean, and this sample intentionally suppresses the warning rather than proving each reflected member to the trimmer.")]
[UnconditionalSuppressMessage ("AOT", "IL3050", Justification = "Sample only (see class comment): built-in member registration calls CreateDelegate on compile-time-known static methods (no MakeGenericType / expression compilation), so no runtime code generation is required.")]
public NativeAotTypeManager ()
{
Expand All @@ -31,7 +26,6 @@ public NativeAotTypeManager ()
// JavaProxyObject, ...) and handles registration and the reverse Type->JNI mapping (via the
// [JniTypeSignature] attribute) for us. We only need to teach it about this sample's own
// managed types.
[return: DynamicallyAccessedMembers (MethodsConstructors)]
protected override Type? GetTypeForSimpleReference (string jniSimpleReference)
{
if (jniSimpleReference == Example.ManagedType.JniTypeName)
Comment thread
simonrozsival marked this conversation as resolved.
Expand Down
6 changes: 1 addition & 5 deletions src/Java.Interop/Java.Interop/JavaArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,7 @@ public void Dispose ()
}

[JniTypeSignature ("java/lang/Object", ArrayRank=1, GenerateJavaPeer=false)]
public abstract class JavaPrimitiveArray<
[DynamicallyAccessedMembers (Constructors)]
T
>
: JavaArray<T>
public abstract class JavaPrimitiveArray<T> : JavaArray<T>
{

internal JavaPrimitiveArray (ref JniObjectReference reference, JniObjectReferenceOptions transfer)
Expand Down
7 changes: 1 addition & 6 deletions src/Java.Interop/Java.Interop/JavaObjectArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
namespace Java.Interop
{
[JniTypeSignature ("java/lang/Object", ArrayRank=1, GenerateJavaPeer=false)]
public class JavaObjectArray<
[DynamicallyAccessedMembers (Constructors)]
T
>
: JavaArray<T>
public class JavaObjectArray<[DynamicallyAccessedMembers (Constructors)] T> : JavaArray<T>
{
internal static readonly ValueMarshaler Instance = new ValueMarshaler ();

Expand Down Expand Up @@ -171,7 +167,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<T>> {
public override IList<T> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<T>.CreateValue (ref reference, options, targetType, (ref JniObjectReference h, JniObjectReferenceOptions t) => new JavaObjectArray<T> (ref h, t) {
Expand Down
15 changes: 0 additions & 15 deletions src/Java.Interop/Java.Interop/JavaPrimitiveArrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -256,7 +255,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Boolean>> {
public override IList<Boolean> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Boolean>.CreateValue (
Expand Down Expand Up @@ -449,7 +447,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -460,7 +457,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<SByte>> {
public override IList<SByte> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<SByte>.CreateValue (
Expand Down Expand Up @@ -653,7 +649,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -664,7 +659,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Char>> {
public override IList<Char> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Char>.CreateValue (
Expand Down Expand Up @@ -868,7 +862,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Int16>> {
public override IList<Int16> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Int16>.CreateValue (
Expand Down Expand Up @@ -1061,7 +1054,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -1072,7 +1064,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Int32>> {
public override IList<Int32> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Int32>.CreateValue (
Expand Down Expand Up @@ -1265,7 +1256,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -1276,7 +1266,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Int64>> {
public override IList<Int64> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Int64>.CreateValue (
Expand Down Expand Up @@ -1469,7 +1458,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -1480,7 +1468,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Single>> {
public override IList<Single> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Single>.CreateValue (
Expand Down Expand Up @@ -1673,7 +1660,6 @@ internal override bool TargetTypeIsCurrentType (Type? targetType)

public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
Expand All @@ -1684,7 +1670,6 @@ internal sealed class ValueMarshaler : JniValueMarshaler<IList<Double>> {
public override IList<Double> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (Constructors)]
Type? targetType)
{
return JavaArray<Double>.CreateValue (
Expand Down
Loading