You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[java.interop] address some "easy" trimmer warnings
Context: #1157
On the path to enabling `IsAotCompatible`, we can enable some settings
to get started:
<IsTrimmable>true</IsTrimmable>
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
<EnableAotAnalyzer>true</EnableAotAnalyzer>
This opts into the analyzers without declaring the assembly is fully
AOT-compatible.
We can also *remove* where we manually specify:
[assembly: AssemblyMetadata ("IsTrimmable", "True")]
The `$(IsTrimmable)` property does this for us, as well as enabling
analyzers.
Starting out, I got 33 warnings: this is an attempt to address the
ones that don't require too much thinking.
This results in 22 warnings remaining. I was also able to solve all
instances of `IL2067;IL2070`, so I turned these two into build errors
to ensure they don't come back.
~~ Example Warnings ~~
`System.Linq.Expression` usage:
src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(672,58):
warning IL2026: Using member 'System.Linq.Expressions.Expression.Property(Expression, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed.
I decorated this one with:
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
`Type.GetNestedType()` usage:
src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(447,28):
warning IL2070: '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.
Added:
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
`Activator.CreateInstance()` usage:
src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(542,33): warning IL2072: '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.
Added:
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
~~ Code that Actually Changed ~~
As I enabled more attributes, these snowball into more and more
warnings! I eventually had to make `GetPeerType()` look like:
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
static Type GetPeerType ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)
The analyzer was not able to understand code like:
static readonly KeyValuePair<Type, Type>[] PeerTypeMappings = new []{
new KeyValuePair<Type, Type>(typeof (object), typeof (JavaObject)),
new KeyValuePair<Type, Type>(typeof (IJavaPeerable), typeof (JavaObject)),
new KeyValuePair<Type, Type>(typeof (Exception), typeof (JavaException)),
};
//...
foreach (var m in PeerTypeMappings) {
if (m.Key == type)
return m.Value;
}
Simply removing the `PeerTypeMappings` array and using `if` statements
solved the warnings. This may be the only real code change if any.
0 commit comments