diff --git a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/MethodDefinitionRocks.cs b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/MethodDefinitionRocks.cs index 9b9ae0974..cdbdcaf02 100644 --- a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/MethodDefinitionRocks.cs +++ b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/MethodDefinitionRocks.cs @@ -9,14 +9,13 @@ namespace Java.Interop.Tools.Cecil { public static class MethodDefinitionRocks { - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static MethodDefinition GetBaseDefinition (this MethodDefinition method) => - GetBaseDefinition (method, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static MethodDefinition GetBaseDefinition (this MethodDefinition method) => throw new NotSupportedException (); - public static MethodDefinition GetBaseDefinition (this MethodDefinition method, TypeDefinitionCache? cache) => - GetBaseDefinition (method, (IMetadataResolver?) cache); + public static MethodDefinition GetBaseDefinition (this MethodDefinition method, TypeDefinitionCache cache) => + GetBaseDefinition (method, (IMetadataResolver) cache); - public static MethodDefinition GetBaseDefinition (this MethodDefinition method, IMetadataResolver? resolver) + public static MethodDefinition GetBaseDefinition (this MethodDefinition method, IMetadataResolver resolver) { if (method.IsStatic || method.IsNewSlot || !method.IsVirtual) return method; @@ -34,14 +33,13 @@ public static MethodDefinition GetBaseDefinition (this MethodDefinition method, return method; } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static IEnumerable GetOverriddenMethods (MethodDefinition method, bool inherit) => - GetOverriddenMethods (method, inherit, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static IEnumerable GetOverriddenMethods (MethodDefinition method, bool inherit) => throw new NotSupportedException (); - public static IEnumerable GetOverriddenMethods (MethodDefinition method, bool inherit, TypeDefinitionCache? cache) => - GetOverriddenMethods (method, inherit, (IMetadataResolver?) cache); + public static IEnumerable GetOverriddenMethods (MethodDefinition method, bool inherit, TypeDefinitionCache cache) => + GetOverriddenMethods (method, inherit, (IMetadataResolver) cache); - public static IEnumerable GetOverriddenMethods (MethodDefinition method, bool inherit, IMetadataResolver? resolver) + public static IEnumerable GetOverriddenMethods (MethodDefinition method, bool inherit, IMetadataResolver resolver) { yield return method; if (inherit) { @@ -53,14 +51,13 @@ public static IEnumerable GetOverriddenMethods (MethodDefiniti } } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool AreParametersCompatibleWith (this Collection a, Collection b) => - AreParametersCompatibleWith (a, b, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool AreParametersCompatibleWith (this Collection a, Collection b) => throw new NotSupportedException (); - public static bool AreParametersCompatibleWith (this Collection a, Collection b, TypeDefinitionCache? cache) => - AreParametersCompatibleWith (a, b, (IMetadataResolver?) cache); + public static bool AreParametersCompatibleWith (this Collection a, Collection b, TypeDefinitionCache cache) => + AreParametersCompatibleWith (a, b, (IMetadataResolver) cache); - public static bool AreParametersCompatibleWith (this Collection a, Collection b, IMetadataResolver? resolver) + public static bool AreParametersCompatibleWith (this Collection a, Collection b, IMetadataResolver resolver) { if (a.Count != b.Count) return false; @@ -75,7 +72,7 @@ public static bool AreParametersCompatibleWith (this Collection - GetBaseType (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static TypeDefinition? GetBaseType (this TypeDefinition type) => throw new NotSupportedException (); - public static TypeDefinition? GetBaseType (this TypeDefinition type, TypeDefinitionCache? cache) => - GetBaseType (type, (IMetadataResolver?) cache); + public static TypeDefinition? GetBaseType (this TypeDefinition type, TypeDefinitionCache cache) => + GetBaseType (type, (IMetadataResolver) cache); - public static TypeDefinition? GetBaseType (this TypeDefinition type, IMetadataResolver? resolver) + public static TypeDefinition? GetBaseType (this TypeDefinition type, IMetadataResolver resolver) { var bt = type.BaseType; if (bt == null) return null; - if (resolver != null) - return resolver.Resolve (bt); - return bt.Resolve (); + return resolver.Resolve (bt); } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static IEnumerable GetTypeAndBaseTypes (this TypeDefinition type) => - GetTypeAndBaseTypes (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static IEnumerable GetTypeAndBaseTypes (this TypeDefinition type) => throw new NotSupportedException (); - public static IEnumerable GetTypeAndBaseTypes (this TypeDefinition type, TypeDefinitionCache? cache) => - GetTypeAndBaseTypes (type, (IMetadataResolver?) cache); + public static IEnumerable GetTypeAndBaseTypes (this TypeDefinition type, TypeDefinitionCache cache) => + GetTypeAndBaseTypes (type, (IMetadataResolver) cache); - public static IEnumerable GetTypeAndBaseTypes (this TypeDefinition type, IMetadataResolver? resolver) + public static IEnumerable GetTypeAndBaseTypes (this TypeDefinition type, IMetadataResolver resolver) { TypeDefinition? t = type; @@ -41,14 +37,13 @@ public static IEnumerable GetTypeAndBaseTypes (this TypeDefiniti } } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static IEnumerable GetBaseTypes (this TypeDefinition type) => - GetBaseTypes (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static IEnumerable GetBaseTypes (this TypeDefinition type) => throw new NotSupportedException(); - public static IEnumerable GetBaseTypes (this TypeDefinition type, TypeDefinitionCache? cache) => - GetBaseTypes (type, (IMetadataResolver?) cache); + public static IEnumerable GetBaseTypes (this TypeDefinition type, TypeDefinitionCache cache) => + GetBaseTypes (type, (IMetadataResolver) cache); - public static IEnumerable GetBaseTypes (this TypeDefinition type, IMetadataResolver? resolver) + public static IEnumerable GetBaseTypes (this TypeDefinition type, IMetadataResolver resolver) { TypeDefinition? t = type; @@ -57,18 +52,17 @@ public static IEnumerable GetBaseTypes (this TypeDefinition type } } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool IsAssignableFrom (this TypeReference type, TypeReference c) => - IsAssignableFrom (type, c, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool IsAssignableFrom (this TypeReference type, TypeReference c) => throw new NotSupportedException (); - public static bool IsAssignableFrom (this TypeReference type, TypeReference c, TypeDefinitionCache? cache) => - IsAssignableFrom (type, c, (IMetadataResolver?) cache); + public static bool IsAssignableFrom (this TypeReference type, TypeReference c, TypeDefinitionCache cache) => + IsAssignableFrom (type, c, (IMetadataResolver) cache); - public static bool IsAssignableFrom (this TypeReference type, TypeReference c, IMetadataResolver? resolver) + public static bool IsAssignableFrom (this TypeReference type, TypeReference c, IMetadataResolver resolver) { if (type.FullName == c.FullName) return true; - var d = (resolver?.Resolve (c)) ?? c.Resolve (); + var d = resolver.Resolve (c); if (d == null) return false; foreach (var t in d.GetTypeAndBaseTypes (resolver)) { @@ -83,13 +77,12 @@ public static bool IsAssignableFrom (this TypeReference type, TypeReference c, I return false; } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool IsSubclassOf (this TypeDefinition type, string typeName) => - IsSubclassOf (type, typeName, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool IsSubclassOf (this TypeDefinition type, string typeName) => throw new NotSupportedException (); - public static bool IsSubclassOf (this TypeDefinition type, string typeName, TypeDefinitionCache? cache) => - IsSubclassOf (type, typeName, (IMetadataResolver?) cache); - public static bool IsSubclassOf (this TypeDefinition type, string typeName, IMetadataResolver? resolver) + public static bool IsSubclassOf (this TypeDefinition type, string typeName, TypeDefinitionCache cache) => + IsSubclassOf (type, typeName, (IMetadataResolver) cache); + public static bool IsSubclassOf (this TypeDefinition type, string typeName, IMetadataResolver resolver) { foreach (var t in type.GetTypeAndBaseTypes (resolver)) { if (t.FullName == typeName) { @@ -99,14 +92,13 @@ public static bool IsSubclassOf (this TypeDefinition type, string typeName, IMet return false; } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool ImplementsInterface (this TypeDefinition type, string interfaceName) => - ImplementsInterface (type, interfaceName, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool ImplementsInterface (this TypeDefinition type, string interfaceName) => throw new NotSupportedException (); - public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, TypeDefinitionCache? cache) => - ImplementsInterface (type, interfaceName, (IMetadataResolver?) cache); + public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, TypeDefinitionCache cache) => + ImplementsInterface (type, interfaceName, (IMetadataResolver) cache); - public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, IMetadataResolver? resolver) + public static bool ImplementsInterface (this TypeDefinition type, string interfaceName, IMetadataResolver resolver) { foreach (var t in type.GetTypeAndBaseTypes (resolver)) { foreach (var i in t.Interfaces) { @@ -118,27 +110,25 @@ public static bool ImplementsInterface (this TypeDefinition type, string interfa return false; } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string GetPartialAssemblyName (this TypeReference type) => - GetPartialAssemblyName (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string GetPartialAssemblyName (this TypeReference type) => throw new NotSupportedException (); - public static string GetPartialAssemblyName (this TypeReference type, TypeDefinitionCache? cache) => - GetPartialAssemblyName (type, (IMetadataResolver?) cache); + public static string GetPartialAssemblyName (this TypeReference type, TypeDefinitionCache cache) => + GetPartialAssemblyName (type, (IMetadataResolver) cache); - public static string GetPartialAssemblyName (this TypeReference type, IMetadataResolver? resolver) + public static string GetPartialAssemblyName (this TypeReference type, IMetadataResolver resolver) { - TypeDefinition? def = (resolver?.Resolve (type)) ?? type.Resolve (); + TypeDefinition? def = resolver.Resolve (type); return (def ?? type).Module.Assembly.Name.Name; } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string GetPartialAssemblyQualifiedName (this TypeReference type) => - GetPartialAssemblyQualifiedName (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string GetPartialAssemblyQualifiedName (this TypeReference type) => throw new NotSupportedException (); - public static string GetPartialAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache? cache) => - GetPartialAssemblyQualifiedName (type, (IMetadataResolver?) cache); + public static string GetPartialAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache cache) => + GetPartialAssemblyQualifiedName (type, (IMetadataResolver) cache); - public static string GetPartialAssemblyQualifiedName (this TypeReference type, IMetadataResolver? resolver) + public static string GetPartialAssemblyQualifiedName (this TypeReference type, IMetadataResolver resolver) { return string.Format ("{0}, {1}", // Cecil likes to use '/' as the nested type separator, while @@ -147,16 +137,15 @@ public static string GetPartialAssemblyQualifiedName (this TypeReference type, I type.GetPartialAssemblyName (resolver)); } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string GetAssemblyQualifiedName (this TypeReference type) => - GetAssemblyQualifiedName (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string GetAssemblyQualifiedName (this TypeReference type) => throw new NotSupportedException (); - public static string GetAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache? cache) => - GetAssemblyQualifiedName (type, (IMetadataResolver?) cache); + public static string GetAssemblyQualifiedName (this TypeReference type, TypeDefinitionCache cache) => + GetAssemblyQualifiedName (type, (IMetadataResolver) cache); - public static string GetAssemblyQualifiedName (this TypeReference type, IMetadataResolver? resolver) + public static string GetAssemblyQualifiedName (this TypeReference type, IMetadataResolver resolver) { - TypeDefinition? def = (resolver?.Resolve (type)) ?? type.Resolve (); + TypeDefinition? def = resolver.Resolve (type); return string.Format ("{0}, {1}", // Cecil likes to use '/' as the nested type separator, while // Reflection uses '+' as the nested type separator. Use Reflection. diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs index d102d57a6..cd489d00d 100644 --- a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs @@ -33,7 +33,7 @@ public abstract class JavaCallableMethodClassifier public class JavaCallableWrapperGenerator { class JavaFieldInfo { - public JavaFieldInfo (MethodDefinition method, string fieldName, IMetadataResolver? resolver) + public JavaFieldInfo (MethodDefinition method, string fieldName, IMetadataResolver resolver) { this.FieldName = fieldName; InitializerName = method.Name; @@ -41,7 +41,7 @@ public JavaFieldInfo (MethodDefinition method, string fieldName, IMetadataResolv ?? throw new ArgumentException ($"Could not get JNI signature for method `{method.Name}`", nameof (method)); IsStatic = method.IsStatic; Access = method.Attributes & MethodAttributes.MemberAccessMask; - Annotations = GetAnnotationsString ("\t", method.CustomAttributes); + Annotations = GetAnnotationsString ("\t", method.CustomAttributes, resolver); } public MethodAttributes Access { get; private set; } @@ -69,25 +69,23 @@ public string GetJavaAccess () readonly IMetadataResolver cache; readonly JavaCallableMethodClassifier? methodClassifier; - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public JavaCallableWrapperGenerator (TypeDefinition type, Action log) - : this (type, log, resolver: null, methodClassifier: null) - { } + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public JavaCallableWrapperGenerator (TypeDefinition type, Action log) => throw new NotSupportedException (); - public JavaCallableWrapperGenerator (TypeDefinition type, Action log, TypeDefinitionCache? cache) - : this (type, log, (IMetadataResolver?) cache, methodClassifier: null) + public JavaCallableWrapperGenerator (TypeDefinition type, Action log, TypeDefinitionCache cache) + : this (type, log, (IMetadataResolver) cache, methodClassifier: null) { } - public JavaCallableWrapperGenerator (TypeDefinition type, Action log, TypeDefinitionCache? cache, JavaCallableMethodClassifier? methodClassifier) - : this (type, log, (IMetadataResolver?) cache, methodClassifier) + public JavaCallableWrapperGenerator (TypeDefinition type, Action log, TypeDefinitionCache cache, JavaCallableMethodClassifier? methodClassifier) + : this (type, log, (IMetadataResolver) cache, methodClassifier) { } - public JavaCallableWrapperGenerator (TypeDefinition type, Action log, IMetadataResolver? resolver) + public JavaCallableWrapperGenerator (TypeDefinition type, Action log, IMetadataResolver resolver) : this (type, log, resolver, methodClassifier: null) { } - public JavaCallableWrapperGenerator (TypeDefinition type, Action log, IMetadataResolver? resolver, JavaCallableMethodClassifier? methodClassifier) + public JavaCallableWrapperGenerator (TypeDefinition type, Action log, IMetadataResolver resolver, JavaCallableMethodClassifier? methodClassifier) : this (type, null, log, resolver, methodClassifier) { AddNestedTypes (type); @@ -133,7 +131,7 @@ void AddNestedTypes (TypeDefinition type) HasExport |= children.Any (t => t.HasExport); } - JavaCallableWrapperGenerator (TypeDefinition type, string? outerType, Action log, IMetadataResolver? resolver, JavaCallableMethodClassifier? methodClassifier = null) + JavaCallableWrapperGenerator (TypeDefinition type, string? outerType, Action log, IMetadataResolver resolver, JavaCallableMethodClassifier? methodClassifier = null) { this.methodClassifier = methodClassifier; this.type = type; @@ -175,20 +173,23 @@ void AddNestedTypes (TypeDefinition type) } } - foreach (MethodDefinition imethod in type.Interfaces.Select (ifaceInfo => ifaceInfo.InterfaceType) - .Select (r => { - var d = r.Resolve (); - if (d == null) - Diagnostic.Error (4204, - LookupSource (type), - Localization.Resources.JavaCallableWrappers_XA4204, - r.FullName); - return d; - }) - .Where (d => d != null && GetTypeRegistrationAttributes (d).Any ()) - .SelectMany (d => d!.Methods) - .Where (m => !m.IsStatic)) { - AddMethod (imethod, imethod); + foreach (InterfaceImplementation ifaceInfo in type.Interfaces) { + var typeReference = ifaceInfo.InterfaceType; + var typeDefinition = cache.Resolve (typeReference); + if (typeDefinition == null) { + Diagnostic.Error (4204, + LookupSource (type), + Localization.Resources.JavaCallableWrappers_XA4204, + typeReference.FullName); + continue; + } + if (!GetTypeRegistrationAttributes (typeDefinition).Any ()) + continue; + foreach (MethodDefinition imethod in typeDefinition.Methods) { + if (imethod.IsStatic) + continue; + AddMethod (imethod, imethod); + } } var ctorTypes = new List () { @@ -303,7 +304,7 @@ void AddConstructor (MethodDefinition ctor, TypeDefinition type, string? outerTy if (rattr != null) { if (ctors.Any (c => c.JniSignature == rattr.Signature)) return; - ctors.Add (new Signature (ctor, rattr, managedParameters, outerType)); + ctors.Add (new Signature (ctor, rattr, managedParameters, outerType, cache)); curCtors.Add (ctor); return; } @@ -473,7 +474,7 @@ void AddMethod (MethodDefinition? registeredMethod, MethodDefinition implemented Diagnostic.Error (4217, LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4217, attr.Name); bool shouldBeDynamicallyRegistered = methodClassifier?.ShouldBeDynamicallyRegistered (type, registeredMethod, implementedMethod, attr.OriginAttribute) ?? true; - var msig = new Signature (implementedMethod, attr, shouldBeDynamicallyRegistered); + var msig = new Signature (implementedMethod, attr, cache, shouldBeDynamicallyRegistered); if (!registeredMethod.IsConstructor && !methods.Any (m => m.Name == msig.Name && m.Params == msig.Params)) methods.Add (msig); } @@ -601,17 +602,17 @@ public void Generate (string outputPath) } } - static string GetAnnotationsString (string indent, IEnumerable atts) + static string GetAnnotationsString (string indent, IEnumerable atts, IMetadataResolver resolver) { var sw = new StringWriter (); - WriteAnnotations (indent, sw, atts); + WriteAnnotations (indent, sw, atts, resolver); return sw.ToString (); } - static void WriteAnnotations (string indent, TextWriter sw, IEnumerable atts) + static void WriteAnnotations (string indent, TextWriter sw, IEnumerable atts, IMetadataResolver resolver) { foreach (var ca in atts) { - var catype = ca.AttributeType.Resolve (); + var catype = resolver.Resolve (ca.AttributeType); var tca = catype.CustomAttributes.FirstOrDefault (a => a.AttributeType.FullName == "Android.Runtime.AnnotationAttribute"); if (tca != null) { sw.Write (indent); @@ -655,7 +656,7 @@ void GenerateHeader (TextWriter sw) sw.WriteLine (); // class annotations. - WriteAnnotations ("", sw, type.CustomAttributes); + WriteAnnotations ("", sw, type.CustomAttributes, cache); sw.WriteLine ("public " + (type.IsAbstract ? "abstract " : "") + "class " + name); @@ -673,15 +674,13 @@ void GenerateHeader (TextWriter sw) sw.Write ("mono.android.IGCUserPeer"); break; } - IEnumerable ifaces = type.Interfaces.Select (ifaceInfo => ifaceInfo.InterfaceType) - .Select (r => r.Resolve ()) - .Where (d => GetTypeRegistrationAttributes (d).Any ()); - if (ifaces.Any ()) { - foreach (TypeDefinition iface in ifaces) { - sw.WriteLine (","); - sw.Write ("\t\t"); - sw.Write (GetJavaTypeName (iface, cache)); - } + foreach (var ifaceInfo in type.Interfaces) { + var iface = cache.Resolve(ifaceInfo.InterfaceType); + if (!GetTypeRegistrationAttributes (iface).Any ()) + continue; + sw.WriteLine (","); + sw.Write ("\t\t"); + sw.Write (GetJavaTypeName (iface, cache)); } sw.WriteLine (); sw.WriteLine ("{"); @@ -811,7 +810,7 @@ static string GetJavaAccess (MethodAttributes access) static string GetJavaTypeName (TypeReference r, IMetadataResolver cache) { - TypeDefinition d = r.Resolve (); + TypeDefinition d = cache.Resolve (r); string? jniName = JavaNativeTypeManager.ToJniName (d, cache); if (jniName == null) { Diagnostic.Error (4201, Localization.Resources.JavaCallableWrappers_XA4201, r.FullName); @@ -827,12 +826,12 @@ bool CannotRegisterInStaticConstructor (TypeDefinition type) class Signature { - public Signature (MethodDefinition method, RegisterAttribute register, bool shouldBeDynamicallyRegistered = true) : this (method, register, null, null, shouldBeDynamicallyRegistered) {} + public Signature (MethodDefinition method, RegisterAttribute register, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true) : this (method, register, null, null, cache, shouldBeDynamicallyRegistered) {} - public Signature (MethodDefinition method, RegisterAttribute register, string? managedParameters, string? outerType, bool shouldBeDynamicallyRegistered = true) + public Signature (MethodDefinition method, RegisterAttribute register, string? managedParameters, string? outerType, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true) : this (register.Name, register.Signature, register.Connector, managedParameters, outerType, null) { - Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes); + Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache); IsDynamicallyRegistered = shouldBeDynamicallyRegistered; } @@ -844,10 +843,10 @@ public Signature (MethodDefinition method, ExportAttribute export, IMetadataReso JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask); ThrownTypeNames = export.ThrownNames; JavaNameOverride = export.Name; - Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes); + Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache); } - public Signature (MethodDefinition method, ExportFieldAttribute exportField, IMetadataResolver? cache) + public Signature (MethodDefinition method, ExportFieldAttribute exportField, IMetadataResolver cache) : this (method.Name, GetJniSignature (method, cache), "__export__", null, null, null) { if (method.HasParameters) diff --git a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaTypeScanner.cs b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaTypeScanner.cs index 77cab0cb4..97f2222ea 100644 --- a/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaTypeScanner.cs +++ b/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaTypeScanner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -76,14 +76,13 @@ void AddJavaTypes (List javaTypes, TypeDefinition type) AddJavaTypes (javaTypes, nested); } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool ShouldSkipJavaCallableWrapperGeneration (TypeDefinition type) => - ShouldSkipJavaCallableWrapperGeneration (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool ShouldSkipJavaCallableWrapperGeneration (TypeDefinition type) => throw new NotSupportedException (); - public static bool ShouldSkipJavaCallableWrapperGeneration (TypeDefinition type, TypeDefinitionCache? cache) => - ShouldSkipJavaCallableWrapperGeneration (type, (IMetadataResolver?) cache); + public static bool ShouldSkipJavaCallableWrapperGeneration (TypeDefinition type, TypeDefinitionCache cache) => + ShouldSkipJavaCallableWrapperGeneration (type, (IMetadataResolver) cache); - public static bool ShouldSkipJavaCallableWrapperGeneration (TypeDefinition type, IMetadataResolver? resolver) + public static bool ShouldSkipJavaCallableWrapperGeneration (TypeDefinition type, IMetadataResolver resolver) { if (JavaNativeTypeManager.IsNonStaticInnerClass (type, resolver)) return true; diff --git a/src/Java.Interop.Tools.JavaTypeSystem/Adapters/ManagedApiImporter.cs b/src/Java.Interop.Tools.JavaTypeSystem/Adapters/ManagedApiImporter.cs index e48e5dc53..aa7dfc541 100644 --- a/src/Java.Interop.Tools.JavaTypeSystem/Adapters/ManagedApiImporter.cs +++ b/src/Java.Interop.Tools.JavaTypeSystem/Adapters/ManagedApiImporter.cs @@ -10,13 +10,16 @@ namespace Java.Interop.Tools.JavaTypeSystem { public static class ManagedApiImporter { - public static JavaTypeCollection Parse (AssemblyDefinition assembly, JavaTypeCollection collection) + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static JavaTypeCollection Parse (AssemblyDefinition assembly, JavaTypeCollection collection) => throw new NotSupportedException (); + + public static JavaTypeCollection Parse (AssemblyDefinition assembly, JavaTypeCollection collection, TypeDefinitionCache resolver) { var types_to_add = new List (); foreach (var md in assembly.Modules) foreach (var td in md.Types) { - if (!ShouldSkipType (td) && ParseType (td, collection) is JavaTypeModel type) + if (!ShouldSkipType (td, resolver) && ParseType (td, collection) is JavaTypeModel type) types_to_add.Add (type); } @@ -219,7 +222,7 @@ static void AddReferenceTypeRecursive (JavaTypeModel type, JavaTypeCollection co AddReferenceTypeRecursive (nested, collection); } - static bool ShouldSkipType (TypeDefinition type) + static bool ShouldSkipType (TypeDefinition type, TypeDefinitionCache cache) { // We want to use Java's collection types instead of our managed adapter. // eg: 'Java.Util.ArrayList' over 'Android.Runtime.JavaList' @@ -240,13 +243,13 @@ static bool ShouldSkipType (TypeDefinition type) ? type.Module.GetType (type.FullName.Substring (0, type.FullName.IndexOf ('`'))) : null; - if (ShouldSkipGeneric (type, non_generic_type, null)) + if (ShouldSkipGeneric (type, non_generic_type, cache)) return true; return false; } - static bool ShouldSkipGeneric (TypeDefinition? a, TypeDefinition? b, TypeDefinitionCache? cache) + static bool ShouldSkipGeneric (TypeDefinition? a, TypeDefinition? b, TypeDefinitionCache cache) { if (a == null || b == null) return false; diff --git a/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs b/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs index 24370f719..a8bba4531 100644 --- a/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs +++ b/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs @@ -414,39 +414,36 @@ internal static ExportParameterAttribute ToExportParameterAttribute (CustomAttri return new ExportParameterAttribute ((ExportParameterKind)attr.ConstructorArguments [0].Value); } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool IsApplication (TypeDefinition type) => - IsApplication (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool IsApplication (TypeDefinition type) => throw new NotSupportedException (); - public static bool IsApplication (TypeDefinition type, TypeDefinitionCache? cache) => - IsApplication (type, (IMetadataResolver?) cache); + public static bool IsApplication (TypeDefinition type, TypeDefinitionCache cache) => + IsApplication (type, (IMetadataResolver) cache); - public static bool IsApplication (TypeDefinition type, IMetadataResolver? resolver) + public static bool IsApplication (TypeDefinition type, IMetadataResolver resolver) { return type.GetBaseTypes (resolver).Any (b => b.FullName == "Android.App.Application"); } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static bool IsInstrumentation (TypeDefinition type) => - IsInstrumentation (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static bool IsInstrumentation (TypeDefinition type) => throw new NotSupportedException (); - public static bool IsInstrumentation (TypeDefinition type, TypeDefinitionCache? cache) => - IsInstrumentation (type, (IMetadataResolver?) cache); + public static bool IsInstrumentation (TypeDefinition type, TypeDefinitionCache cache) => + IsInstrumentation (type, (IMetadataResolver) cache); - public static bool IsInstrumentation (TypeDefinition type, IMetadataResolver? resolver) + public static bool IsInstrumentation (TypeDefinition type, IMetadataResolver resolver) { return type.GetBaseTypes (resolver).Any (b => b.FullName == "Android.App.Instrumentation"); } // moved from JavaTypeInfo - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string? GetJniSignature (MethodDefinition method) => - GetJniSignature (method, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string? GetJniSignature (MethodDefinition method) => throw new NotSupportedException (); - public static string? GetJniSignature (MethodDefinition method, TypeDefinitionCache? cache) => - GetJniSignature (method, (IMetadataResolver?) cache); + public static string? GetJniSignature (MethodDefinition method, TypeDefinitionCache cache) => + GetJniSignature (method, (IMetadataResolver) cache); - public static string? GetJniSignature (MethodDefinition method, IMetadataResolver? resolver) + public static string? GetJniSignature (MethodDefinition method, IMetadataResolver resolver) { return GetJniSignature ( method.Parameters, @@ -459,35 +456,33 @@ public static bool IsInstrumentation (TypeDefinition type, IMetadataResolver? re } // moved from JavaTypeInfo - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string? GetJniTypeName (TypeReference typeRef) => - GetJniTypeName (typeRef, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string? GetJniTypeName (TypeReference typeRef) => throw new NotSupportedException (); - public static string? GetJniTypeName (TypeReference typeRef, TypeDefinitionCache? cache) => - GetJniTypeName (typeRef, (IMetadataResolver?) cache); + public static string? GetJniTypeName (TypeReference typeRef, TypeDefinitionCache cache) => + GetJniTypeName (typeRef, (IMetadataResolver) cache); - public static string? GetJniTypeName (TypeReference typeRef, IMetadataResolver? resolver) + public static string? GetJniTypeName (TypeReference typeRef, IMetadataResolver resolver) { return GetJniTypeName (typeRef, ExportParameterKind.Unspecified, resolver); } - internal static string? GetJniTypeName (TypeReference typeRef, ExportParameterKind exportKind, IMetadataResolver? cache) + internal static string? GetJniTypeName (TypeReference typeRef, ExportParameterKind exportKind, IMetadataResolver cache) { - return GetJniTypeName (typeRef, exportKind, t => t.Resolve (), t => { + return GetJniTypeName (typeRef, exportKind, t => cache.Resolve (t), t => { TypeReference etype; int rank = GetArrayInfo (typeRef, out etype); return new KeyValuePair (rank,etype); }, t => t.FullName, (t, k) => ToJniName (t, k, cache)); } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string ToCompatJniName (TypeDefinition type) => - ToCompatJniName (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string ToCompatJniName (TypeDefinition type) => throw new NotSupportedException (); - public static string ToCompatJniName (TypeDefinition type, TypeDefinitionCache? cache) => - ToCompatJniName (type, (IMetadataResolver?) cache); + public static string ToCompatJniName (TypeDefinition type, TypeDefinitionCache cache) => + ToCompatJniName (type, (IMetadataResolver) cache); - public static string ToCompatJniName (TypeDefinition type, IMetadataResolver? resolver) + public static string ToCompatJniName (TypeDefinition type, IMetadataResolver resolver) { return ToJniName ( type: type, @@ -505,27 +500,26 @@ static string ToCompatPackageName (TypeDefinition type) } // Keep in sync with ToJniNameFromAttributes(Type) and ToJniName(Type) - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string ToJniName (TypeDefinition type) => - ToJniName (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string ToJniName (TypeDefinition type) => throw new NotSupportedException (); - public static string ToJniName (TypeDefinition type, TypeDefinitionCache? cache) => - ToJniName (type, (IMetadataResolver?) cache); + public static string ToJniName (TypeDefinition type, TypeDefinitionCache cache) => + ToJniName (type, (IMetadataResolver) cache); - public static string ToJniName (TypeDefinition type, IMetadataResolver? resolver) + public static string ToJniName (TypeDefinition type, IMetadataResolver resolver) { var x = ToJniName (type, ExportParameterKind.Unspecified, resolver) ?? "java/lang/Object"; return x; } - static string? ToJniName (TypeDefinition type, ExportParameterKind exportKind, IMetadataResolver? cache) + static string? ToJniName (TypeDefinition type, ExportParameterKind exportKind, IMetadataResolver cache) { if (type == null) throw new ArgumentNullException ("type"); if (type.IsValueType) - return GetPrimitiveClass (type); + return GetPrimitiveClass (type, cache); if (type.FullName == "System.String") return "java/lang/String"; @@ -545,16 +539,16 @@ public static string ToJniName (TypeDefinition type, IMetadataResolver? resolver ); } - static string? ToJniNameFromAttributes (TypeDefinition type, IMetadataResolver? resolver) + static string? ToJniNameFromAttributes (TypeDefinition type, IMetadataResolver resolver) { return ToJniNameFromAttributesForAndroid (type, resolver) ?? ToJniNameFromAttributesForInterop (type, resolver); } - static string? ToJniNameFromAttributesForInterop (TypeDefinition type, IMetadataResolver? resolver) + static string? ToJniNameFromAttributesForInterop (TypeDefinition type, IMetadataResolver resolver) { var attr = type.CustomAttributes.FirstOrDefault (a => - Resolve (resolver, a.AttributeType) + resolver.Resolve (a.AttributeType) .FullName == "Java.Interop.JniTypeSignatureAttribute"); if (attr == null) { return null; @@ -565,7 +559,7 @@ public static string ToJniName (TypeDefinition type, IMetadataResolver? resolver return (string) carg.Value; } - static string? ToJniNameFromAttributesForAndroid (TypeDefinition type, IMetadataResolver? resolver) + static string? ToJniNameFromAttributesForAndroid (TypeDefinition type, IMetadataResolver resolver) { if (!type.HasCustomAttributes) return null; @@ -597,24 +591,19 @@ public static string ToJniName (TypeDefinition type, IMetadataResolver? resolver "Android.Runtime.RegisterAttribute", }; - static bool IsIJniNameProviderAttribute (CustomAttribute attr, IMetadataResolver? resolver) + static bool IsIJniNameProviderAttribute (CustomAttribute attr, IMetadataResolver resolver) { // Fast path for a list of known IJniNameProviderAttribute implementations if (KnownIJniNameProviders.Contains (attr.AttributeType.FullName)) return true; // Slow path resolves the type, looking for IJniNameProviderAttribute - var attributeType = Resolve (resolver, attr.AttributeType); + var attributeType = resolver.Resolve (attr.AttributeType); if (!attributeType.HasInterfaces) return false; return attributeType.Interfaces.Any (it => it.InterfaceType.FullName == typeof (IJniNameProviderAttribute).FullName); } - static TypeDefinition Resolve (IMetadataResolver? resolver, TypeReference typeReference) => - resolver?.Resolve (typeReference) ?? - typeReference.Resolve () ?? - throw new InvalidOperationException (); - public static int GetArrayInfo (Mono.Cecil.TypeReference type, out Mono.Cecil.TypeReference elementType) { elementType = type; @@ -626,10 +615,10 @@ public static int GetArrayInfo (Mono.Cecil.TypeReference type, out Mono.Cecil.Ty return rank; } - static string? GetPrimitiveClass (Mono.Cecil.TypeDefinition type) + static string? GetPrimitiveClass (Mono.Cecil.TypeDefinition type, IMetadataResolver cache) { if (type.IsEnum) - return GetPrimitiveClass (type.Fields.First (f => f.IsSpecialName).FieldType.Resolve ()); + return GetPrimitiveClass (cache.Resolve (type.Fields.First (f => f.IsSpecialName).FieldType), cache); if (type.FullName == "System.Byte") return "B"; if (type.FullName == "System.Char") @@ -649,14 +638,13 @@ public static int GetArrayInfo (Mono.Cecil.TypeReference type, out Mono.Cecil.Ty return null; } - [Obsolete ("Use the TypeDefinitionCache overload for better performance.")] - public static string GetPackageName (TypeDefinition type) => - GetPackageName (type, resolver: null); + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static string GetPackageName (TypeDefinition type) => throw new NotSupportedException (); - public static string GetPackageName (TypeDefinition type, TypeDefinitionCache? cache) => - GetPackageName (type, (IMetadataResolver?) cache); + public static string GetPackageName (TypeDefinition type, TypeDefinitionCache cache) => + GetPackageName (type, (IMetadataResolver) cache); - public static string GetPackageName (TypeDefinition type, IMetadataResolver? resolver) + public static string GetPackageName (TypeDefinition type, IMetadataResolver resolver) { if (IsPackageNamePreservedForAssembly (type.GetPartialAssemblyName (resolver))) return type.Namespace.ToLowerInvariant (); @@ -712,7 +700,7 @@ static string ToJniName (T type, Func decl, Func name, Func< } #if HAVE_CECIL - internal static bool IsNonStaticInnerClass (TypeDefinition? type, IMetadataResolver? cache) + internal static bool IsNonStaticInnerClass (TypeDefinition? type, IMetadataResolver cache) { if (type == null) return false; @@ -726,7 +714,7 @@ internal static bool IsNonStaticInnerClass (TypeDefinition? type, IMetadataResol .Any (ctor => ctor.Parameters.Any (p => p.Name == "__self")); } - static IEnumerable GetBaseConstructors (TypeDefinition type, IMetadataResolver? cache) + static IEnumerable GetBaseConstructors (TypeDefinition type, IMetadataResolver cache) { var baseType = type.GetBaseTypes (cache).FirstOrDefault (t => t.GetCustomAttributes (typeof (RegisterAttribute)).Any ()); if (baseType != null) diff --git a/tools/generator/CodeGenerator.cs b/tools/generator/CodeGenerator.cs index 753f05138..150e93382 100644 --- a/tools/generator/CodeGenerator.cs +++ b/tools/generator/CodeGenerator.cs @@ -110,7 +110,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve // Resolve types using Java.Interop.Tools.JavaTypeSystem if (is_classparse && !options.UseLegacyJavaResolver) { var output_xml = api_xml_adjuster_output ?? Path.Combine (Path.GetDirectoryName (filename), Path.GetFileName (filename) + ".adjusted"); - JavaTypeResolutionFixups.Fixup (filename, output_xml, resolver, references.Distinct ().ToArray ()); + JavaTypeResolutionFixups.Fixup (filename, output_xml, resolver, references.Distinct ().ToArray (), resolverCache); if (only_xml_adjuster) return; diff --git a/tools/generator/Java.Interop.Tools.Generator.Transformation/JavaTypeResolutionFixups.cs b/tools/generator/Java.Interop.Tools.Generator.Transformation/JavaTypeResolutionFixups.cs index 648475fce..71b0c2b0b 100644 --- a/tools/generator/Java.Interop.Tools.Generator.Transformation/JavaTypeResolutionFixups.cs +++ b/tools/generator/Java.Interop.Tools.Generator.Transformation/JavaTypeResolutionFixups.cs @@ -9,9 +9,12 @@ namespace generator { public static class JavaTypeResolutionFixups { + [Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)] + public static void Fixup (string xmlFile, string outputXmlFile, DirectoryAssemblyResolver resolver, string [] references) => throw new NotSupportedException (); + // This fixup ensures all referenced Java types can be resolved, and // removes types and members that rely on unresolvable Java types. - public static void Fixup (string xmlFile, string outputXmlFile, DirectoryAssemblyResolver resolver, string [] references) + public static void Fixup (string xmlFile, string outputXmlFile, DirectoryAssemblyResolver resolver, string [] references, TypeDefinitionCache cache) { // Parse api.xml var type_collection = JavaXmlApiImporter.Parse (xmlFile); @@ -21,7 +24,7 @@ public static void Fixup (string xmlFile, string outputXmlFile, DirectoryAssembl Report.Verbose (0, "Resolving assembly for Java type resolution: '{0}'.", reference); var assembly = resolver.Load (reference); - ManagedApiImporter.Parse (assembly, type_collection); + ManagedApiImporter.Parse (assembly, type_collection, cache); } // Run the type resolution pass