Skip to content

Commit c6e3893

Browse files
[Java.Interop] use Type.GetType() to find MarshalMemberBuilder (#1193)
Context: #1184 (comment) Previously we were using `Assembly.Load()` and `Assembly.GetType()`, which require us to ignore various trimmer warnings. During code review, @vitek-karas asked: > Is there a reason > [`JniRuntime.JniMarshalMemberBuilder.SetMarshalMemberBuilder()`] > can't use > `Type.GetType("Java.Interop.MarshalMemberBuilder, Java.Interop.Export")`. > That is something both trimmer and AOT understand and will handle > without warnings and correctly preserve the .ctor in the call to > `Activator.CreateInstance`. Indeed, there isn't a reason to avoid `Type.GetType()` in `JniRuntime.JniMarshalMemberBuilder.SetMarshalMemberBuilder()`. If we use `Type.GetType()` with a constant string instead, the trimmer knows how to handle this properly. (The code is also simpler.)
1 parent 7d1e705 commit c6e3893

File tree

1 file changed

+1
-14
lines changed

1 file changed

+1
-14
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ public JniMarshalMemberBuilder MarshalMemberBuilder {
2727
}
2828
}
2929

30-
[System.Diagnostics.CodeAnalysis.SuppressMessage ("Design", "CA1031:Do not catch general exception types", Justification = "the *.Export assemblies are optional, so we don't care when they cannot be loaded (they are presumably missing)")]
31-
#if NET
3230
[DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, "Java.Interop.MarshalMemberBuilder", "Java.Interop.Export")]
33-
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the constructor.")]
34-
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = "DynamicDependency should preserve the constructor.")]
35-
[UnconditionalSuppressMessage ("Trimming", "IL2035", Justification = "Java.Interop.Export.dll is not always present.")]
36-
#endif
3731
partial void SetMarshalMemberBuilder (CreationOptions options)
3832
{
3933
if (!options.UseMarshalMemberBuilder) {
@@ -45,14 +39,7 @@ partial void SetMarshalMemberBuilder (CreationOptions options)
4539
return;
4640
}
4741

48-
Assembly jie;
49-
try {
50-
jie = Assembly.Load (new AssemblyName ("Java.Interop.Export"));
51-
} catch (Exception e) {
52-
System.Diagnostics.Debug.WriteLine ($"Java.Interop.Export assembly was not loaded: {e}");
53-
return;
54-
}
55-
var t = jie.GetType ("Java.Interop.MarshalMemberBuilder");
42+
var t = Type.GetType ("Java.Interop.MarshalMemberBuilder, Java.Interop.Export", throwOnError: false);
5643
if (t == null)
5744
throw new InvalidOperationException ("Could not find Java.Interop.MarshalMemberBuilder from Java.Interop.Export.dll!");
5845
var b = (JniMarshalMemberBuilder) Activator.CreateInstance (t)!;

0 commit comments

Comments
 (0)