-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathJavaPeerInfo.cs
More file actions
428 lines (364 loc) · 15.2 KB
/
Copy pathJavaPeerInfo.cs
File metadata and controls
428 lines (364 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections.Generic;
namespace Microsoft.Android.Sdk.TrimmableTypeMap;
/// <summary>
/// Represents a Java peer type discovered during assembly scanning.
/// Contains all data needed by downstream generators (TypeMap IL, UCO wrappers, JCW Java sources).
/// Generators consume this data model — they never touch PEReader/MetadataReader.
/// </summary>
public sealed record JavaPeerInfo
{
/// <summary>
/// JNI type name, e.g., "android/app/Activity".
/// Extracted from the [Register] attribute.
/// </summary>
public required string JavaName { get; init; }
/// <summary>
/// Compat JNI type name, e.g., "myapp.namespace/MyType" for user types (uses raw namespace, not CRC64).
/// For MCW binding types (with [Register]), this equals <see cref="JavaName"/>.
/// Used by acw-map.txt to support legacy custom view name resolution in layout XMLs.
/// </summary>
public required string CompatJniName { get; init; }
/// <summary>
/// Full managed type name, e.g., "Android.App.Activity".
/// </summary>
public required string ManagedTypeName { get; init; }
/// <summary>
/// Managed type namespace, e.g., "Android.App".
/// </summary>
public required string ManagedTypeNamespace { get; init; }
/// <summary>
/// Managed type short name (without namespace), e.g., "Activity".
/// </summary>
public required string ManagedTypeShortName { get; init; }
/// <summary>
/// Assembly name the type belongs to, e.g., "Mono.Android".
/// </summary>
public required string AssemblyName { get; init; }
/// <summary>
/// True when the type belongs to a framework assembly supplied by the Android SDK.
/// Framework ACWs are generated by the SDK and can be trimmed like bindings unless
/// another rule explicitly roots them.
/// </summary>
public bool IsFrameworkAssembly { get; set; }
/// <summary>
/// True when per-rank array typemap entries should be generated for this peer.
/// </summary>
public bool GenerateArrayEntries { get; set; } = true;
/// <summary>
/// JNI name of the base Java type, e.g., "android/app/Activity" for a type
/// that extends Activity. Null for java/lang/Object or types without a Java base.
/// Needed by JCW Java source generation ("extends" clause).
/// </summary>
public string? BaseJavaName { get; init; }
/// <summary>
/// JNI names of Java interfaces this type implements, e.g., ["android/view/View$OnClickListener"].
/// Needed by JCW Java source generation ("implements" clause).
/// </summary>
public IReadOnlyList<string> ImplementedInterfaceJavaNames { get; init; } = Array.Empty<string> ();
public bool IsInterface { get; init; }
public bool IsAbstract { get; init; }
/// <summary>
/// If true, this is a Managed Callable Wrapper (MCW) binding type.
/// No JCW or RegisterNatives will be generated for it.
/// </summary>
public bool DoNotGenerateAcw { get; init; }
/// <summary>
/// True when the type was discovered via <c>[JniTypeSignatureAttribute]</c>
/// rather than <c>[RegisterAttribute]</c>. Used to resolve cross-assembly
/// alias ownership: <c>[Register]</c> types take precedence.
/// </summary>
public bool IsFromJniTypeSignature { get; init; }
/// <summary>
/// Types with component attributes ([Activity], [Service], etc.),
/// custom views from layout XML, or manifest-declared components
/// are unconditionally preserved (not trimmable).
/// May be set to <c>true</c> after scanning when the manifest references a type
/// that the scanner did not mark as unconditional. Should only ever be set
/// to <c>true</c>, never back to <c>false</c>.
/// </summary>
public bool IsUnconditional { get; set; }
/// <summary>
/// True for Application and Instrumentation types. These types cannot call
/// <c>registerNatives</c> in their static initializer because the native library
/// (<c>libmonodroid.so</c>) is not loaded until after the Application class is instantiated.
/// Registration is deferred to <c>ApplicationRegistration.registerApplications()</c>.
/// This may also be set after scanning when a type is only discovered from
/// manifest <c>android:name</c> usage on <c><application></c> or
/// <c><instrumentation></c>.
/// </summary>
public bool CannotRegisterInStaticConstructor { get; set; }
/// <summary>
/// Marshal methods: methods with [Register(name, sig, connector)], [Export], or
/// constructor registrations ([Register(".ctor", sig, "")] / [JniConstructorSignature]).
/// Constructors are identified by <see cref="MarshalMethodInfo.IsConstructor"/>.
/// Ordered — the index in this list is the method's ordinal for RegisterNatives.
/// </summary>
public IReadOnlyList<MarshalMethodInfo> MarshalMethods { get; init; } = Array.Empty<MarshalMethodInfo> ();
/// <summary>
/// Java constructors to emit in the JCW .java file.
/// Each has a JNI signature and an ordinal index for the nctor_N native method.
/// </summary>
public IReadOnlyList<JavaConstructorInfo> JavaConstructors { get; init; } = [];
/// <summary>
/// Java fields from [ExportField] attributes.
/// Each field is initialized by calling the annotated method.
/// </summary>
public IReadOnlyList<JavaFieldInfo> JavaFields { get; init; } = [];
/// <summary>
/// Information about the activation constructor for this type.
/// May reference a base type's constructor if the type doesn't define its own.
/// </summary>
public ActivationCtorInfo? ActivationCtor { get; init; }
/// <summary>
/// For interfaces and abstract types, the name of the invoker type
/// used to instantiate instances from Java.
/// </summary>
public string? InvokerTypeName { get; init; }
/// <summary>
/// Activation constructor style declared by <see cref="InvokerTypeName"/>.
/// Kept separate from <see cref="ActivationCtor"/>, which describes the
/// target type or its base types.
/// </summary>
public ActivationCtorStyle? InvokerActivationCtorStyle { get; init; }
/// <summary>
/// True if this is an open generic type definition.
/// Generic types get TypeMap entries but CreateInstance throws NotSupportedException.
/// </summary>
public bool IsGenericDefinition { get; init; }
/// <summary>
/// Android component attribute data ([Activity], [Service], [BroadcastReceiver], [ContentProvider],
/// [Application], [Instrumentation]) if present on this type. Used for manifest generation.
/// </summary>
public ComponentInfo? ComponentAttribute { get; init; }
}
/// <summary>
/// Describes a marshal method (a method with [Register] or [Export]) on a Java peer type.
/// Contains all data needed to generate a UCO wrapper, a JCW native declaration,
/// and a RegisterNatives call.
/// </summary>
public sealed record MarshalMethodInfo
{
/// <summary>
/// JNI method name, e.g., "onCreate".
/// This is the Java method name (without n_ prefix).
/// </summary>
public required string JniName { get; init; }
/// <summary>
/// JNI method signature, e.g., "(Landroid/os/Bundle;)V".
/// Contains both parameter types and return type.
/// </summary>
public required string JniSignature { get; init; }
/// <summary>
/// The connector string from [Register], e.g., "GetOnCreate_Landroid_os_Bundle_Handler".
/// Null for [Export] methods.
/// </summary>
public string? Connector { get; init; }
/// <summary>
/// Name of the managed method this maps to, e.g., "OnCreate".
/// </summary>
public required string ManagedMethodName { get; init; }
/// <summary>
/// Full name of the type that declares the managed method (may be a base type).
/// Empty when the declaring type is the same as the peer type.
/// </summary>
public string DeclaringTypeName { get; init; } = "";
/// <summary>
/// Assembly name of the type that declares the managed method.
/// Needed for cross-assembly UCO wrapper generation.
/// Empty when the declaring type is the same as the peer type.
/// </summary>
public string DeclaringAssemblyName { get; init; } = "";
/// <summary>
/// Exact type that declares the managed method. This can be a constructed
/// generic base type, e.g. <c>BaseAdapter<object></c>.
/// </summary>
internal TypeRefData? DeclaringType { get; init; }
/// <summary>
/// The native callback method name, e.g., "n_onCreate".
/// This is the Java/JNI-visible native method name that the generated JCW calls.
/// </summary>
public required string NativeCallbackName { get; init; }
/// <summary>
/// CLR type-name strings (e.g. "System.Boolean", "System.SByte", "System.IntPtr") for the
/// real native <c>n_*</c> callback's JNI parameters, in order, excluding the leading
/// <c>jnienv</c>/<c>native__this</c> IntPtr pair. Captured from the actual <c>n_*</c> method's
/// metadata signature so the emitted callback MemberRef matches it exactly — importantly, JNI
/// boolean is <c>bool</c> or <c>sbyte</c> and JNI char is <c>char</c> or <c>ushort</c> depending
/// on which generator version compiled the binding (see java-interop #1296). Null when the
/// <c>n_*</c> method's signature could not be resolved.
/// </summary>
internal IReadOnlyList<string>? NativeCallbackParameterTypeNames { get; init; }
/// <summary>
/// CLR return type-name string for the real native <c>n_*</c> callback, captured from metadata.
/// Null when the <c>n_*</c> method's signature could not be resolved.
/// </summary>
internal string? NativeCallbackReturnTypeName { get; init; }
/// <summary>
/// True if this is a constructor registration.
/// </summary>
public bool IsConstructor { get; init; }
/// <summary>
/// True if this method comes from an [Export] attribute (rather than [Register]).
/// [Export] methods use the C# method's access modifier in the JCW Java file
/// instead of always being "public".
/// </summary>
public bool IsExport { get; init; }
/// <summary>
/// Java access modifier for [Export] methods ("public", "protected", "private").
/// Null for [Register] methods (always "public").
/// </summary>
public string? JavaAccess { get; init; }
/// <summary>
/// For [Export] methods: Java exception types that the method declares it can throw.
/// Null for [Register] methods.
/// </summary>
public IReadOnlyList<string>? ThrownNames { get; init; }
/// <summary>
/// For [Export] methods: super constructor arguments string.
/// Null for [Register] methods.
/// </summary>
public string? SuperArgumentsString { get; init; }
/// <summary>
/// Managed method parameter types, in declaration order.
/// </summary>
internal IReadOnlyList<TypeRefData> ManagedParameterTypes { get; init; } = [];
/// <summary>
/// Per-parameter [ExportParameter] kinds for legacy callback marshalling.
/// </summary>
internal IReadOnlyList<ExportParameterKindInfo> ManagedParameterExportKinds { get; init; } = [];
/// <summary>
/// Managed return type, including the defining assembly.
/// </summary>
internal TypeRefData ManagedReturnType { get; init; } = new () {
ManagedTypeName = "System.Void",
AssemblyName = "System.Runtime",
};
/// <summary>
/// [ExportParameter] kind applied to the return value, if any.
/// </summary>
internal ExportParameterKindInfo ManagedReturnExportKind { get; init; }
/// <summary>
/// Whether the managed target method is static.
/// </summary>
public bool IsStatic { get; init; }
/// <summary>
/// True if this method was collected from an implemented interface
/// (Pass 4: CollectInterfaceMethodImplementations), not from the type itself.
/// </summary>
public bool IsInterfaceImplementation { get; init; }
/// <summary>
/// True when the generated UCO wrapper should dispatch directly to the managed method
/// instead of forwarding to an existing static n_* callback. This is needed for
/// direct registered methods because an inherited callback can dispatch through a
/// different managed virtual slot when a derived type hides a base member with
/// <c>new virtual</c> while reusing the same JNI name and signature.
/// </summary>
public bool CallManagedMethodDirectly { get; init; }
}
/// <summary>
/// Describes a JNI parameter for UCO method generation.
/// </summary>
public sealed record JniParameterInfo
{
/// <summary>
/// JNI type descriptor, e.g., "Landroid/os/Bundle;", "I", "Z".
/// </summary>
public required string JniType { get; init; }
/// <summary>
/// Managed parameter type name, e.g., "Android.OS.Bundle", "System.Int32".
/// </summary>
public string ManagedType { get; init; } = "";
}
/// <summary>
/// Describes a Java constructor to emit in the JCW .java source file.
/// </summary>
public sealed record JavaConstructorInfo
{
/// <summary>
/// JNI constructor signature, e.g., "(Landroid/content/Context;)V".
/// </summary>
public required string JniSignature { get; init; }
/// <summary>
/// Ordinal index for the native constructor method (nctor_0, nctor_1, ...).
/// </summary>
public required int ConstructorIndex { get; init; }
/// <summary>
/// For [Export] constructors: super constructor arguments string.
/// Null for [Register] constructors.
/// </summary>
public string? SuperArgumentsString { get; init; }
/// <summary>
/// Managed constructor parameter types, in declaration order.
/// </summary>
internal IReadOnlyList<TypeRefData> ManagedParameterTypes { get; init; } = [];
/// <summary>
/// True when this Java constructor has a matching public managed constructor on the target type.
/// </summary>
public bool HasMatchingManagedCtor { get; init; }
}
/// <summary>
/// Describes a Java field from an [ExportField] attribute.
/// The field is initialized by calling the annotated method.
/// </summary>
public sealed record JavaFieldInfo
{
/// <summary>
/// Java field name, e.g., "STATIC_INSTANCE".
/// </summary>
public required string FieldName { get; init; }
/// <summary>
/// Java type name for the field, e.g., "java.lang.String".
/// </summary>
public required string JavaTypeName { get; init; }
/// <summary>
/// Name of the method that initializes this field, e.g., "GetInstance".
/// </summary>
public required string InitializerMethodName { get; init; }
/// <summary>
/// Java access modifier ("public", "protected", "private").
/// </summary>
public required string Visibility { get; init; }
/// <summary>
/// Whether the field is static.
/// </summary>
public bool IsStatic { get; init; }
}
/// <summary>
/// Describes how to call the activation constructor for a Java peer type.
/// </summary>
public sealed record ActivationCtorInfo
{
/// <summary>
/// The type that declares the activation constructor.
/// May be the type itself or a base type.
/// </summary>
public required string DeclaringTypeName { get; init; }
/// <summary>
/// The assembly containing the declaring type.
/// </summary>
public required string DeclaringAssemblyName { get; init; }
/// <summary>
/// Exact type that declares the activation constructor. This can be a
/// constructed generic base type.
/// </summary>
internal TypeRefData DeclaringType { get; init; } = new () {
ManagedTypeName = "",
AssemblyName = "",
};
/// <summary>
/// The style of activation constructor found.
/// </summary>
public required ActivationCtorStyle Style { get; init; }
}
public enum ActivationCtorStyle
{
/// <summary>
/// Xamarin.Android style: (IntPtr handle, JniHandleOwnership transfer)
/// </summary>
XamarinAndroid,
/// <summary>
/// Java.Interop style: (ref JniObjectReference reference, JniObjectReferenceOptions options)
/// </summary>
JavaInterop,
}