-
Notifications
You must be signed in to change notification settings - Fork 56
[tests] fix trimmer warnings in Java.Interop-Tests #1318
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
Conversation
Context: dotnet/android#9846 When building `Mono.Android.NET-Tests.csproj` for NativeAOT, you end up with many warnings (that are upgraded to errors): external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs(92,4): error IL2026: Using member 'System.Reflection.Assembly.GetType(String, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs(93,4): error IL2026: Using member 'System.Reflection.Assembly.GetType(String, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JavaObjectArrayTest.cs(15,11): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaObjectArray<T>'. The generic parameter 'T' of 'Java.InteropTests.JavaObjectArrayContractTest<T>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JavaObjectArrayTest.cs(21,48): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaObjectArray<T>'. The generic parameter 'T' of 'Java.InteropTests.JavaObjectArrayContractTest<T>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JavaObjectArrayTest.cs(22,48): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaObjectArray<T>'. The generic parameter 'T' of 'Java.InteropTests.JavaObjectArrayContractTest<T>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JavaObjectArrayTest.cs(23,44): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaObjectArray<T>'. The generic parameter 'T' of 'Java.InteropTests.JavaObjectArrayContractTest<T>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JavaExceptionTests.cs(105,34): error IL2026: Using member 'System.Reflection.Assembly.GetType(String, Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JavaExceptionTests.cs(108,34): error IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Activator.CreateInstance(Type, params Object[])'. The return value of method 'System.Reflection.Assembly.GetType(String, Boolean)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs(99,41): error IL2077: 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Delegate.CreateDelegate(Type, Type, String, Boolean, Boolean)'. The field 'Java.InteropTests.JniEnvironmentTests.NativeMethods_type' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs(111,35): error IL2077: 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Delegate.CreateDelegate(Type, Type, String, Boolean, Boolean)'. The field 'Java.InteropTests.JniEnvironmentTests.NativeMethods_type' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs(650,127): error IL2092: 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'targetType' of method 'Java.InteropTests.DemoValueTypeValueMarshaler.CreateGenericValue(ref JniObjectReference, JniObjectReferenceOptions, Type)' don't match overridden parameter 'targetType' of method 'Java.Interop.JniValueMarshaler<T>.CreateGenericValue(ref JniObjectReference, JniObjectReferenceOptions, Type)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniRuntimeJniValueManagerContract.cs(28,18): error IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.InteropTests.JniRuntimeJniValueManagerContract.ValueManagerType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs(36,35): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler<T>()'. The generic parameter 'T' of 'Java.InteropTests.JniValueMarshalerContractTests<T>' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs(224,18): error IL2026: Using member 'Java.Interop.JniValueMarshaler.CreateReturnValueFromManagedExpression(JniValueMarshalerContext, ParameterExpression)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. System.Linq.Expression usage may trim away required code. external/Java.Interop/tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs(224,18): error IL3050: Using member 'Java.Interop.JniValueMarshaler.CreateReturnValueFromManagedExpression(JniValueMarshalerContext, ParameterExpression)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. System.Linq.Expression usage may trim away required code. We can address these here by importing: <Import Project="..\..\build-tools\trim-analyzers\trim-analyzers.props" /> And then solving the warnings (that are now errors) by adding missing attributes. This might also improve the chances of these tests passing under NativeAOT, in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR aims to resolve trimming warnings (elevated to errors) when building Mono.Android.NET-Tests.csproj for NativeAOT by adding missing DynamicAccessedMember annotations and updating type retrieval methods. Key changes include:
- Adding [DynamicallyAccessedMembers] attributes to properties, generic parameters, and method parameters in various test files.
- Updating type resolution from Assembly.GetType to Type.GetType in JavaExceptionTests and JniEnvironmentTests.
- Ensuring generic contract test classes include the necessary constructor annotations.
Reviewed Changes
File | Description |
---|---|
tests/Java.Interop-Tests/Java.Interop/JniRuntimeJniValueManagerContract.cs | Added annotations to property and generic parameter to support trimming requirements. |
tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs | Updated generic parameter and method parameter annotations and added Requires* attributes. |
tests/Java.Interop-Tests/Java.Interop/JavaExceptionTests.cs | Refactored type retrieval to use Type.GetType with assembly-qualified names. |
tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs | Applied [DynamicallyAccessedMembers] to NativeMethods_type and simplified type resolution. |
tests/Java.Interop-Tests/Java.Interop/JavaObjectArrayTest.cs | Added the necessary annotations on the generic parameter for constructor access. |
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
public override DemoValueType CreateGenericValue ( | ||
ref JniObjectReference reference, | ||
JniObjectReferenceOptions options, | ||
[DynamicallyAccessedMembers (Constructors)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The attribute value 'Constructors' is not a valid member of DynamicallyAccessedMemberTypes. Consider using 'DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors' instead.
[DynamicallyAccessedMembers (Constructors)] | |
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is from:
java-interop/src/Java.Interop/Java.Interop/JniValueMarshaler.cs
Lines 121 to 123 in 719e615
public abstract class JniValueMarshaler { | |
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors; |
It couldn't be used in the other places.
Ok, the tests build & run now: TODO: get them to pass! |
Context: dotnet/android#9846
When building
Mono.Android.NET-Tests.csproj
for NativeAOT, you end up with many warnings (that are upgraded to errors):We can address these here by importing:
And then solving the warnings (that are now errors) by adding missing attributes.
This might also improve the chances of these tests passing under NativeAOT, in general.