-
Notifications
You must be signed in to change notification settings - Fork 54
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
[Java.Interop] remove DynamicallyAccessedMemberTypes.Interfaces
#1285
Conversation
Context: dotnet/android#9630 As #9630 required new declarations of `DynamicallyAccessedMemberTypes.Interfaces`, we intestigated to see *why* these were needed in Java.Interop. There are two cases: var listIface = typeof (IList<>); var listType = (from iface in type.GetInterfaces ().Concat (new[]{type}) where (listIface).IsAssignableFrom (iface.IsGenericType ? iface.GetGenericTypeDefinition () : iface) select iface) .FirstOrDefault (); This first one, if `IList<>` were trimmed away. We don't actually care, as everything in this code path would still work. We can suppress the warning instead. The second case: JniValueMarshalerAttribute? ifaceAttribute = null; foreach (var iface in type.GetInterfaces ()) { marshalerAttr = iface.GetCustomAttribute<JniValueMarshalerAttribute> (); if (marshalerAttr != null) { if (ifaceAttribute != null) throw new NotSupportedException ($"There is more than one interface with custom marshaler for type {type}."); ifaceAttribute = marshalerAttr; } } if (ifaceAttribute != null) return (JniValueMarshaler) Activator.CreateInstance (ifaceAttribute.MarshalerType)!; Feels like we should be able to remove this code completely. With these changes we can remove `DynamicallyAccessedMemberTypes.Interfaces`. I also introduced `build-tools/trim-analyzers/trim-analyzers.props` that will setup the appropriate trimmer MSBuild properties to make trimmer warnings an error. This should keep us from accidentally creating warnings. I only use this setting in projects that were already using `$(EnableAotAnalyzer)`.
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.
Copilot reviewed 5 out of 12 changed files in this pull request and generated no comments.
Files not reviewed (7)
- build-tools/trim-analyzers/trim-analyzers.props: Language not supported
- src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings.csproj: Language not supported
- src/Java.Interop/Java.Interop.csproj: Language not supported
- src/Java.Interop/Java.Interop/JniBuiltinMarshalers.cs: Evaluated as low risk
- src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs: Evaluated as low risk
- src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs: Evaluated as low risk
- src/Java.Interop/Java.Interop/JniStringValueMarshaler.cs: Evaluated as low risk
Comments suppressed due to low confidence (2)
src/Java.Interop/Java.Interop/JniValueMarshaler.cs:123
- The removal of DynamicallyAccessedMemberTypes.Interfaces might cause issues if any code relies on dynamically accessed interfaces. Ensure that all necessary interfaces are still accessible and properly handled.
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
src/Java.Interop/Java.Interop/JniValueMarshaler.cs:135
- Ensure that there are sufficient tests covering the behavior of CreateValue when only constructors are dynamically accessed. This is to ensure that no functionality relying on interfaces is broken.
public abstract object? CreateValue (ref JniObjectReference reference, JniObjectReferenceOptions options, [DynamicallyAccessedMembers (Constructors)] Type? targetType = null);
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
@jonpryor I think the changes here + dotnet/android#9630 cause a crash in
|
The cause of the crash is here:
The default
…which we don't know how to dispose, because we don't know what it is. The fix is…another utility function! partial class JNIEnv {
internal static JniObjectReference CreateJniObjectReference (IntPtr handle, JniHandleOwnership transfer)
{
if (transfer.HasFlag (JniHandleOwnership.TransferLocalRef))
return new JniObjectReference (handle, JniObjectReferenceType.Local);
if (transfer.HasFlag (JniHandleOwnership.TransferGlobalRef))
return new JniObjectReference (handle, JniObjectReferenceType.Global);
return new JniObjectReference (handle); // there be possible dragons here!
}
} Then var reference = JNIEnv.CreateJniObjectReference (handle, transfer); |
Changes: dotnet/java-interop@f800ea5...2c06b3c * dotnet/java-interop@2c06b3c2: [Java.Interop] remove `DynamicallyAccessedMemberTypes.Interfaces` (dotnet/java-interop#1285)
Changes: dotnet/java-interop@f800ea5...2c06b3c * dotnet/java-interop@2c06b3c2: [Java.Interop] remove `DynamicallyAccessedMemberTypes.Interfaces` (dotnet/java-interop#1285)
Changes: dotnet/java-interop@f800ea5...2c06b3c * dotnet/java-interop@2c06b3c2: [Java.Interop] remove `DynamicallyAccessedMemberTypes.Interfaces` (dotnet/java-interop#1285)
Changes: dotnet/java-interop@f800ea5...2c06b3c * dotnet/java-interop@2c06b3c2: [Java.Interop] remove `DynamicallyAccessedMemberTypes.Interfaces` (dotnet/java-interop#1285)
Context: dotnet/android#9630
As #9630 required new declarations of
DynamicallyAccessedMemberTypes.Interfaces
, we intestigated to see why these were needed in Java.Interop.There are two cases:
This first one, if
IList<>
were trimmed away. We don't actually care, as everything in this code path would still work. We can suppress the warning instead.The second case:
Feels like we should be able to remove this code completely.
With these changes we can remove
DynamicallyAccessedMemberTypes.Interfaces
.I also introduced
build-tools/trim-analyzers/trim-analyzers.props
that will setup the appropriate trimmer MSBuild properties to make trimmer warnings an error. This should keep us from accidentally creating warnings. I only use this setting in projects that were already using$(EnableAotAnalyzer)
.