diff --git a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/CustomAttributeProviderRocks.cs b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/CustomAttributeProviderRocks.cs index 0eaa830ed..11b498593 100644 --- a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/CustomAttributeProviderRocks.cs +++ b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/CustomAttributeProviderRocks.cs @@ -8,11 +8,23 @@ namespace Java.Interop.Tools.Cecil { public static class CustomAttributeProviderRocks { + public static bool AnyCustomAttributes (this ICustomAttributeProvider item, Type attribute) => + item.AnyCustomAttributes (attribute.FullName); + public static IEnumerable GetCustomAttributes (this ICustomAttributeProvider item, Type attribute) { return item.GetCustomAttributes (attribute.FullName); } + public static bool AnyCustomAttributes (this ICustomAttributeProvider item, string attribute_fullname) + { + foreach (CustomAttribute custom_attribute in item.CustomAttributes) { + if (custom_attribute.Constructor.DeclaringType.FullName == attribute_fullname) + return true; + } + return false; + } + public static IEnumerable GetCustomAttributes (this ICustomAttributeProvider item, string attribute_fullname) { foreach (CustomAttribute custom_attribute in item.CustomAttributes) { 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 cd489d00d..76f2bfc04 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 @@ -164,10 +164,10 @@ void AddNestedTypes (TypeDefinition type) var baseRegisteredMethod = GetBaseRegisteredMethod (minfo); if (baseRegisteredMethod != null) AddMethod (baseRegisteredMethod, minfo); - else if (GetExportFieldAttributes (minfo).Any ()) { + else if (minfo.AnyCustomAttributes (typeof(ExportFieldAttribute))) { AddMethod (null, minfo); HasExport = true; - } else if (GetExportAttributes (minfo).Any ()) { + } else if (minfo.AnyCustomAttributes (typeof (ExportAttribute))) { AddMethod (null, minfo); HasExport = true; } @@ -205,8 +205,8 @@ void AddNestedTypes (TypeDefinition type) var curCtors = new List (); - foreach (MethodDefinition minfo in type.Methods.Where (m => m.IsConstructor)) { - if (GetExportAttributes (minfo).Any ()) { + foreach (MethodDefinition minfo in type.Methods) { + if (minfo.IsConstructor && minfo.AnyCustomAttributes (typeof (ExportAttribute))) { if (minfo.IsStatic) { // Diagnostic.Warning (log, "ExportAttribute does not work on static constructor"); } @@ -278,8 +278,8 @@ static void ExtractJavaNames (string jniName, out string package, out string typ void AddConstructors (TypeDefinition type, string? outerType, List? baseCtors, List curCtors, bool onlyRegisteredOrExportedCtors) { - foreach (MethodDefinition ctor in type.Methods.Where (m => m.IsConstructor && !m.IsStatic)) - if (!GetExportAttributes (ctor).Any ()) + foreach (MethodDefinition ctor in type.Methods) + if (ctor.IsConstructor && !ctor.IsStatic && !ctor.AnyCustomAttributes (typeof (ExportAttribute))) AddConstructor (ctor, type, outerType, baseCtors, curCtors, onlyRegisteredOrExportedCtors, false); } @@ -342,8 +342,7 @@ void AddConstructor (MethodDefinition ctor, TypeDefinition type, string? outerTy while ((bmethod = method.GetBaseDefinition (cache)) != method) { method = bmethod; - var attributes = method.GetCustomAttributes (typeof (RegisterAttribute)); - if (attributes.Any ()) { + if (method.AnyCustomAttributes (typeof (RegisterAttribute))) { return method; } } 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 a8bba4531..176e478a4 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 @@ -710,16 +710,24 @@ internal static bool IsNonStaticInnerClass (TypeDefinition? type, IMetadataResol if (!type.DeclaringType.IsSubclassOf ("Java.Lang.Object", cache)) return false; - return GetBaseConstructors (type, cache) - .Any (ctor => ctor.Parameters.Any (p => p.Name == "__self")); - } + foreach (var baseType in type.GetBaseTypes (cache)) { + if (baseType == null) + continue; + if (!baseType.AnyCustomAttributes (typeof (RegisterAttribute))) + continue; - static IEnumerable GetBaseConstructors (TypeDefinition type, IMetadataResolver cache) - { - var baseType = type.GetBaseTypes (cache).FirstOrDefault (t => t.GetCustomAttributes (typeof (RegisterAttribute)).Any ()); - if (baseType != null) - return baseType.Methods.Where (m => m.IsConstructor && !m.IsStatic); - return Enumerable.Empty (); + foreach (var method in baseType.Methods) { + if (!method.IsConstructor || method.IsStatic) + continue; + if (method.Parameters.Any (p => p.Name == "__self")) + return true; + } + + // Stop at the first base type with [Register] + break; + } + + return false; } #endif // HAVE_CECIL