Skip to content

Commit 3a1efaa

Browse files
committed
Subclass TypeDefinitionCache
This implements @jonathanpepper's idea to use the linker's built-in Resolve cache without duplicating the extension methods from java.interop. By overriding TypeDefinitionCache, we can continue using the existing extension methods that take TypeDefinitionCache, and this leaves open the option to modify the extension method signatures to instead take an IMetadataResolver as in dotnet/java-interop#842.
1 parent 20d1ca6 commit 3a1efaa

File tree

9 files changed

+37
-208
lines changed

9 files changed

+37
-208
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
using Mono.Linker;
21
using Mono.Linker.Steps;
2+
using Microsoft.Android.Sdk.ILLink;
33

44
namespace Mono.Linker
55
{
66
public class BaseMarkHandler : IMarkHandler
77
{
88
protected LinkContext Context;
99
protected AnnotationStore Annotations => Context?.Annotations;
10+
protected LinkContextMetadataResolver cache;
1011

1112
public virtual void Initialize (LinkContext context, MarkContext markContext)
1213
{
1314
Context = context;
15+
cache = new LinkContextMetadataResolver (context);
1416
}
1517
}
1618
}

src/Microsoft.Android.Sdk.ILLink/LinkContextExtensions.cs

Lines changed: 0 additions & 161 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Java.Interop.Tools.Cecil;
2+
using Mono.Cecil;
3+
using Mono.Linker;
4+
5+
namespace Microsoft.Android.Sdk.ILLink
6+
{
7+
public class LinkContextMetadataResolver : TypeDefinitionCache {
8+
LinkContext context;
9+
10+
public LinkContextMetadataResolver (LinkContext context)
11+
{
12+
this.context = context;
13+
}
14+
15+
public override TypeDefinition Resolve (TypeReference typeReference)
16+
{
17+
return context.ResolveTypeDefinition (typeReference);
18+
}
19+
}
20+
}

src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
<!--Other Xamarin.Android / Java.Interop files-->
4343
<Compile Include="..\..\external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil\CustomAttributeProviderRocks.cs" Link="Java.Interop\CustomAttributeProviderRocks.cs" />
44+
<Compile Include="..\..\external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil\MethodDefinitionRocks.cs" Link="Java.Interop\MethodDefinitionRocks.cs" />
45+
<Compile Include="..\..\external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil\TypeDefinitionCache.cs" Link="Java.Interop\TypeDefinitionCache.cs" />
46+
<Compile Include="..\..\external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil\TypeDefinitionRocks.cs" Link="Java.Interop\TypeDefinitionRocks.cs" />
4447
<Compile Include="..\Xamarin.Android.Build.Tasks\Utilities\MonoAndroidHelper.Linker.cs" Link="Utilities\MonoAndroidHelper.Linker.cs" />
4548
</ItemGroup>
4649
<ItemGroup>

