Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java.Interop.Tools.JavaCallableWrappers] use less System.Linq for CAs #1072

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +11 to +12
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely sold on the name AnyCustomAttributes(), let me know if anyone has other ideas.


public static IEnumerable<CustomAttribute> 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<CustomAttribute> GetCustomAttributes (this ICustomAttributeProvider item, string attribute_fullname)
{
foreach (CustomAttribute custom_attribute in item.CustomAttributes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -205,8 +205,8 @@ void AddNestedTypes (TypeDefinition type)

var curCtors = new List<MethodDefinition> ();

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");
}
Expand Down Expand Up @@ -278,8 +278,8 @@ static void ExtractJavaNames (string jniName, out string package, out string typ

void AddConstructors (TypeDefinition type, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> 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);
}

Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodDefinition> 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<MethodDefinition> ();
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

Expand Down