dotnet / java-interop

Java.Interop provides open-source bindings of Java's Java Native Interface (JNI) for use with .NET managed languages such as C#
Other
189 stars 48 forks source link

[Java.Interop] suppress IL3050 with `#pragma` #1201

Closed jonathanpeppers closed 4 months ago

jonathanpeppers commented 4 months ago

Context: https://github.com/xamarin/xamarin-android/pull/8758#discussion_r1503086757 Context: https://github.com/xamarin/java.interop/issues/1192

In 7d1e7057 and b8f6f8884, we suppressed IL3050, an AOT-related warning with:

// FIXME: https://github.com/xamarin/java.interop/issues/1192
[UnconditionalSuppressMessage ("AOT", "IL3050")]
// Problematic code here

We don't immediately plan to support NativeAOT on Android in .NET 9, so it would be nice if we could suppress the warning for now. But if anyone tried to use this code in a NativeAOT context, they could get a warning.

So instead we should use:

// FIXME: https://github.com/xamarin/java.interop/issues/1192
#pragma warning disable IL3050
// Problematic code here
#pragma warning restore IL3050

This will prevent us from introducing new IL3050 and related warnings, but if anyone consumes Java.Interop.dll from NativeAOT -- they will see the warning.

jonpryor commented 4 months ago

One the one hand, this "works"; the build for samples/Hello-NativeAOTFromJNI now has IL3050 warnings: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=9160228&view=logs&j=4ab9874f-3b82-50ef-2a6b-4340d0043621&t=3f720a70-9e37-509d-b752-3ff7ec647839

…/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs(665): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniValueManager.<GetObjectArrayMarshaler>g__MakeGenericMethod|41_0(MethodInfo,Type): Using member 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
…/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs(381): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniValueManager.<GetInvokerType>g__MakeGenericType|31_1(Type,Type[]): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
…/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs(789): AOT analysis warning IL3050: Java.Interop.JavaPeerableValueMarshaler.CreateParameterToManagedExpression(JniValueMarshalerContext,ParameterExpression,ParameterAttributes,Type): Using member 'System.Linq.Expressions.Expression.Call(Expression,String,Type[],Expression[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Calling a generic method requires dynamic code generation. This can be suppressed if the method is not generic.
…/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs(284): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniTypeManager.MakeGenericType(Type,Type): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
…/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs(277): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniTypeManager.MakeArrayType(Type): Using member 'System.Type.MakeArrayType()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available.

On the other hand, I'm not quite sure how to fix these warnings?

jonathanpeppers commented 4 months ago

I think the way to fix would be to "generate code", such that you'd perform the same calls without System.Reflection. That would be a lot of work for some of these...

I was playing with trimming issues like this here: