-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathClassGen.cs
388 lines (335 loc) · 14.1 KB
/
ClassGen.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Java.Interop.Tools.Generator;
using Java.Interop.Tools.TypeNameMappings;
using Xamarin.Android.Binder;
namespace MonoDroid.Generation
{
public class ClassGen : GenBase
{
bool fill_explicit_implementation_started;
HashSet<string> skipped_interface_methods;
public List<Ctor> Ctors { get; private set; } = new List<Ctor> ();
public ClassGen (GenBaseSupport support) : base (support)
{
InheritsObject = true;
if (Namespace == "Java.Lang" && (Name == "Object" || Name == "Throwable"))
InheritsObject = false;
DefaultValue = "IntPtr.Zero";
NativeType = "IntPtr";
}
static void AddNestedInterfaceTypes (GenBase type, List<InterfaceExtensionInfo> nestedInterfaces)
{
foreach (var nt in type.NestedTypes) {
if (nt is InterfaceGen ni)
nestedInterfaces.Add (new InterfaceExtensionInfo {
DeclaringType = type.FullName.Substring (type.Namespace.Length + 1).Replace (".", "_"),
Type = ni
});
else
AddNestedInterfaceTypes (nt, nestedInterfaces);
}
}
public override ClassGen BaseGen =>
(base_symbol is GenericSymbol ? (base_symbol as GenericSymbol).Gen : base_symbol) as ClassGen;
public string BaseType { get; set; }
public bool ContainsCtor (string jni_sig) => Ctors.Any (c => c.JniSignature == jni_sig);
public bool ContainsNestedType (GenBase gen)
{
if (BaseGen != null && BaseGen.ContainsNestedType (gen))
return true;
return HasNestedType (gen.Name);
}
public List<string> ExplicitlyImplementedInterfaceMethods { get; } = new List<string> (); // do not initialize here; see FixupMethodOverides()
public override void FixupAccessModifiers (CodeGenerationOptions opt)
{
while (!IsAnnotation && !string.IsNullOrEmpty (BaseType)) {
if (opt.SymbolTable.Lookup (BaseType) is ClassGen baseClass && RawVisibility == "public" && baseClass.RawVisibility != "public") {
//Skip the BaseType and copy over any "missing" methods
foreach (var baseMethod in baseClass.Methods) {
var method = Methods.FirstOrDefault (m => m.Matches (baseMethod));
if (method == null)
Methods.Add (baseMethod.Clone (this));
}
BaseType = baseClass.BaseType;
} else {
break;
}
}
base.FixupAccessModifiers (opt);
}
public override void FixupExplicitImplementation ()
{
if (fill_explicit_implementation_started)
return; // already done.
fill_explicit_implementation_started = true;
if (BaseGen != null && BaseGen.ExplicitlyImplementedInterfaceMethods == null)
BaseGen.FixupExplicitImplementation ();
foreach (InterfaceGen iface in GetAllDerivedInterfaces ()) {
if (iface.IsGeneric) {
bool skip = false;
foreach (ISymbol isym in Interfaces) {
if (isym is GenericSymbol gs && gs.IsConcrete && gs.Gen == iface)
skip = true;
}
if (skip)
continue; // we don't handle it here; generic interface methods are generated in different manner.
}
if (BaseGen != null && BaseGen.GetAllDerivedInterfaces ().Contains (iface))
continue; // no need to fill members for already-implemented-in-base-class iface.
foreach (var m in iface.Methods.Where (m => !ContainsMethod (m, false, false))) {
string sig = m.GetSignature ();
bool doExplicitly = false;
if (IsCovariantMethod (m))
doExplicitly = true;
else if (m.IsGeneric)
doExplicitly = true;
if (doExplicitly)
ExplicitlyImplementedInterfaceMethods.Add (sig);
}
}
// Keep in sync with Generate() that generates explicit iface method impl.
foreach (ISymbol isym in Interfaces) {
if (isym is GenericSymbol) {
GenericSymbol gs = isym as GenericSymbol;
if (gs.IsConcrete) {
foreach (Method m in gs.Gen.Methods)
if (m.IsGeneric) {
ExplicitlyImplementedInterfaceMethods.Add (m.GetSignature ());
}
}
}
}
foreach (var nt in NestedTypes)
nt.FixupExplicitImplementation ();
}
public override string FromNative (CodeGenerationOptions opt, string varname, bool owned) => opt.CodeGenerationTarget switch {
CodeGenerationTarget.JavaInterop1 =>
"global::Java.Interop.JniEnvironment.Runtime.ValueManager.GetValue<" +
opt.GetOutputName (FullName) +
$"> (ref {varname}, JniObjectReferenceOptions.{(owned ? "CopyAndDispose" : "Copy")})",
_ =>
$"global::Java.Lang.Object.GetObject<{opt.GetOutputName (FullName)}> ({varname}, {(owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer")})"
};
public bool FromXml { get; set; }
public override void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
{
using (var sw = gen_info.OpenStream (opt.GetFileName (FullName))) {
WriteAutoGeneratedHeader (sw);
sw.WriteLine ("using System;");
sw.WriteLine ("using System.Collections.Generic;");
if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1) {
sw.WriteLine ("using Android.Runtime;");
}
sw.WriteLine ("using Java.Interop;");
sw.WriteLine ();
var hasNamespace = !string.IsNullOrWhiteSpace (Namespace);
if (hasNamespace) {
sw.WriteLine ("namespace {0} {{", Namespace);
sw.WriteLine ();
}
var generator = opt.CreateCodeGenerator (sw);
generator.WriteType (this, hasNamespace ? "\t" : string.Empty, gen_info);
if (hasNamespace) {
sw.WriteLine ("}");
}
}
}
public static void GenerateEnumList (GenerationInfo gen_info)
{
using (var sw = new StreamWriter (File.Create (Path.Combine (gen_info.CSharpDir, "enumlist")))) {
foreach (string e in gen_info.Enums.OrderBy (p => p, StringComparer.OrdinalIgnoreCase))
sw.WriteLine (e);
}
}
public static void GenerateTypeRegistrations (CodeGenerationOptions opt, GenerationInfo gen_info)
{
if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1) {
return;
}
using (var sw = gen_info.OpenStream (opt.GetFileName ("Java.Interop.__TypeRegistrations"))) {
Dictionary<string, List<KeyValuePair<string, string>>> mapping = new Dictionary<string, List<KeyValuePair<string, string>>> ();
foreach (KeyValuePair<string, string> reg in gen_info.TypeRegistrations.OrderBy (p => p.Key, StringComparer.OrdinalIgnoreCase)) {
int ls = reg.Key.LastIndexOf ('/');
string package = ls >= 0 ? reg.Key.Substring (0, ls) : "";
if (JavaNativeTypeManager.ToCliType (reg.Key) == reg.Value)
continue;
if (!mapping.TryGetValue (package, out var v))
mapping.Add (package, v = new List<KeyValuePair<string, string>> ());
v.Add (new KeyValuePair<string, string> (reg.Key, reg.Value));
}
WriteAutoGeneratedHeader (sw);
sw.WriteLine ("using System;");
sw.WriteLine ("using System.Collections.Generic;");
if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1) {
sw.WriteLine ("using Android.Runtime;");
}
sw.WriteLine ();
sw.WriteLine ("namespace Java.Interop {");
sw.WriteLine ();
sw.WriteLine ("\tpartial class __TypeRegistrations {");
sw.WriteLine ();
sw.WriteLine ("\t\tpublic static void RegisterPackages ()");
sw.WriteLine ("\t\t{");
sw.WriteLine ("#if MONODROID_TIMING");
sw.WriteLine ("\t\t\tvar start = DateTime.Now;");
sw.WriteLine ("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages start: \" + (start - new DateTime (1970, 1, 1)).TotalMilliseconds);");
sw.WriteLine ("#endif // def MONODROID_TIMING");
sw.WriteLine ("\t\t\tJava.Interop.TypeManager.RegisterPackages (");
sw.WriteLine ("\t\t\t\t\tnew string[]{");
foreach (KeyValuePair<string, List<KeyValuePair<string, string>>> e in mapping) {
sw.WriteLine ("\t\t\t\t\t\t\"{0}\",", e.Key);
}
sw.WriteLine ("\t\t\t\t\t},");
sw.WriteLine ("\t\t\t\t\tnew Converter<string, Type{0}>[]{{", opt.NullableOperator);
foreach (KeyValuePair<string, List<KeyValuePair<string, string>>> e in mapping) {
sw.WriteLine ("\t\t\t\t\t\tlookup_{0}_package,", e.Key.Replace ('/', '_'));
}
sw.WriteLine ("\t\t\t\t\t});");
sw.WriteLine ("#if MONODROID_TIMING");
sw.WriteLine ("\t\t\tvar end = DateTime.Now;");
sw.WriteLine ("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages time: \" + (end - new DateTime (1970, 1, 1)).TotalMilliseconds + \" [elapsed: \" + (end - start).TotalMilliseconds + \" ms]\");");
sw.WriteLine ("#endif // def MONODROID_TIMING");
sw.WriteLine ("\t\t}");
sw.WriteLine ();
sw.WriteLine ("#if NET5_0_OR_GREATER");
sw.WriteLine("\t\t[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage (\"Trimming\", \"IL2057\")]");
sw.WriteLine ("#endif");
sw.WriteLine ("\t\tstatic Type{0} Lookup (string[] mappings, string javaType)", opt.NullableOperator);
sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tvar managedType = Java.Interop.TypeManager.LookupTypeMapping (mappings, javaType);");
sw.WriteLine ("\t\t\tif (managedType == null)");
sw.WriteLine ("\t\t\t\treturn null;");
sw.WriteLine ("\t\t\treturn Type.GetType (managedType);");
sw.WriteLine ("\t\t}");
foreach (KeyValuePair<string, List<KeyValuePair<string, string>>> map in mapping) {
sw.WriteLine ();
string package = map.Key.Replace ('/', '_');
sw.WriteLine ("\t\tstatic string[]{1} package_{0}_mappings;", package, opt.NullableOperator);
sw.WriteLine ("\t\tstatic Type{1} lookup_{0}_package (string klass)", package, opt.NullableOperator);
sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tif (package_{0}_mappings == null) {{", package);
sw.WriteLine ("\t\t\t\tpackage_{0}_mappings = new string[]{{", package);
map.Value.Sort ((a, b) => string.Compare (a.Key, b.Key, StringComparison.Ordinal));
foreach (KeyValuePair<string, string> t in map.Value) {
sw.WriteLine ("\t\t\t\t\t\"{0}:{1}\",", t.Key, t.Value);
}
sw.WriteLine ("\t\t\t\t};");
sw.WriteLine ("\t\t\t}");
sw.WriteLine ("");
sw.WriteLine ("\t\t\treturn Lookup (package_{0}_mappings, klass);", package);
sw.WriteLine ("\t\t}");
}
sw.WriteLine ("\t}");
sw.WriteLine ("}");
}
}
static void WriteAutoGeneratedHeader (StreamWriter sw)
{
sw.WriteLine ("//------------------------------------------------------------------------------");
sw.WriteLine ("// <auto-generated>");
sw.WriteLine ("// This code was generated by a tool.");
sw.WriteLine ("//");
sw.WriteLine ("// Changes to this file may cause incorrect behavior and will be lost if");
sw.WriteLine ("// the code is regenerated.");
sw.WriteLine ("// </auto-generated>");
sw.WriteLine ("//------------------------------------------------------------------------------");
sw.WriteLine ();
sw.WriteLine ("#nullable restore"); // Roslyn turns off NRT for generated files by default, re-enable it
}
protected override bool GetEnumMappedMemberInfo ()
{
foreach (var m in Ctors)
return true;
return base.GetEnumMappedMemberInfo ();
}
internal IEnumerable<InterfaceExtensionInfo> GetNestedInterfaceTypes ()
{
var nestedInterfaces = new List<InterfaceExtensionInfo> ();
AddNestedInterfaceTypes (this, nestedInterfaces);
return nestedInterfaces;
}
public bool InheritsObject { get; set; }
public bool IsAbstract { get; set; }
public bool IsExplicitlyImplementedMethod (string sig)
{
for (var c = this; c != null; c = c.BaseGen)
if (c.ExplicitlyImplementedInterfaceMethods.Contains (sig))
return true;
return false;
}
public bool IsFinal { get; set; }
public bool NeedsNew { get; set; }
public string PeerConstructorPartialMethod { get; set; }
protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context)
{
if (validated)
return IsValid;
validated = true;
if (!support.OnValidate (opt)) {
IsValid = false;
return false;
}
// We're validating this in prior to BaseType.
if (TypeParameters != null && !TypeParameters.Validate (opt, type_params, context)) {
IsValid = false;
return false;
}
if (char.IsNumber (Name [0])) {
// it is an anonymous class which does not need output.
IsValid = false;
return false;
}
base_symbol = IsAnnotation ? opt.SymbolTable.Lookup ("java.lang.Object") : BaseType != null ? opt.SymbolTable.Lookup (BaseType) : null;
if (base_symbol == null && FullName != "Java.Lang.Object" && FullName != "System.Object") {
Report.LogCodedWarning (0, Report.WarningUnknownBaseType, this, FullName, BaseType);
IsValid = false;
return false;
}
if ((base_symbol != null && !base_symbol.Validate (opt, TypeParameters, context)) || !base.OnValidate (opt, type_params, context)) {
Report.LogCodedWarning (0, Report.WarningInvalidBaseType, this, FullName, BaseType);
IsValid = false;
return false;
}
var valid_ctors = new List<Ctor> ();
foreach (var c in Ctors)
if (c.Validate (opt, TypeParameters, context))
valid_ctors.Add (c);
Ctors = valid_ctors;
return true;
}
public override void ResetValidation ()
{
validated = false;
base.ResetValidation ();
}
public HashSet<string> SkippedInterfaceMethods => skipped_interface_methods ??= new HashSet<string> ();
public override string ToNative (CodeGenerationOptions opt, string varname, Dictionary<string, string> mappings = null)
{
if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1) {
return $"({varname}?.PeerReference ?? default)";
}
return $"JNIEnv.ToLocalJniHandle ({varname})";
}
public override void UpdateEnumsInInterfaceImplementation ()
{
foreach (InterfaceGen iface in GetAllDerivedInterfaces ()) {
if (iface.HasEnumMappedMembers) {
foreach (Method imethod in iface.Methods) {
var method = Methods.FirstOrDefault (m => m.Name == imethod.Name && m.JniSignature == imethod.JniSignature);
if (method != null) {
if (imethod.IsReturnEnumified)
method.RetVal.SetGeneratedEnumType (imethod.RetVal.FullName);
for (int i = 0; i < imethod.Parameters.Count; i++)
if (imethod.Parameters [i].IsEnumified)
method.Parameters [i].SetGeneratedEnumType (imethod.Parameters [i].Type);
}
}
}
}
base.UpdateEnumsInInterfaceImplementation ();
}
}
}