Skip to content

Commit a2ac436

Browse files
[TrimmableTypeMap][Core A] Use required init properties on records
Add netstandard2.0 polyfills for IsExternalInit, RequiredMemberAttribute, CompilerFeatureRequiredAttribute, and SetsRequiredMembersAttribute. Convert all record properties to init-only, with 'required' on non-nullable string and essential properties. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e2f1c06 commit a2ac436

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Polyfills for C# language features on netstandard2.0
2+
3+
// Required for init-only setters
4+
namespace System.Runtime.CompilerServices
5+
{
6+
static class IsExternalInit { }
7+
8+
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
9+
sealed class RequiredMemberAttribute : Attribute { }
10+
11+
[AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited = false)]
12+
sealed class CompilerFeatureRequiredAttribute : Attribute
13+
{
14+
public CompilerFeatureRequiredAttribute (string featureName)
15+
{
16+
FeatureName = featureName;
17+
}
18+
19+
public string FeatureName { get; }
20+
public bool IsOptional { get; init; }
21+
}
22+
}
23+
24+
namespace System.Diagnostics.CodeAnalysis
25+
{
26+
[AttributeUsage (AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
27+
sealed class SetsRequiredMembersAttribute : Attribute { }
28+
}

src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerInfo.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,79 @@ sealed record JavaPeerInfo
1414
/// JNI type name, e.g., "android/app/Activity".
1515
/// Extracted from the [Register] attribute.
1616
/// </summary>
17-
public string JavaName { get; set; } = "";
17+
public required string JavaName { get; init; }
1818

1919
/// <summary>
2020
/// Compat JNI type name, e.g., "myapp.namespace/MyType" for user types (uses raw namespace, not CRC64).
2121
/// For MCW binding types (with [Register]), this equals <see cref="JavaName"/>.
2222
/// Used by acw-map.txt to support legacy custom view name resolution in layout XMLs.
2323
/// </summary>
24-
public string CompatJniName { get; set; } = "";
24+
public required string CompatJniName { get; init; }
2525

2626
/// <summary>
2727
/// Full managed type name, e.g., "Android.App.Activity".
2828
/// </summary>
29-
public string ManagedTypeName { get; set; } = "";
29+
public required string ManagedTypeName { get; init; }
3030

3131
/// <summary>
3232
/// Assembly name the type belongs to, e.g., "Mono.Android".
3333
/// </summary>
34-
public string AssemblyName { get; set; } = "";
34+
public required string AssemblyName { get; init; }
3535

3636
/// <summary>
3737
/// JNI name of the base Java type, e.g., "android/app/Activity" for a type
3838
/// that extends Activity. Null for java/lang/Object or types without a Java base.
3939
/// Needed by JCW Java source generation ("extends" clause).
4040
/// </summary>
41-
public string? BaseJavaName { get; set; }
41+
public string? BaseJavaName { get; init; }
4242

4343
/// <summary>
4444
/// JNI names of Java interfaces this type implements, e.g., ["android/view/View$OnClickListener"].
4545
/// Needed by JCW Java source generation ("implements" clause).
4646
/// </summary>
47-
public IReadOnlyList<string> ImplementedInterfaceJavaNames { get; set; } = Array.Empty<string> ();
47+
public IReadOnlyList<string> ImplementedInterfaceJavaNames { get; init; } = Array.Empty<string> ();
4848

49-
public bool IsInterface { get; set; }
50-
public bool IsAbstract { get; set; }
49+
public bool IsInterface { get; init; }
50+
public bool IsAbstract { get; init; }
5151

5252
/// <summary>
5353
/// If true, this is a Managed Callable Wrapper (MCW) binding type.
5454
/// No JCW or RegisterNatives will be generated for it.
5555
/// </summary>
56-
public bool DoNotGenerateAcw { get; set; }
56+
public bool DoNotGenerateAcw { get; init; }
5757

5858
/// <summary>
5959
/// Types with component attributes ([Activity], [Service], etc.),
6060
/// custom views from layout XML, or manifest-declared components
6161
/// are unconditionally preserved (not trimmable).
6262
/// </summary>
63-
public bool IsUnconditional { get; set; }
63+
public bool IsUnconditional { get; init; }
6464

6565
/// <summary>
6666
/// Marshal methods: methods with [Register(name, sig, connector)], [Export], or
6767
/// constructor registrations ([Register(".ctor", sig, "")] / [JniConstructorSignature]).
6868
/// Constructors are identified by <see cref="MarshalMethodInfo.IsConstructor"/>.
6969
/// Ordered — the index in this list is the method's ordinal for RegisterNatives.
7070
/// </summary>
71-
public IReadOnlyList<MarshalMethodInfo> MarshalMethods { get; set; } = Array.Empty<MarshalMethodInfo> ();
71+
public IReadOnlyList<MarshalMethodInfo> MarshalMethods { get; init; } = Array.Empty<MarshalMethodInfo> ();
7272

7373
/// <summary>
7474
/// Information about the activation constructor for this type.
7575
/// May reference a base type's constructor if the type doesn't define its own.
7676
/// </summary>
77-
public ActivationCtorInfo? ActivationCtor { get; set; }
77+
public ActivationCtorInfo? ActivationCtor { get; init; }
7878

7979
/// <summary>
8080
/// For interfaces and abstract types, the name of the invoker type
8181
/// used to instantiate instances from Java.
8282
/// </summary>
83-
public string? InvokerTypeName { get; set; }
83+
public string? InvokerTypeName { get; init; }
8484

8585
/// <summary>
8686
/// True if this is an open generic type definition.
8787
/// Generic types get TypeMap entries but CreateInstance throws NotSupportedException.
8888
/// </summary>
89-
public bool IsGenericDefinition { get; set; }
89+
public bool IsGenericDefinition { get; init; }
9090
}
9191

9292
/// <summary>
@@ -100,41 +100,41 @@ sealed record MarshalMethodInfo
100100
/// JNI method name, e.g., "onCreate".
101101
/// This is the Java method name (without n_ prefix).
102102
/// </summary>
103-
public string JniName { get; set; } = "";
103+
public required string JniName { get; init; }
104104

105105
/// <summary>
106106
/// JNI method signature, e.g., "(Landroid/os/Bundle;)V".
107107
/// Contains both parameter types and return type.
108108
/// </summary>
109-
public string JniSignature { get; set; } = "";
109+
public required string JniSignature { get; init; }
110110

111111
/// <summary>
112112
/// The connector string from [Register], e.g., "GetOnCreate_Landroid_os_Bundle_Handler".
113113
/// Null for [Export] methods.
114114
/// </summary>
115-
public string? Connector { get; set; }
115+
public string? Connector { get; init; }
116116

117117
/// <summary>
118118
/// Name of the managed method this maps to, e.g., "OnCreate".
119119
/// </summary>
120-
public string ManagedMethodName { get; set; } = "";
120+
public required string ManagedMethodName { get; init; }
121121

122122
/// <summary>
123123
/// True if this is a constructor registration.
124124
/// </summary>
125-
public bool IsConstructor { get; set; }
125+
public bool IsConstructor { get; init; }
126126

127127
/// <summary>
128128
/// For [Export] methods: Java exception types that the method declares it can throw.
129129
/// Null for [Register] methods.
130130
/// </summary>
131-
public IReadOnlyList<string>? ThrownNames { get; set; }
131+
public IReadOnlyList<string>? ThrownNames { get; init; }
132132

133133
/// <summary>
134134
/// For [Export] methods: super constructor arguments string.
135135
/// Null for [Register] methods.
136136
/// </summary>
137-
public string? SuperArgumentsString { get; set; }
137+
public string? SuperArgumentsString { get; init; }
138138
}
139139

140140
/// <summary>
@@ -146,17 +146,17 @@ sealed record ActivationCtorInfo
146146
/// The type that declares the activation constructor.
147147
/// May be the type itself or a base type.
148148
/// </summary>
149-
public string DeclaringTypeName { get; set; } = "";
149+
public required string DeclaringTypeName { get; init; }
150150

151151
/// <summary>
152152
/// The assembly containing the declaring type.
153153
/// </summary>
154-
public string DeclaringAssemblyName { get; set; } = "";
154+
public required string DeclaringAssemblyName { get; init; }
155155

156156
/// <summary>
157157
/// The style of activation constructor found.
158158
/// </summary>
159-
public ActivationCtorStyle Style { get; set; }
159+
public required ActivationCtorStyle Style { get; init; }
160160
}
161161

162162
enum ActivationCtorStyle

0 commit comments

Comments
 (0)