Skip to content

Commit 2fbe5c2

Browse files
committed
[Java.Interop.Tools.Cecil] Unroll TypeDefinitionRocks iterators.
1 parent fcad336 commit 2fbe5c2

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<LangVersion>8.0</LangVersion>
5+
<LangVersion>9.0</LangVersion>
66
<Nullable>enable</Nullable>
77
<SignAssembly>true</SignAssembly>
88
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/MethodDefinitionRocks.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public static MethodDefinition GetBaseDefinition (this MethodDefinition method,
2020
if (method.IsStatic || method.IsNewSlot || !method.IsVirtual)
2121
return method;
2222

23-
foreach (var baseType in method.DeclaringType.GetBaseTypes (resolver)) {
23+
TypeDefinition? baseType = method.DeclaringType;
24+
25+
while ((baseType = baseType.GetBaseType (resolver)) != null) {
2426
foreach (var m in baseType.Methods) {
2527
if (!m.IsConstructor &&
2628
m.Name == method.Name &&

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/TypeDefinitionRocks.cs

+23-6
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ public static bool IsAssignableFrom (this TypeReference type, TypeReference c, I
6565
var d = resolver.Resolve (c);
6666
if (d == null)
6767
return false;
68-
foreach (var t in d.GetTypeAndBaseTypes (resolver)) {
68+
69+
TypeDefinition? t = d;
70+
71+
while (t is not null) {
6972
if (type.FullName == t.FullName)
7073
return true;
7174
foreach (var ifaceImpl in t.Interfaces) {
7275
var i = ifaceImpl.InterfaceType;
7376
if (IsAssignableFrom (type, i, resolver))
7477
return true;
7578
}
79+
80+
t = t.GetBaseType (resolver);
7681
}
7782
return false;
7883
}
@@ -84,20 +89,26 @@ public static bool IsSubclassOf (this TypeDefinition type, string typeName, Type
8489
IsSubclassOf (type, typeName, (IMetadataResolver) cache);
8590
public static bool IsSubclassOf (this TypeDefinition type, string typeName, IMetadataResolver resolver)
8691
{
87-
foreach (var t in type.GetTypeAndBaseTypes (resolver)) {
92+
TypeDefinition? t = type;
93+
94+
while (t is not null) {
8895
if (t.FullName == typeName) {
8996
return true;
9097
}
98+
99+
t = t.GetBaseType (resolver);
91100
}
92101
return false;
93102
}
94103

95104
public static bool HasJavaPeer (this TypeDefinition type, IMetadataResolver resolver)
96105
{
97-
if (type.IsInterface && type.ImplementsInterface ("Java.Interop.IJavaPeerable", resolver))
98-
return true;
106+
if (type.IsInterface)
107+
return type.ImplementsInterface ("Java.Interop.IJavaPeerable", resolver);
99108

100-
foreach (var t in type.GetTypeAndBaseTypes (resolver)) {
109+
TypeDefinition? t = type;
110+
111+
while (t is not null) {
101112
switch (t.FullName) {
102113
case "Java.Lang.Object":
103114
case "Java.Lang.Throwable":
@@ -107,6 +118,8 @@ public static bool HasJavaPeer (this TypeDefinition type, IMetadataResolver reso
107118
default:
108119
break;
109120
}
121+
122+
t = t.GetBaseType (resolver);
110123
}
111124
return false;
112125
}
@@ -119,12 +132,16 @@ public static bool ImplementsInterface (this TypeDefinition type, string interfa
119132

120133
public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, IMetadataResolver resolver)
121134
{
122-
foreach (var t in type.GetTypeAndBaseTypes (resolver)) {
135+
TypeDefinition? t = type;
136+
137+
while (t is not null) {
123138
foreach (var i in t.Interfaces) {
124139
if (i.InterfaceType.FullName == interfaceName) {
125140
return true;
126141
}
127142
}
143+
144+
t = t.GetBaseType (resolver);
128145
}
129146
return false;
130147
}

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.Adapters/CecilImporter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ static CallableWrapperType CreateType (TypeDefinition type, IMetadataResolver re
124124
type,
125125
};
126126

127-
foreach (var bt in type.GetBaseTypes (resolver)) {
127+
TypeDefinition? bt = type;
128+
129+
while ((bt = bt.GetBaseType (resolver)) != null) {
128130
ctorTypes.Add (bt);
129131
var rattr = CecilExtensions.GetTypeRegistrationAttributes (bt).FirstOrDefault ();
130132

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public static bool IsApplication (TypeDefinition type, TypeDefinitionCache cache
438438

439439
public static bool IsApplication (TypeDefinition type, IMetadataResolver resolver)
440440
{
441-
return type.GetBaseTypes (resolver).Any (b => b.FullName == "Android.App.Application");
441+
return type.IsSubclassOf ("Android.App.Application", resolver);
442442
}
443443

444444
[Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)]
@@ -449,7 +449,7 @@ public static bool IsInstrumentation (TypeDefinition type, TypeDefinitionCache c
449449

450450
public static bool IsInstrumentation (TypeDefinition type, IMetadataResolver resolver)
451451
{
452-
return type.GetBaseTypes (resolver).Any (b => b.FullName == "Android.App.Instrumentation");
452+
return type.IsSubclassOf ("Android.App.Instrumentation", resolver);
453453
}
454454

455455
// moved from JavaTypeInfo
@@ -734,9 +734,10 @@ internal static bool IsNonStaticInnerClass (TypeDefinition? type, IMetadataResol
734734
if (!type.DeclaringType.HasJavaPeer (cache))
735735
return false;
736736

737-
foreach (var baseType in type.GetBaseTypes (cache)) {
738-
if (baseType == null)
739-
continue;
737+
TypeDefinition? baseType = type;
738+
739+
while ((baseType = baseType.GetBaseType (cache)) != null) {
740+
740741
if (!baseType.AnyCustomAttributes (typeof (RegisterAttribute)))
741742
continue;
742743

0 commit comments

Comments
 (0)