Skip to content

Commit a8f3e62

Browse files
Simplify TryGetJniNameForType to use IJniNameProviderAttribute
Both RegisterAttribute and JniTypeSignatureAttribute implement IJniNameProviderAttribute, so a single GetCustomAttributes check covers both. Made the method internal+static so TrimmableTypeMapTypeManager.GetSimpleReferences can reuse it instead of duplicating the attribute lookup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8eae12c commit a8f3e62

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMap.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,14 @@ internal bool TryGetType (string jniSimpleReference, [NotNullWhen (true)] out Ty
9696
}
9797

9898
/// <summary>
99-
/// Resolves a managed type's JNI name from its [Register] or [JniTypeSignature] attributes.
99+
/// Resolves a managed type's JNI name from its <see cref="IJniNameProviderAttribute"/>
100+
/// (implemented by both <c>[Register]</c> and <c>[JniTypeSignature]</c>).
100101
/// </summary>
101-
static bool TryGetJniNameForType (Type type, [NotNullWhen (true)] out string? jniName)
102+
internal static bool TryGetJniNameForType (Type type, [NotNullWhen (true)] out string? jniName)
102103
{
103-
// Check [Register("jniName", ...)] attribute (Mono.Android binding types)
104-
foreach (var attr in type.GetCustomAttributesData ()) {
105-
if (attr.AttributeType.FullName == "Android.Runtime.RegisterAttribute"
106-
&& attr.ConstructorArguments.Count > 0
107-
&& attr.ConstructorArguments[0].Value is string name
108-
&& name.Length > 0
109-
&& !name.Contains ('(')) { // Skip method-level [Register]
110-
jniName = name;
111-
return true;
112-
}
113-
}
114-
115-
// Check [JniTypeSignature("jniName")] attribute (Java.Interop types)
116-
var sigAttr = type.GetCustomAttribute<JniTypeSignatureAttribute> (inherit: false);
117-
if (sigAttr is not null && !string.IsNullOrEmpty (sigAttr.SimpleReference)) {
118-
jniName = sigAttr.SimpleReference;
104+
var provider = type.GetCustomAttribute<IJniNameProviderAttribute> (inherit: false);
105+
if (provider is not null && !string.IsNullOrEmpty (provider.Name)) {
106+
jniName = provider.Name.Replace ('.', '/');
119107
return true;
120108
}
121109

src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMapTypeManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)
3939
yield return r;
4040
}
4141

42-
var attr = type.GetCustomAttributes (typeof (IJniNameProviderAttribute), inherit: false);
43-
if (attr.Length > 0 && attr [0] is IJniNameProviderAttribute jniNameProvider && !string.IsNullOrEmpty (jniNameProvider.Name)) {
44-
yield return jniNameProvider.Name.Replace ('.', '/');
42+
if (TrimmableTypeMap.TryGetJniNameForType (type, out var jniName)) {
43+
yield return jniName;
4544
}
4645
}
4746

0 commit comments

Comments
 (0)