Skip to content

Commit 6845af4

Browse files
Fix when running in MSBuild / netstandard2.0 context
1 parent 9ac0133 commit 6845af4

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ public static string ToJniName (Type type)
167167
"java/lang/Object";
168168
}
169169

170-
static readonly Lazy<Type> IJavaPeerableType = new Lazy<Type> (() =>
171-
Type.GetType ("Java.Interop.IJavaPeerable, Java.Interop", throwOnError: true)!
172-
);
173-
174170
static string? ToJniName (Type type, ExportParameterKind exportKind)
175171
{
176172
if (type == null)
@@ -182,14 +178,29 @@ public static string ToJniName (Type type)
182178
if (type == typeof (string))
183179
return "java/lang/String";
184180

185-
if (IJavaPeerableType.Value.IsAssignableFrom (type))
181+
if (ShouldCheckSpecialExportJniType (type))
186182
return GetSpecialExportJniType (type.FullName!, exportKind);
187183

188184
return ToJniName (type, t => t.DeclaringType!, t => t.Name, GetPackageName, t => {
189185
return ToJniNameFromAttributes (t);
190186
}, _ => false);
191187
}
192188

189+
#if !NETSTANDARD2_0
190+
static readonly Lazy<Type> IJavaPeerableType = new Lazy<Type> (() =>
191+
Type.GetType ("Java.Interop.IJavaPeerable, Java.Interop", throwOnError: true)!
192+
);
193+
#endif
194+
195+
// NOTE: NETSTANDARD2_0 could be running in an MSBuild context where Java.Interop.dll is not available.
196+
// Trimming warnings are not enabled for netstandard2.0 in this project.
197+
static bool ShouldCheckSpecialExportJniType (Type type) =>
198+
#if NETSTANDARD2_0
199+
type.GetInterfaces ().Any (t => t.FullName == "Java.Interop.IJavaPeerable");
200+
#else
201+
IJavaPeerableType.Value.IsAssignableFrom (type);
202+
#endif
203+
193204
public static string ToJniName (string jniType, int rank)
194205
{
195206
if (rank == 0)

0 commit comments

Comments
 (0)