Skip to content

Conversation

@jonathanpeppers
Copy link
Member

Context: #5652

Building an app such as:

dotnet new android
dotnet build -c Release -p:SuppressTrimAnalysisWarnings=false -p:TrimmerSingleWarn=false

Results in many illink/trimmer warnings:

external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniMarshalMemberBuilder.cs(57,4): warning IL2072: Java.Interop.JniRuntime.SetMarshalMemberBuilder(JniRuntime.CreationOptions): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(464,32): warning IL2067: Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethods(Type)'. The parameter 'marshalType' of method 'Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(414,5): warning IL2070: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicNestedTypes' in call to 'System.Type.GetNestedType(String, BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(418,5): warning IL2072: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'name' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethod(Type, String, Type[])'. The return value of method 'System.Type.GetNestedType(String, BindingFlags)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(338,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(577,5): warning IL2060: Java.Interop.JniRuntime.JniValueManager.GetObjectArrayMarshaler(Type): Call to 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic method.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(523,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(537,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(559,27): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(569,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\ManagedPeer.cs(93,5): warning IL2057: Java.Interop.ManagedPeer.Construct(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(156,5): warning IL2057: Java.Interop.ManagedPeer.GetParameterTypes(String): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(198,5): warning IL2057: Java.Interop.ManagedPeer.RegisterNativeMembers(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
src\Mono.Android\Android.Runtime\AndroidRuntime.cs(403,5): warning IL2075: Android.Runtime.AndroidTypeManager.CreateDynamicCallback(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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.
src\Mono.Android\Android.Runtime\AndroidRuntime.cs(526,8): warning IL2070: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>)' 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.
src\Mono.Android\Android.Runtime\AndroidRuntime.cs(540,9): warning IL2057: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
src\Mono.Android\Android.Runtime\AndroidRuntime.cs(546,8): warning IL2072: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type, Type, String)'. The return value of method 'System.Type.BaseType.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.
src\Mono.Android\Android.Runtime\AndroidRuntime.cs(546,8): warning IL2067: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type, Type, String)'. The parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>)' 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\src\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs(182,4): warning IL2070: Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind)' 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.
src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(139,4): warning IL2055: Java.Interop.JavaObjectExtensions.GetInvokerType(Type): Call to 'System.Type.MakeGenericType(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.
src\Mono.Android\Java.Interop\TypeManager.cs(169,5): warning IL2072: Java.Interop.TypeManager.Activate(IntPtr, ConstructorInfo, Object[]): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.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.
src\Mono.Android\Java.Interop\TypeManager.cs(326,4): warning IL2070: Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])'. The parameter 'type' of method 'Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership)' 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.
src\Mono.Android\Java.Interop\TypeManager.cs(330,4): warning IL2070: Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])'. The parameter 'type' of method 'Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership)' 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.
src\Mono.Android\Java.Interop\TypeManager.cs(126,5): warning IL2057: Java.Interop.TypeManager.GetParameterTypes(String): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
src\Mono.Android\Java.Interop\TypeManager.cs(146,4): warning IL2057: Java.Interop.TypeManager.n_Activate(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.

This commit suppresses/solves the ones in Mono.Android.dll and we are left with 15 that can be solved in the xamarin/java.interop repo:

external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniMarshalMemberBuilder.cs(57,4): warning IL2072: Java.Interop.JniRuntime.SetMarshalMemberBuilder(JniRuntime.CreationOptions): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(464,32): warning IL2067: Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethods(Type)'. The parameter 'marshalType' of method 'Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(414,5): warning IL2070: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicNestedTypes' in call to 'System.Type.GetNestedType(String, BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(418,5): warning IL2072: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'name' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethod(Type, String, Type[])'. The return value of method 'System.Type.GetNestedType(String, BindingFlags)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(338,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(577,5): warning IL2060: Java.Interop.JniRuntime.JniValueManager.GetObjectArrayMarshaler(Type): Call to 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic method.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(523,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(537,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(559,27): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(569,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\ManagedPeer.cs(93,5): warning IL2057: Java.Interop.ManagedPeer.Construct(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(156,5): warning IL2057: Java.Interop.ManagedPeer.GetParameterTypes(String): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(198,5): warning IL2057: Java.Interop.ManagedPeer.RegisterNativeMembers(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
src\Mono.Android\Android.Runtime\AndroidRuntime.cs(484,4): warning IL2092: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, String): 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, String)' don't match overridden parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.RegisterNativeMembers(JniType, Type, String)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
external\Java.Interop\src\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs(182,4): warning IL2070: Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind)' 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.

This may actually make the trimmer better in the places I decorated Type parameters with [DynamicallyAccessedMembers], but most of the places are simply suppressing the warning where we have preserved the types via other means.

Context: dotnet#5652

Building an app such as:

    dotnet new android
    dotnet build -c Release -p:SuppressTrimAnalysisWarnings=false -p:TrimmerSingleWarn=false

Results in many illink/trimmer warnings:

    external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniMarshalMemberBuilder.cs(57,4): warning IL2072: Java.Interop.JniRuntime.SetMarshalMemberBuilder(JniRuntime.CreationOptions): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(464,32): warning IL2067: Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethods(Type)'. The parameter 'marshalType' of method 'Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(414,5): warning IL2070: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicNestedTypes' in call to 'System.Type.GetNestedType(String, BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(418,5): warning IL2072: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'name' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethod(Type, String, Type[])'. The return value of method 'System.Type.GetNestedType(String, BindingFlags)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(338,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(577,5): warning IL2060: Java.Interop.JniRuntime.JniValueManager.GetObjectArrayMarshaler(Type): Call to 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic method.
    external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(523,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(537,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(559,27): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(569,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\ManagedPeer.cs(93,5): warning IL2057: Java.Interop.ManagedPeer.Construct(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(156,5): warning IL2057: Java.Interop.ManagedPeer.GetParameterTypes(String): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(198,5): warning IL2057: Java.Interop.ManagedPeer.RegisterNativeMembers(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(403,5): warning IL2075: Android.Runtime.AndroidTypeManager.CreateDynamicCallback(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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.
    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(526,8): warning IL2070: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>)' 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.
    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(540,9): warning IL2057: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(546,8): warning IL2072: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type, Type, String)'. The return value of method 'System.Type.BaseType.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.
    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(546,8): warning IL2067: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type, Type, String)'. The parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, ReadOnlySpan<Char>)' 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\src\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs(182,4): warning IL2070: Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind)' 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.
    src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(139,4): warning IL2055: Java.Interop.JavaObjectExtensions.GetInvokerType(Type): Call to 'System.Type.MakeGenericType(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.
    src\Mono.Android\Java.Interop\TypeManager.cs(169,5): warning IL2072: Java.Interop.TypeManager.Activate(IntPtr, ConstructorInfo, Object[]): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.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.
    src\Mono.Android\Java.Interop\TypeManager.cs(326,4): warning IL2070: Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])'. The parameter 'type' of method 'Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership)' 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.
    src\Mono.Android\Java.Interop\TypeManager.cs(330,4): warning IL2070: Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])'. The parameter 'type' of method 'Java.Interop.TypeManager.CreateProxy(Type, IntPtr, JniHandleOwnership)' 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.
    src\Mono.Android\Java.Interop\TypeManager.cs(126,5): warning IL2057: Java.Interop.TypeManager.GetParameterTypes(String): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    src\Mono.Android\Java.Interop\TypeManager.cs(146,4): warning IL2057: Java.Interop.TypeManager.n_Activate(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.

This commit suppresses/solves the ones in `Mono.Android.dll` and we
are left with 15 that can be solved in the xamarin/java.interop repo:

    external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniMarshalMemberBuilder.cs(57,4): warning IL2072: Java.Interop.JniRuntime.SetMarshalMemberBuilder(JniRuntime.CreationOptions): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(464,32): warning IL2067: Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethods(Type)'. The parameter 'marshalType' of method 'Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type, JniNativeMethodRegistrationArguments)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(414,5): warning IL2070: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicNestedTypes' in call to 'System.Type.GetNestedType(String, BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(418,5): warning IL2072: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType, Type, String): 'name' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethod(Type, String, Type[])'. The return value of method 'System.Type.GetNestedType(String, BindingFlags)' 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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(338,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(577,5): warning IL2060: Java.Interop.JniRuntime.JniValueManager.GetObjectArrayMarshaler(Type): Call to 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic method.
    external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(523,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(537,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(559,27): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(569,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.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\src\Java.Interop\Java.Interop\ManagedPeer.cs(93,5): warning IL2057: Java.Interop.ManagedPeer.Construct(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(156,5): warning IL2057: Java.Interop.ManagedPeer.GetParameterTypes(String): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(198,5): warning IL2057: Java.Interop.ManagedPeer.RegisterNativeMembers(IntPtr, IntPtr, IntPtr, IntPtr, IntPtr): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type.
    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(484,4): warning IL2092: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, String): 'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType, Type, String)' don't match overridden parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.RegisterNativeMembers(JniType, Type, String)'. All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.
    external\Java.Interop\src\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs(182,4): warning IL2070: Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type, ExportParameterKind)' 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.

This *may* actually make the trimmer better in the places I decorated
`Type` parameters with `[DynamicallyAccessedMembers]`, but most of
the places are simply suppressing the warning where we have preserved
the types via other means.
@jonathanpeppers jonathanpeppers requested a review from jonpryor as a code owner May 24, 2023 22:05
@jonathanpeppers
Copy link
Member Author

Only failure is InstallAndroidDependenciesTest, which is fixed in another PR.

[UnconditionalSuppressMessage ("Trimming", "IL2057", Justification = "Type.GetType() can never statically know the string value parsed from parameter 'methods'.")]
[UnconditionalSuppressMessage ("Trimming", "IL2067", Justification = "Delegate.CreateDelegate() can never statically know the string value parsed from parameter 'methods'.")]
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = "Delegate.CreateDelegate() can never statically know the string value parsed from parameter 'methods'.")]
public void RegisterNativeMembers (JniType nativeClass, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type type, ReadOnlySpan<char> methods)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-formatting wise, I think adding these parameter custom attributes makes the resulting code less readable, so I would prefer changing these into multi-line signatures:

public void RegisterNativeMembers (
        JniType nativeClass,
        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
        Type type,
        ReadOnlySpan<char> methods)
{
    // …
}

Additionally, why [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] and not [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the trimmer warning specifies which values to use.

static readonly Type[] JIConstructorSignature = new Type [] { typeof (JniObjectReference).MakeByRefType (), typeof (JniObjectReferenceOptions) };

internal static object CreateProxy (Type type, IntPtr handle, JniHandleOwnership transfer)
internal static object CreateProxy ([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, IntPtr handle, JniHandleOwnership transfer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same code-formatting comments as above.

@jonpryor jonpryor merged commit 3ab842f into dotnet:main Jun 14, 2023
@jonathanpeppers jonathanpeppers deleted the mono.android-illink branch June 14, 2023 19:02
grendello added a commit to grendello/xamarin-android that referenced this pull request Jun 15, 2023
* main:
  [Xamarin.Android.Build.Tasks] <GenerateJavaStubs/> should open readonly in some cases (dotnet#8129)
  [Mono.Android] suppress/solve more illink warnings (dotnet#8063)
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants