Skip to content

Commit 6096f8b

Browse files
[tests] fix trimmer warnings in Java.Interop-Tests (#1318)
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.
1 parent 719e615 commit 6096f8b

6 files changed

+44
-13
lines changed

tests/Java.Interop-Tests/Java.Interop-Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<Import Project="..\..\TargetFrameworkDependentValues.props" />
13+
<Import Project="..\..\build-tools\trim-analyzers\trim-analyzers.props" />
1314

1415
<PropertyGroup>
1516
<OutputPath>$(TestOutputFullPath)</OutputPath>

tests/Java.Interop-Tests/Java.Interop/JavaExceptionTests.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public void InnerExceptionIsNotAProxy ()
102102

103103
static JavaException CreateJavaProxyThrowable (Exception value)
104104
{
105-
var JavaProxyThrowable_type = typeof(JavaObject)
106-
.Assembly
107-
.GetType ("Java.Interop.JavaProxyThrowable", throwOnError :true);
105+
var JavaProxyThrowable_type = Type.GetType ("Java.Interop.JavaProxyThrowable, Java.Interop", throwOnError :true);
108106
var proxy = (JavaException) Activator.CreateInstance (JavaProxyThrowable_type, value);
109107
return proxy;
110108
}

tests/Java.Interop-Tests/Java.Interop/JavaObjectArrayTest.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45

56
using Java.Interop;
@@ -8,7 +9,10 @@
89

910
namespace Java.InteropTests
1011
{
11-
public abstract class JavaObjectArrayContractTest<T> : JavaArrayContract<T>
12+
public abstract class JavaObjectArrayContractTest<
13+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
14+
T
15+
> : JavaArrayContract<T>
1216
{
1317
protected override System.Collections.Generic.ICollection<T> CreateCollection (System.Collections.Generic.IEnumerable<T> values)
1418
{

tests/Java.Interop-Tests/Java.Interop/JniEnvironmentTests.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Runtime.InteropServices;
34

45
using Java.Interop;
@@ -88,9 +89,10 @@ public void References_CreatedReference_LocalRef ()
8889
}
8990
}
9091

92+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
9193
static readonly Type NativeMethods_type =
92-
typeof (JniEnvironment).Assembly.GetType ("Java.Interop.NativeMethods", throwOnError: false) ??
93-
typeof (JniEnvironment).Assembly.GetType ("Java.Interop.JIPinvokes.NativeMethods", throwOnError: false);
94+
Type.GetType ("Java.Interop.NativeMethods, Java.Interop", throwOnError: false) ??
95+
Type.GetType ("Java.Interop.JIPinvokes.NativeMethods, Java.Interop", throwOnError: false);
9496

9597
static Func<IntPtr, IntPtr, IntPtr> GetNewRefFunc (string method)
9698
{

tests/Java.Interop-Tests/Java.Interop/JniRuntimeJniValueManagerContract.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
67
using System.Reflection;
78
using System.Threading;
@@ -19,6 +20,7 @@ namespace Java.InteropTests {
1920
#endif // !__ANDROID__
2021
public abstract class JniRuntimeJniValueManagerContract : JavaVMFixture {
2122

23+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
2224
protected abstract Type ValueManagerType {
2325
get;
2426
}
@@ -296,8 +298,12 @@ static string DumpPeers (IEnumerable<JniSurfacedPeerInfo> peers)
296298
// Adding an instance already added in a previous scope?
297299
}
298300

299-
public abstract class JniRuntimeJniValueManagerContract<T> : JniRuntimeJniValueManagerContract {
300-
301+
public abstract class JniRuntimeJniValueManagerContract<
302+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
303+
T
304+
> : JniRuntimeJniValueManagerContract
305+
{
306+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
301307
protected override Type ValueManagerType => typeof (T);
302308
}
303309

@@ -313,8 +319,10 @@ public class JniRuntimeJniValueManagerContract_Mono : JniRuntimeJniValueManagerC
313319

314320
[TestFixture]
315321
public class JniRuntimeJniValueManagerContract_NoGCIntegration : JniRuntimeJniValueManagerContract {
322+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
316323
static Type ManagedValueManagerType = Type.GetType ("Java.Interop.ManagedValueManager, Java.Runtime.Environment", throwOnError:true)!;
317324

325+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
318326
protected override Type ValueManagerType => ManagedValueManagerType;
319327
}
320328
#endif // !__ANDROID__

tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Reflection;
@@ -13,7 +14,11 @@
1314

1415
namespace Java.InteropTests {
1516

16-
public abstract class JniValueMarshalerContractTests<T> : JavaVMFixture {
17+
public abstract class JniValueMarshalerContractTests<
18+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
19+
T
20+
> : JavaVMFixture
21+
{
1722

1823
protected abstract T Value {get;}
1924

@@ -211,6 +216,8 @@ public void DestroyGenericArgumentState ()
211216
}
212217

213218
[Test]
219+
[RequiresUnreferencedCode ("CreateReturnValueFromManagedExpression")]
220+
[RequiresDynamicCode ("CreateReturnValueFromManagedExpression")]
214221
public void CreateReturnValueFromManagedExpression ()
215222
{
216223
var runtime = Expression.Variable (typeof (JniRuntime), "__jvm");
@@ -335,7 +342,11 @@ protected override string GetExpectedReturnValueFromManagedExpression (string jv
335342
}
336343
}
337344

338-
public abstract class JniValueMarshaler_BuiltinType_ContractTests<T> : JniValueMarshalerContractTests<T> {
345+
public abstract class JniValueMarshaler_BuiltinType_ContractTests<
346+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
347+
T
348+
> : JniValueMarshalerContractTests<T>
349+
{
339350
protected override bool IsJniValueType {get {return true;}}
340351

341352
protected override string GetExpectedReturnValueFromManagedExpression (string jvm, string value, Expression ret)
@@ -442,8 +453,11 @@ public class JniValueMarshaler_NullableDouble_ContractTests : JniValueMarshalerC
442453
protected override double? Value {get {return 8D;}}
443454
}
444455

445-
public abstract class JniInt32ArrayValueMarshalerContractTests<T> : JniValueMarshalerContractTests<T>
446-
where T : IEnumerable<int>
456+
public abstract class JniInt32ArrayValueMarshalerContractTests<
457+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
458+
T
459+
> : JniValueMarshalerContractTests<T>
460+
where T : IEnumerable<int>
447461
{
448462
protected abstract T CreateArray (int[] values);
449463
protected abstract string ValueMarshalerSourceType {get;}
@@ -647,7 +661,11 @@ public DemoValueTypeValueMarshaler ()
647661
Int32Marshaler = JniRuntime.CurrentRuntime.ValueManager.GetValueMarshaler<int> ();
648662
}
649663

650-
public override DemoValueType CreateGenericValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType)
664+
public override DemoValueType CreateGenericValue (
665+
ref JniObjectReference reference,
666+
JniObjectReferenceOptions options,
667+
[DynamicallyAccessedMembers (Constructors)]
668+
Type targetType)
651669
{
652670
var v = Int32Marshaler.CreateGenericValue (ref reference, options, typeof (int));
653671
return new DemoValueType (v);

0 commit comments

Comments
 (0)