-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathJavaCallableWrapperGenerator.cs
More file actions
1154 lines (1019 loc) · 39.3 KB
/
JavaCallableWrapperGenerator.cs
File metadata and controls
1154 lines (1019 loc) · 39.3 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Android.Runtime;
using Java.Interop.Tools.Cecil;
using Java.Interop.Tools.Diagnostics;
using Java.Interop.Tools.TypeNameMappings;
using MethodAttributes = Mono.Cecil.MethodAttributes;
using static Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager;
namespace Java.Interop.Tools.JavaCallableWrappers {
public enum JavaPeerStyle {
XAJavaInterop1,
JavaInterop1,
}
public abstract class JavaCallableMethodClassifier
{
public abstract bool ShouldBeDynamicallyRegistered (TypeDefinition topType, MethodDefinition registeredMethod, MethodDefinition implementedMethod, CustomAttribute? registerAttribute);
}
public class JavaCallableWrapperGenerator {
class JavaFieldInfo {
public JavaFieldInfo (MethodDefinition method, string fieldName, IMetadataResolver resolver)
{
this.FieldName = fieldName;
InitializerName = method.Name;
TypeName = JavaNativeTypeManager.ReturnTypeFromSignature (GetJniSignature (method, resolver))?.Type
?? throw new ArgumentException ($"Could not get JNI signature for method `{method.Name}`", nameof (method));
IsStatic = method.IsStatic;
Access = method.Attributes & MethodAttributes.MemberAccessMask;
Annotations = GetAnnotationsString ("\t", method.CustomAttributes, resolver);
}
public MethodAttributes Access { get; private set; }
public bool IsStatic { get; private set; }
public string TypeName { get; private set; }
public string FieldName { get; private set; }
public string InitializerName { get; private set; }
public string Annotations { get; private set; }
public string GetJavaAccess ()
{
return JavaCallableWrapperGenerator.GetJavaAccess (Access);
}
}
Action<string, object[]> log;
string name;
string package;
TypeDefinition type;
List<JavaFieldInfo> exported_fields = new List<JavaFieldInfo> ();
List<Signature> methods = new List<Signature> ();
List<Signature> ctors = new List<Signature> ();
List<JavaCallableWrapperGenerator>? children;
readonly IMetadataResolver cache;
readonly JavaCallableMethodClassifier? methodClassifier;
[Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)]
public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object []> log) => throw new NotSupportedException ();
public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[]> log, TypeDefinitionCache cache)
: this (type, log, (IMetadataResolver) cache, methodClassifier: null)
{ }
public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[]> log, TypeDefinitionCache cache, JavaCallableMethodClassifier? methodClassifier)
: this (type, log, (IMetadataResolver) cache, methodClassifier)
{
}
public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[]> log, IMetadataResolver resolver)
: this (type, log, resolver, methodClassifier: null)
{ }
public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[]> log, IMetadataResolver resolver, JavaCallableMethodClassifier? methodClassifier)
: this (type, null, log, resolver, methodClassifier)
{
AddNestedTypes (type);
}
public string? ApplicationJavaClass { get; set; }
public JavaPeerStyle CodeGenerationTarget { get; set; }
public bool GenerateOnCreateOverrides { get; set; }
public bool HasExport { get; private set; }
// If there are no methods, we need to generate "empty" registration because of backward compatibility
public bool HasDynamicallyRegisteredMethods => methods.Count == 0 || methods.Any ((Signature sig) => sig.IsDynamicallyRegistered);
/// <summary>
/// The Java source code to be included in Instrumentation.onCreate
///
/// Originally came from MonoRuntimeProvider.java delimited by:
/// // Mono Runtime Initialization {{{
/// // }}}
/// </summary>
public string? MonoRuntimeInitialization { get; set; }
public string Name {
get { return name; }
}
void AddNestedTypes (TypeDefinition type)
{
if (!type.HasNestedTypes) {
return;
}
children = children ?? new List<JavaCallableWrapperGenerator> ();
foreach (TypeDefinition nt in type.NestedTypes) {
if (!nt.IsSubclassOf ("Java.Lang.Object", cache))
continue;
if (!JavaNativeTypeManager.IsNonStaticInnerClass (nt, cache))
continue;
children.Add (new JavaCallableWrapperGenerator (nt, JavaNativeTypeManager.ToJniName (type, cache), log, cache));
AddNestedTypes (nt);
}
HasExport |= children.Any (t => t.HasExport);
}
JavaCallableWrapperGenerator (TypeDefinition type, string? outerType, Action<string, object[]> log, IMetadataResolver resolver, JavaCallableMethodClassifier? methodClassifier = null)
{
this.methodClassifier = methodClassifier;
this.type = type;
this.log = log;
this.cache = resolver ?? new TypeDefinitionCache ();
if (type.IsEnum || type.IsInterface || type.IsValueType)
Diagnostic.Error (4200, LookupSource (type), Localization.Resources.JavaCallableWrappers_XA4200, type.FullName);
string jniName = JavaNativeTypeManager.ToJniName (type, this.cache);
if (jniName == null) {
Diagnostic.Error (4201, LookupSource (type), Localization.Resources.JavaCallableWrappers_XA4201, type.FullName);
throw new InvalidOperationException ("--nrt:jniName-- Should not be reached");
}
if (outerType != null && !string.IsNullOrEmpty (outerType)) {
string p;
jniName = jniName.Substring (outerType.Length + 1);
ExtractJavaNames (outerType, out p, out outerType);
}
ExtractJavaNames (jniName, out package, out name);
if (string.IsNullOrEmpty (package) &&
(type.IsSubclassOf ("Android.App.Activity", cache) ||
type.IsSubclassOf ("Android.App.Application", cache) ||
type.IsSubclassOf ("Android.App.Service", cache) ||
type.IsSubclassOf ("Android.Content.BroadcastReceiver", cache) ||
type.IsSubclassOf ("Android.Content.ContentProvider", cache)))
Diagnostic.Error (4203, LookupSource (type), Localization.Resources.JavaCallableWrappers_XA4203, jniName);
foreach (MethodDefinition minfo in type.Methods.Where (m => !m.IsConstructor)) {
var baseRegisteredMethod = GetBaseRegisteredMethod (minfo);
if (baseRegisteredMethod != null)
AddMethod (baseRegisteredMethod, minfo);
else if (minfo.AnyCustomAttributes ("Java.Interop.JavaCallableAttribute")) {
AddMethod (null, minfo);
HasExport = true;
} else if (minfo.AnyCustomAttributes ("Java.Interop.JavaCallableConstructorAttribute")) {
AddMethod (null, minfo);
HasExport = true;
} else if (minfo.AnyCustomAttributes (typeof(ExportFieldAttribute))) {
AddMethod (null, minfo);
HasExport = true;
} else if (minfo.AnyCustomAttributes (typeof (ExportAttribute))) {
AddMethod (null, minfo);
HasExport = true;
}
}
foreach (InterfaceImplementation ifaceInfo in type.Interfaces) {
var typeReference = ifaceInfo.InterfaceType;
var typeDefinition = cache.Resolve (typeReference);
if (typeDefinition == null) {
Diagnostic.Error (4204,
LookupSource (type),
Localization.Resources.JavaCallableWrappers_XA4204,
typeReference.FullName);
continue;
}
if (!GetTypeRegistrationAttributes (typeDefinition).Any ())
continue;
foreach (MethodDefinition imethod in typeDefinition.Methods) {
if (imethod.IsStatic)
continue;
AddMethod (imethod, imethod);
}
}
var ctorTypes = new List<TypeDefinition> () {
type,
};
foreach (var bt in type.GetBaseTypes (cache)) {
ctorTypes.Add (bt);
RegisterAttribute rattr = GetMethodRegistrationAttributes (bt).FirstOrDefault ();
if (rattr != null && rattr.DoNotGenerateAcw)
break;
}
ctorTypes.Reverse ();
var curCtors = new List<MethodDefinition> ();
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");
}
else {
AddConstructor (minfo, ctorTypes [0], outerType, null, curCtors, false, true);
HasExport = true;
}
}
}
AddConstructors (ctorTypes [0], outerType, null, curCtors, true);
for (int i = 1; i < ctorTypes.Count; ++i) {
var baseCtors = curCtors;
curCtors = new List<MethodDefinition> ();
AddConstructors (ctorTypes [i], outerType, baseCtors, curCtors, false);
}
}
static void ExtractJavaNames (string jniName, out string package, out string type)
{
int i = jniName.LastIndexOf ('/');
if (i < 0) {
type = jniName;
package = string.Empty;
}
else {
type = jniName.Substring (i+1);
package = jniName.Substring (0, i).Replace ('/', '.');
}
}
static SequencePoint? LookupSource (MethodDefinition method)
{
if (!method.HasBody)
return null;
foreach (var ins in method.Body.Instructions) {
var seqPoint = method.DebugInformation.GetSequencePoint (ins);
if (seqPoint != null)
return seqPoint;
}
return null;
}
static SequencePoint? LookupSource (TypeDefinition type)
{
SequencePoint? candidate = null;
foreach (var method in type.Methods) {
if (!method.HasBody)
continue;
foreach (var ins in method.Body.Instructions) {
var seq = method.DebugInformation.GetSequencePoint (ins);
if (seq == null)
continue;
if (Regex.IsMatch (seq.Document.Url, ".+\\.(g|designer)\\..+"))
break;
if (candidate == null || seq.StartLine < candidate.StartLine)
candidate = seq;
break;
}
}
return candidate;
}
void AddConstructors (TypeDefinition type, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> curCtors, bool onlyRegisteredOrExportedCtors)
{
foreach (MethodDefinition ctor in type.Methods)
if (ctor.IsConstructor && !ctor.IsStatic && !ctor.AnyCustomAttributes (typeof (ExportAttribute)))
AddConstructor (ctor, type, outerType, baseCtors, curCtors, onlyRegisteredOrExportedCtors, false);
}
void AddConstructor (MethodDefinition ctor, TypeDefinition type, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> curCtors, bool onlyRegisteredOrExportedCtors, bool skipParameterCheck)
{
string managedParameters = GetManagedParameters (ctor, outerType);
if (!skipParameterCheck && (managedParameters == null || ctors.Any (c => c.ManagedParameters == managedParameters))) {
return;
}
ExportAttribute eattr = GetExportAttributes (ctor).FirstOrDefault ();
if (eattr != null) {
if (!string.IsNullOrEmpty (eattr.Name)) {
// Diagnostic.Warning (log, "Use of ExportAttribute.Name property is invalid on constructors");
}
ctors.Add (new Signature (ctor, eattr, managedParameters, cache));
curCtors.Add (ctor);
return;
}
RegisterAttribute rattr = GetMethodRegistrationAttributes (ctor).FirstOrDefault ();
if (rattr != null) {
if (ctors.Any (c => c.JniSignature == rattr.Signature))
return;
ctors.Add (new Signature (ctor, rattr, managedParameters, outerType, cache));
curCtors.Add (ctor);
return;
}
if (onlyRegisteredOrExportedCtors)
return;
string? jniSignature = GetJniSignature (ctor, cache);
if (jniSignature == null)
return;
if (ctors.Any (c => c.JniSignature == jniSignature))
return;
if (baseCtors == null) {
throw new InvalidOperationException ("`baseCtors` should not be null!");
}
if (baseCtors.Any (m => m.Parameters.AreParametersCompatibleWith (ctor.Parameters, cache))) {
ctors.Add (new Signature (".ctor", jniSignature, "", managedParameters, outerType, null));
curCtors.Add (ctor);
return;
}
if (baseCtors.Any (m => !m.HasParameters)) {
ctors.Add (new Signature (".ctor", jniSignature, "", managedParameters, outerType, ""));
curCtors.Add (ctor);
return;
}
}
MethodDefinition? GetBaseRegisteredMethod (MethodDefinition method)
{
MethodDefinition bmethod;
while ((bmethod = method.GetBaseDefinition (cache)) != method) {
method = bmethod;
if (method.AnyCustomAttributes (typeof (RegisterAttribute))) {
return method;
}
}
return null;
}
internal static RegisterAttribute? ToRegisterAttribute (CustomAttribute attr)
{
// attr.Resolve ();
RegisterAttribute? r = null;
if (attr.ConstructorArguments.Count == 1)
r = new RegisterAttribute ((string) attr.ConstructorArguments [0].Value, attr);
else if (attr.ConstructorArguments.Count == 3)
r = new RegisterAttribute (
(string) attr.ConstructorArguments [0].Value,
(string) attr.ConstructorArguments [1].Value,
(string) attr.ConstructorArguments [2].Value,
attr);
if (r != null) {
var v = attr.Properties.FirstOrDefault (p => p.Name == "DoNotGenerateAcw");
r.DoNotGenerateAcw = v.Name == null ? false : (bool) v.Argument.Value;
}
return r;
}
internal static RegisterAttribute? RegisterFromJniTypeSignatureAttribute (CustomAttribute attr)
{
// attr.Resolve ();
RegisterAttribute? r = null;
if (attr.ConstructorArguments.Count == 1)
r = new RegisterAttribute ((string) attr.ConstructorArguments [0].Value, attr);
if (r != null) {
var v = attr.Properties.FirstOrDefault (p => p.Name == "GenerateJavaPeer");
if (v.Name == null) {
r.DoNotGenerateAcw = false;
} else if (v.Name == "GenerateJavaPeer") {
r.DoNotGenerateAcw = ! (bool) v.Argument.Value;
}
}
return r;
}
internal static RegisterAttribute? RegisterFromJniMethodSignatureAttribute (CustomAttribute attr)
{
// attr.Resolve ();
RegisterAttribute? r = null;
if (attr.ConstructorArguments.Count == 2)
r = new RegisterAttribute ((string) attr.ConstructorArguments [0].Value,
(string) attr.ConstructorArguments [1].Value,
"",
attr);
return r;
}
ExportAttribute ToExportAttribute (CustomAttribute attr, IMemberDefinition declaringMember)
{
var name = attr.ConstructorArguments.Count > 0 ? (string) attr.ConstructorArguments [0].Value : declaringMember.Name;
if (attr.Properties.Count == 0)
return new ExportAttribute (name);
var typeArgs = (CustomAttributeArgument []) attr.Properties.FirstOrDefault (p => p.Name == "Throws").Argument.Value;
var thrown = typeArgs != null && typeArgs.Any ()
? (from caa in typeArgs select JavaNativeTypeManager.Parse (GetJniTypeName ((TypeReference)caa.Value, cache))?.Type)
.Where (v => v != null)
.ToArray ()
: null;
var superArgs = (string) attr.Properties.FirstOrDefault (p => p.Name == "SuperArgumentsString").Argument.Value;
return new ExportAttribute (name) {ThrownNames = thrown, SuperArgumentsString = superArgs};
}
ExportAttribute ToExportAttributeFromJavaCallableAttribute (CustomAttribute attr, IMemberDefinition declaringMember)
{
var name = attr.ConstructorArguments.Count > 0
? (string) attr.ConstructorArguments [0].Value
: declaringMember.Name;
return new ExportAttribute (name);
}
ExportAttribute ToExportAttributeFromJavaCallableConstructorAttribute (CustomAttribute attr, IMemberDefinition declaringMember)
{
var superArgs = (string) attr.Properties
.FirstOrDefault (p => p.Name == "SuperConstructorExpression")
.Argument
.Value;
return new ExportAttribute (".ctor") {
SuperArgumentsString = superArgs,
};
}
internal static ExportFieldAttribute ToExportFieldAttribute (CustomAttribute attr)
{
return new ExportFieldAttribute ((string) attr.ConstructorArguments [0].Value);
}
internal static IEnumerable<RegisterAttribute> GetTypeRegistrationAttributes (Mono.Cecil.ICustomAttributeProvider p)
{
foreach (var a in GetAttributes<RegisterAttribute> (p, a => ToRegisterAttribute (a))) {
yield return a;
}
foreach (var c in p.GetCustomAttributes ("Java.Interop.JniTypeSignatureAttribute")) {
var r = RegisterFromJniTypeSignatureAttribute (c);
if (r == null) {
continue;
}
yield return r;
}
}
static IEnumerable<RegisterAttribute> GetMethodRegistrationAttributes (Mono.Cecil.ICustomAttributeProvider p)
{
foreach (var a in GetAttributes<RegisterAttribute> (p, a => ToRegisterAttribute (a))) {
yield return a;
}
foreach (var c in p.GetCustomAttributes ("Java.Interop.JniMethodSignatureAttribute")) {
var r = RegisterFromJniMethodSignatureAttribute (c);
if (r == null) {
continue;
}
yield return r;
}
}
IEnumerable<ExportAttribute> GetExportAttributes (IMemberDefinition p)
{
return GetAttributes<ExportAttribute> (p, a => ToExportAttribute (a, p))
.Concat (GetAttributes<ExportAttribute> (p, "Java.Interop.JavaCallableAttribute",
a => ToExportAttributeFromJavaCallableAttribute (a, p)))
.Concat (GetAttributes<ExportAttribute> (p, "Java.Interop.JavaCallableConstructorAttribute",
a => ToExportAttributeFromJavaCallableConstructorAttribute (a, p)));
}
static IEnumerable<ExportFieldAttribute> GetExportFieldAttributes (Mono.Cecil.ICustomAttributeProvider p)
{
return GetAttributes<ExportFieldAttribute> (p, a => ToExportFieldAttribute (a));
}
static IEnumerable<TAttribute> GetAttributes<TAttribute> (Mono.Cecil.ICustomAttributeProvider p, Func<CustomAttribute, TAttribute?> selector)
where TAttribute : class
{
return GetAttributes (p, typeof (TAttribute).FullName, selector);
}
static IEnumerable<TAttribute> GetAttributes<TAttribute> (Mono.Cecil.ICustomAttributeProvider p, string attributeName, Func<CustomAttribute, TAttribute?> selector)
where TAttribute : class
{
return p.GetCustomAttributes (attributeName)
.Select (selector)
.Where (v => v != null)
.Select (v => v!);
}
void AddMethod (MethodDefinition? registeredMethod, MethodDefinition implementedMethod)
{
if (registeredMethod != null)
foreach (RegisterAttribute attr in GetMethodRegistrationAttributes (registeredMethod)) {
// Check for Kotlin-mangled methods that cannot be overridden
if (attr.Name.Contains ("-impl") || (attr.Name.Length > 7 && attr.Name[attr.Name.Length - 8] == '-'))
Diagnostic.Error (4217, LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4217, attr.Name);
bool shouldBeDynamicallyRegistered = methodClassifier?.ShouldBeDynamicallyRegistered (type, registeredMethod, implementedMethod, attr.OriginAttribute) ?? true;
var msig = new Signature (implementedMethod, attr, cache, shouldBeDynamicallyRegistered);
if (!registeredMethod.IsConstructor && !methods.Any (m => m.Name == msig.Name && m.Params == msig.Params))
methods.Add (msig);
}
foreach (ExportAttribute attr in GetExportAttributes (implementedMethod)) {
if (type.HasGenericParameters)
Diagnostic.Error (4206, LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4206);
var msig = new Signature (implementedMethod, attr, managedParameters: null, cache: cache);
if (!string.IsNullOrEmpty (attr.SuperArgumentsString)) {
// Diagnostic.Warning (log, "Use of ExportAttribute.SuperArgumentsString property is invalid on methods");
}
if (!implementedMethod.IsConstructor && !methods.Any (m => m.Name == msig.Name && m.Params == msig.Params))
methods.Add (msig);
}
foreach (ExportFieldAttribute attr in GetExportFieldAttributes (implementedMethod)) {
if (type.HasGenericParameters)
Diagnostic.Error (4207, LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4207);
var msig = new Signature (implementedMethod, attr, cache);
if (!implementedMethod.IsConstructor && !methods.Any (m => m.Name == msig.Name && m.Params == msig.Params)) {
methods.Add (msig);
exported_fields.Add (new JavaFieldInfo (implementedMethod, attr.Name, cache));
}
}
}
string GetManagedParameters (MethodDefinition ctor, string? outerType)
{
StringBuilder sb = new StringBuilder ();
foreach (ParameterDefinition pdef in ctor.Parameters) {
if (sb.Length > 0)
sb.Append (':');
if (outerType != null && sb.Length == 0)
sb.Append (type.DeclaringType.GetPartialAssemblyQualifiedName (cache));
else
sb.Append (pdef.ParameterType.GetPartialAssemblyQualifiedName (cache));
}
return sb.ToString ();
}
// example of java target to generate for a type
//
// package mono.droid;
//
// import android.app.Activity;
// import android.os.Bundle;
//
// public class MonoActivity extends android.app.Activity
// {
// static final String __md_methods;
// static {
// __md_methods =
// "n_OnCreate:(Landroid/os/Bundle;)V:GetOnCreate_Landroid_os_Bundle_Handler\n" +
// "";
// mono.android.Runtime.register ("Mono.Droid.MonoActivity, AssemblyName", MonoActivity.class, __md_methods);
// }
//
// public void onCreate(android.os.Bundle savedInstanceState)
// {
// n_onCreate (savedInstanceState);
// }
//
// private native void n_onCreate (android.os.Bundle bundle);
// }
public void Generate (TextWriter writer)
{
if (!string.IsNullOrEmpty (package)) {
writer.WriteLine ("package " + package + ";");
writer.WriteLine ();
}
GenerateHeader (writer);
bool needCtor = false;
if (HasDynamicallyRegisteredMethods) {
needCtor = true;
writer.WriteLine ("/** @hide */");
writer.WriteLine ("\tpublic static final String __md_methods;");
}
if (children != null) {
for (int i = 0; i < children.Count; i++) {
if (!children[i].HasDynamicallyRegisteredMethods) {
continue;
}
needCtor = true;
writer.Write ("\tstatic final String __md_");
writer.Write (i + 1);
writer.WriteLine ("_methods;");
}
}
if (needCtor) {
writer.WriteLine ("\tstatic {");
if (HasDynamicallyRegisteredMethods) {
GenerateRegisterType (writer, this, "__md_methods");
}
if (children != null) {
for (int i = 0; i < children.Count; ++i) {
GenerateRegisterType (writer, children [i], $"__md_{i + 1}_methods");
}
}
writer.WriteLine ("\t}");
}
GenerateBody (writer);
if (children != null)
foreach (JavaCallableWrapperGenerator child in children) {
child.GenerateHeader (writer);
child.GenerateBody (writer);
child.GenerateFooter (writer);
}
GenerateFooter (writer);
}
public void Generate (string outputPath)
{
using (StreamWriter sw = OpenStream (outputPath)) {
Generate (sw);
}
}
static string GetAnnotationsString (string indent, IEnumerable<CustomAttribute> atts, IMetadataResolver resolver)
{
var sw = new StringWriter ();
WriteAnnotations (indent, sw, atts, resolver);
return sw.ToString ();
}
static void WriteAnnotations (string indent, TextWriter sw, IEnumerable<CustomAttribute> atts, IMetadataResolver resolver)
{
foreach (var ca in atts) {
var catype = resolver.Resolve (ca.AttributeType);
var tca = catype.CustomAttributes.FirstOrDefault (a => a.AttributeType.FullName == "Android.Runtime.AnnotationAttribute");
if (tca != null) {
sw.Write (indent);
sw.Write ('@');
sw.Write (tca.ConstructorArguments [0].Value);
if (ca.Properties.Count > 0) {
sw.WriteLine ("(");
bool wrote = false;
foreach (var p in ca.Properties) {
if (wrote)
sw.WriteLine (',');
var pd = catype.Properties.FirstOrDefault (pp => pp.Name == p.Name);
var reg = pd != null ? pd.CustomAttributes.FirstOrDefault (pdca => pdca.AttributeType.FullName == "Android.Runtime.RegisterAttribute") : null;
sw.Write (reg != null ? reg.ConstructorArguments [0].Value : p.Name);
sw.Write (" = ");
sw.Write (ManagedValueToJavaSource (p.Argument.Value));
wrote = true;
}
sw.Write (")");
}
sw.WriteLine ();
}
}
}
// FIXME: this is hacky. Is there any existing code for value to source conversion?
static string ManagedValueToJavaSource (object value)
{
if (value is string)
return "\"" + value.ToString ().Replace ("\"", "\"\"") + '"';
else if (value.GetType ().FullName == "Java.Lang.Class")
return value.ToString () + ".class";
else if (value is bool)
return ((bool) value) ? "true" : "false";
else
return value.ToString ();
}
void GenerateHeader (TextWriter sw)
{
sw.WriteLine ();
// class annotations.
WriteAnnotations ("", sw, type.CustomAttributes, cache);
sw.WriteLine ("public " + (type.IsAbstract ? "abstract " : "") + "class " + name);
string extendsType = GetJavaTypeName (type.BaseType, cache);
if (extendsType == "android.app.Application" && ApplicationJavaClass != null && !string.IsNullOrEmpty (ApplicationJavaClass))
extendsType = ApplicationJavaClass;
sw.WriteLine ("\textends " + extendsType);
sw.WriteLine ("\timplements");
sw.Write ("\t\t");
switch (CodeGenerationTarget) {
case JavaPeerStyle.JavaInterop1:
sw.Write ("com.xamarin.java_interop.GCUserPeerable");
break;
default:
sw.Write ("mono.android.IGCUserPeer");
break;
}
foreach (var ifaceInfo in type.Interfaces) {
var iface = cache.Resolve(ifaceInfo.InterfaceType);
if (!GetTypeRegistrationAttributes (iface).Any ())
continue;
sw.WriteLine (",");
sw.Write ("\t\t");
sw.Write (GetJavaTypeName (iface, cache));
}
sw.WriteLine ();
sw.WriteLine ("{");
}
void GenerateBody (TextWriter sw)
{
foreach (Signature ctor in ctors) {
if (string.IsNullOrEmpty (ctor.Params) && JavaNativeTypeManager.IsApplication (type, cache))
continue;
GenerateConstructor (ctor, sw);
}
GenerateApplicationConstructor (sw);
foreach (JavaFieldInfo field in exported_fields)
GenerateExportedField (field, sw);
foreach (Signature method in methods)
GenerateMethod (method, sw);
if (GenerateOnCreateOverrides && JavaNativeTypeManager.IsApplication (type, cache) && !methods.Any (m => m.Name == "onCreate"))
WriteApplicationOnCreate (sw, w => {
w.Write ("\t\tmono.android.Runtime.register (\"");
w.Write (type.GetPartialAssemblyQualifiedName (cache));
w.Write ("\", ");
w.Write (name);
w.WriteLine (".class, __md_methods);");
w.WriteLine ("\t\tsuper.onCreate ();");
});
if (GenerateOnCreateOverrides && JavaNativeTypeManager.IsInstrumentation (type, cache) && !methods.Any (m => m.Name == "onCreate"))
WriteInstrumentationOnCreate (sw, w => {
w.Write ("\t\tmono.android.Runtime.register (\"");
w.Write (type.GetPartialAssemblyQualifiedName (cache));
w.Write ("\", ");
w.Write (name);
w.WriteLine (".class, __md_methods);");
w.WriteLine ("\t\tsuper.onCreate (arguments);");
});
string addRef = "monodroidAddReference";
string clearRefs = "monodroidClearReferences";
if (CodeGenerationTarget == JavaPeerStyle.JavaInterop1) {
addRef = "jiAddManagedReference";
clearRefs = "jiClearManagedReferences";
}
sw.WriteLine ();
sw.WriteLine ("\tprivate java.util.ArrayList refList;");
sw.WriteLine ($"\tpublic void {addRef} (java.lang.Object obj)");
sw.WriteLine ("\t{");
sw.WriteLine ("\t\tif (refList == null)");
sw.WriteLine ("\t\t\trefList = new java.util.ArrayList ();");
sw.WriteLine ("\t\trefList.add (obj);");
sw.WriteLine ("\t}");
sw.WriteLine ();
sw.WriteLine ($"\tpublic void {clearRefs} ()");
sw.WriteLine ("\t{");
sw.WriteLine ("\t\tif (refList != null)");
sw.WriteLine ("\t\t\trefList.clear ();");
sw.WriteLine ("\t}");
}
void GenerateRegisterType (TextWriter sw, JavaCallableWrapperGenerator self, string field)
{
if (!self.HasDynamicallyRegisteredMethods) {
return;
}
sw.Write ("\t\t");
sw.Write (field);
sw.WriteLine (" = ");
string managedTypeName = self.type.GetPartialAssemblyQualifiedName (cache);
string javaTypeName = $"{package}.{name}";
foreach (Signature method in self.methods) {
if (method.IsDynamicallyRegistered) {
sw.Write ("\t\t\t\"", method.Method);
sw.Write (method.Method);
sw.WriteLine ("\\n\" +");
}
}
sw.WriteLine ("\t\t\t\"\";");
if (CannotRegisterInStaticConstructor (self.type))
return;
sw.Write ("\t\t");
switch (CodeGenerationTarget) {
case JavaPeerStyle.JavaInterop1:
sw.Write ("com.xamarin.java_interop.ManagedPeer.registerNativeMembers (");
sw.Write (self.name);
sw.Write (".class, \"");
sw.Write (managedTypeName);
sw.Write ("\", ");
sw.Write (field);
sw.WriteLine (");");
break;
default:
sw.Write ("mono.android.Runtime.register (\"");
sw.Write (managedTypeName);
sw.Write ("\", ");
sw.Write (self.name);
sw.Write (".class, ");
sw.Write (field);
sw.WriteLine (");");
break;
}
}
void GenerateFooter (TextWriter sw)
{
sw.WriteLine ("}");
}
static string GetJavaAccess (MethodAttributes access)
{
switch (access) {
case MethodAttributes.Public:
return "public";
case MethodAttributes.FamORAssem:
return "protected";
case MethodAttributes.Family:
return "protected";
default:
return "private";
}
}
static string GetJavaTypeName (TypeReference r, IMetadataResolver cache)
{
TypeDefinition d = cache.Resolve (r);
string? jniName = JavaNativeTypeManager.ToJniName (d, cache);
if (jniName == null) {
Diagnostic.Error (4201, Localization.Resources.JavaCallableWrappers_XA4201, r.FullName);
throw new InvalidOperationException ("--nrt:jniName-- Should not be reached");
}
return jniName.Replace ('/', '.').Replace ('$', '.');
}
bool CannotRegisterInStaticConstructor (TypeDefinition type)
{
return JavaNativeTypeManager.IsApplication (type, cache) || JavaNativeTypeManager.IsInstrumentation (type, cache);
}
class Signature {
public Signature (MethodDefinition method, RegisterAttribute register, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true) : this (method, register, null, null, cache, shouldBeDynamicallyRegistered) {}
public Signature (MethodDefinition method, RegisterAttribute register, string? managedParameters, string? outerType, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true)
: this (register.Name, register.Signature, register.Connector, managedParameters, outerType, null)
{
Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache);
IsDynamicallyRegistered = shouldBeDynamicallyRegistered;
}
public Signature (MethodDefinition method, ExportAttribute export, string? managedParameters, IMetadataResolver cache)
: this (method.Name, GetJniSignature (method, cache), "__export__", null, null, export.SuperArgumentsString)
{
IsExport = true;
IsStatic = method.IsStatic;
JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask);
ThrownTypeNames = export.ThrownNames;
JavaNameOverride = export.Name;
ManagedParameters = managedParameters;
Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache);
}
public Signature (MethodDefinition method, ExportFieldAttribute exportField, IMetadataResolver cache)
: this (method.Name, GetJniSignature (method, cache), "__export__", null, null, null)
{
if (method.HasParameters)
Diagnostic.Error (4205, JavaCallableWrapperGenerator.LookupSource (method), Localization.Resources.JavaCallableWrappers_XA4205);
if (method.ReturnType.MetadataType == MetadataType.Void)
Diagnostic.Error (4208, JavaCallableWrapperGenerator.LookupSource (method), Localization.Resources.JavaCallableWrappers_XA4208);
IsExport = true;
IsStatic = method.IsStatic;
JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask);
// annotations are processed within JavaFieldInfo, not the initializer method. So we don't generate them here.
}
public Signature (string name, string? signature, string? connector, string? managedParameters, string? outerType, string? superCall)
{
ManagedParameters = managedParameters;
JniSignature = signature ?? throw new ArgumentNullException ("`connector` cannot be null.", nameof (connector));
Method = "n_" + name + ":" + JniSignature + ":" + connector;
Name = name;
var jnisig = JniSignature;
int closer = jnisig.IndexOf (')');
string ret = jnisig.Substring (closer + 1);
retval = JavaNativeTypeManager.Parse (ret)?.Type;
string jniparms = jnisig.Substring (1, closer - 1);
if (string.IsNullOrEmpty (jniparms) && string.IsNullOrEmpty (superCall))
return;
var parms = new StringBuilder ();
var scall = new StringBuilder ();
var acall = new StringBuilder ();
bool first = true;
int i = 0;
foreach (JniTypeName jti in JavaNativeTypeManager.FromSignature (jniparms)) {
if (outerType != null) {
acall.Append (outerType).Append (".this");
outerType = null;
continue;
}
string? parmType = jti.Type;
if (!first) {
parms.Append (", ");
scall.Append (", ");
acall.Append (", ");
}
first = false;
parms.Append (parmType).Append (" p").Append (i);
scall.Append ("p").Append (i);
acall.Append ("p").Append (i);
++i;
}
this.parms = parms.ToString ();
this.call = superCall != null ? superCall : scall.ToString ();
this.ActivateCall = acall.ToString ();
}
string? call;
public string? SuperCall {
get { return call; }
}
public string? ActivateCall {get; private set;}
public readonly string Name;
public readonly string? JavaNameOverride;
public string JavaName {
get { return JavaNameOverride ?? Name; }
}
string? parms;
public string? Params {
get { return parms; }
}
string? retval;
public string? Retval {
get { return retval; }
}
public string? ThrowsDeclaration {
get { return ThrownTypeNames?.Length > 0 ? " throws " + String.Join (", ", ThrownTypeNames) : null; }
}
public readonly string? JavaAccess;
public readonly string? ManagedParameters;
public readonly string JniSignature;
public readonly string Method;
public readonly bool IsExport;
public readonly bool IsStatic;
public readonly bool IsDynamicallyRegistered = true;
public readonly string []? ThrownTypeNames;
public readonly string? Annotations;
}
void GenerateConstructor (Signature ctor, TextWriter sw)
{
// TODO: we only generate constructors so that Android types w/ no
// default constructor can be subclasses by our generated code.
//
// This does NOT currently allow creating managed types from Java.
sw.WriteLine ();
if (ctor.Annotations != null)
sw.WriteLine (ctor.Annotations);
sw.Write ("\tpublic ");
sw.Write (name);
sw.Write (" (");
sw.Write (ctor.Params);
sw.Write (')');
sw.WriteLine (ctor.ThrowsDeclaration);
sw.WriteLine ("\t{");
sw.Write ("\t\tsuper (");
sw.Write (ctor.SuperCall);
sw.WriteLine (");");
#if MONODROID_TIMING
sw.WriteLine ("\t\tandroid.util.Log.i(\"MonoDroid-Timing\", \"{0}..ctor({1}): time: \"+java.lang.System.currentTimeMillis());", name, ctor.Params);
#endif
if (!CannotRegisterInStaticConstructor (type)) {