src/Microsoft.Android.Sdk.ILLink/PreserveRegistrations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void ProcessMethod (MethodDefinition method)
7575
if (!method.TryGetRegisterMember (out var member, out var nativeMethod, out var signature)) {
7676
if (PreserveJniMarshalMethods () &&
7777
method.DeclaringType.GetMarshalMethodsType () != null &&
78-
Context.TryGetBaseOrInterfaceRegisterMember (method, out member, out nativeMethod, out signature)) {
78+
method.TryGetBaseOrInterfaceRegisterMember (cache, out member, out nativeMethod, out signature)) {
7979
preserveJniMarshalMethodOnly = true;
8080
} else {
8181
return;

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ namespace MonoDroid.Tuner
1818
{
1919
public class AddKeepAlivesStep : BaseStep
2020
{
21-
#if !NET5_LINKER
21+
22+
#if NET5_LINKER
23+
LinkContextMetadataResolver cache;
24+
25+
protected override void Process ()
26+
{
27+
cache = new LinkContextMetadataResolver (Context);
28+
}
29+
#else // !NET5_LINKER
2230
readonly TypeDefinitionCache cache;
2331

2432
public AddKeepAlivesStep (TypeDefinitionCache cache)
@@ -70,11 +78,7 @@ static void AddNestedTypes (List<TypeDefinition> types, TypeDefinition type)
7078

7179
bool MightNeedFix (TypeDefinition type)
7280
{
73-
#if NET5_LINKER
74-
return !type.IsAbstract && Context.IsSubclassOf (type, "Java.Lang.Object");
75-
#else // !NET5_LINKER
7681
return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", cache);
77-
#endif // !NET5_LINKER
7882
}
7983

8084
MethodDefinition methodKeepAlive = null;

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
using Java.Interop;
1212
using Java.Interop.Tools.Cecil;
1313

14-
#if NET5_LINKER
15-
using Microsoft.Android.Sdk.ILLink;
16-
#endif // NET5_LINKER
17-
1814
namespace MonoDroid.Tuner {
1915

2016
static class Extensions {
@@ -259,11 +255,7 @@ public static TypeDefinition GetMarshalMethodsType (this TypeDefinition type)
259255
return null;
260256
}
261257

262-
#if NET5_LINKER
263-
public static bool TryGetBaseOrInterfaceRegisterMember (this LinkContext context, MethodDefinition method, out string member, out string nativeMethod, out string signature)
264-
#else // !NET5_LINKER
265258
public static bool TryGetBaseOrInterfaceRegisterMember (this MethodDefinition method, TypeDefinitionCache cache, out string member, out string nativeMethod, out string signature)
266-
#endif // !NET5_LINKER
267259
{
268260
var type = method.DeclaringType;
269261

@@ -272,11 +264,7 @@ public static bool TryGetBaseOrInterfaceRegisterMember (this MethodDefinition me
272264
if (method.IsConstructor || type == null || !type.HasNestedTypes)
273265
return false;
274266

275-
#if NET5_LINKER
276-
var m = context.GetBaseDefinition (method);
277-
#else // !NET5_LINKER
278267
var m = method.GetBaseDefinition (cache);
279-
#endif // !NET5_LINKER
280268

281269
while (m != null) {
282270
if (m == method)
@@ -287,11 +275,7 @@ public static bool TryGetBaseOrInterfaceRegisterMember (this MethodDefinition me
287275
if (m.TryGetRegisterMember (out member, out nativeMethod, out signature))
288276
return true;
289277

290-
#if NET5_LINKER
291-
context.GetBaseDefinition (m);
292-
#else // !NET5_LINKER
293278
m = m.GetBaseDefinition (cache);
294-
#endif // !NET5_LINKER
295279
}
296280

297281
if (!method.DeclaringType.HasInterfaces || !method.IsNewSlot)
@@ -306,30 +290,19 @@ public static bool TryGetBaseOrInterfaceRegisterMember (this MethodDefinition me
306290
continue;
307291

308292
foreach (var im in itype.Methods)
309-
#if NET5_LINKER
310-
if (context.IsEqual (im, method))
311-
#else // !NET5_LINKER
312293
if (im.IsEqual (method, cache))
313-
#endif // !NET5_LINKER
314294
return im.TryGetRegisterMember (out member, out nativeMethod, out signature);
315295
}
316296

317297
return false;
318298
}
319299

320-
#if NET5_LINKER
321-
public static bool IsEqual (this LinkContext context, MethodDefinition m1, MethodDefinition m2)
322-
#else // !NET5_LINKER
323300
public static bool IsEqual (this MethodDefinition m1, MethodDefinition m2, TypeDefinitionCache cache)
324-
#endif // !NET5_LINKER
325301
{
326302
if (m1.Name != m2.Name || m1.ReturnType.Name != m2.ReturnType.Name)
327303
return false;
328-
#if NET5_LINKER
329-
return context.AreParametersCompatibleWith (m1.Parameters, m2.Parameters);
330-
#else // !NET5_LINKER
304+
331305
return m1.Parameters.AreParametersCompatibleWith (m2.Parameters, cache);
332-
#endif // !NET5_LINKER
333306
}
334307

335308
public static bool TryGetMarshalMethod (this MethodDefinition method, string nativeMethod, string signature, out MethodDefinition marshalMethod)

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,7 @@ bool IsProductOrSdkAssembly (AssemblyDefinition assembly)
136136

137137
bool MightNeedFix (TypeDefinition type)
138138
{
139-
#if NET5_LINKER
140-
return !type.IsAbstract && Context.IsSubclassOf (type, "Java.Lang.Object");
141-
#else // !NET5_LINKER
142139
return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", cache);
143-
#endif // !NET5_LINKER
144140
}
145141

146142
static bool CompareTypes (TypeReference iType, TypeReference tType)
@@ -249,11 +245,7 @@ bool FixAbstractMethods (TypeDefinition type)
249245

250246
bool rv = false;
251247
List<MethodDefinition> typeMethods = new List<MethodDefinition> (type.Methods);
252-
#if NET5_LINKER
253-
foreach (var baseType in Context.GetBaseTypes (type))
254-
#else // !NET5_LINKER
255248
foreach (var baseType in type.GetBaseTypes (cache))
256-
#endif // !NET5_LINKER
257249
typeMethods.AddRange (baseType.Methods);
258250

259251
foreach (var ifaceInfo in type.Interfaces) {

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveAttributes.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
using Mono.Linker;
77
using Mono.Linker.Steps;
88

9-
#if NET5_LINKER
10-
using Microsoft.Android.Sdk.ILLink;
11-
#endif
12-
139
using Mono.Tuner;
1410
using Mobile.Tuner;
1511

0 commit comments

Comments
 (0)