-
Notifications
You must be signed in to change notification settings - Fork 531
/
AndroidRuntime.cs
957 lines (821 loc) · 32.3 KB
/
AndroidRuntime.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading;
using System.Reflection;
using Java.Interop;
using Java.Interop.Tools.TypeNameMappings;
using System.Diagnostics.CodeAnalysis;
#if JAVA_INTEROP
namespace Android.Runtime {
class AndroidRuntime : JniRuntime {
public const string InternalDllName = RuntimeConstants.InternalDllName;
internal AndroidRuntime (IntPtr jnienv,
IntPtr vm,
IntPtr classLoader,
IntPtr classLoader_loadClass,
bool jniAddNativeMethodRegistrationAttributePresent)
: base (new AndroidRuntimeOptions (jnienv,
vm,
classLoader,
classLoader_loadClass,
jniAddNativeMethodRegistrationAttributePresent))
{
// This is not ideal, but we need to set this while the runtime is initializing but we can't do it directly from the `JNIEnvInit.Initialize` method, since
// it lives in an assembly that does not reference Mono.Android. So we do it here, because this class is instantiated by JNIEnvInit.Initialize.
AndroidEnvironmentInternal.UnhandledExceptionHandler = AndroidEnvironment.UnhandledException;
}
public override void FailFast (string? message)
{
AndroidEnvironment.FailFast (message);
}
public override string GetCurrentManagedThreadName ()
{
return Thread.CurrentThread.Name!;
}
public override string GetCurrentManagedThreadStackTrace (int skipFrames, bool fNeedFileInfo)
{
return new StackTrace (skipFrames, fNeedFileInfo)
.ToString ();
}
public override Exception? GetExceptionForThrowable (ref JniObjectReference reference, JniObjectReferenceOptions options)
{
if (!reference.IsValid)
return null;
var peeked = JNIEnvInit.AndroidValueManager?.PeekPeer (reference);
var peekedExc = peeked as Exception;
if (peekedExc == null) {
var throwable = Java.Lang.Object.GetObject<Java.Lang.Throwable> (reference.Handle, JniHandleOwnership.DoNotTransfer);
JniObjectReference.Dispose (ref reference, options);
return throwable;
}
JniObjectReference.Dispose (ref reference, options);
var unwrapped = JNIEnvInit.AndroidValueManager?.UnboxException (peeked!);
if (unwrapped != null) {
return unwrapped;
}
return peekedExc;
}
public override void RaisePendingException (Exception pendingException)
{
var je = pendingException as JavaProxyThrowable;
if (je == null) {
je = JavaProxyThrowable.Create (pendingException);
}
var r = new JniObjectReference (je.Handle);
JniEnvironment.Exceptions.Throw (r);
}
}
class AndroidRuntimeOptions : JniRuntime.CreationOptions {
public AndroidRuntimeOptions (IntPtr jnienv,
IntPtr vm,
IntPtr classLoader,
IntPtr classLoader_loadClass,
bool jniAddNativeMethodRegistrationAttributePresent)
{
EnvironmentPointer = jnienv;
ClassLoader = new JniObjectReference (classLoader, JniObjectReferenceType.Global);
ClassLoader_LoadClass_id= classLoader_loadClass;
InvocationPointer = vm;
ObjectReferenceManager = new AndroidObjectReferenceManager ();
TypeManager = new AndroidTypeManager (jniAddNativeMethodRegistrationAttributePresent);
ValueManager = new AndroidValueManager ();
UseMarshalMemberBuilder = false;
JniAddNativeMethodRegistrationAttributePresent = jniAddNativeMethodRegistrationAttributePresent;
}
}
class AndroidObjectReferenceManager : JniRuntime.JniObjectReferenceManager {
public override int GlobalReferenceCount {
get {return RuntimeNativeMethods._monodroid_gref_get ();}
}
public override int WeakGlobalReferenceCount {
get {return RuntimeNativeMethods._monodroid_weak_gref_get ();}
}
public override JniObjectReference CreateLocalReference (JniObjectReference value, ref int localReferenceCount)
{
var r = base.CreateLocalReference (value, ref localReferenceCount);
if (Logger.LogLocalRef) {
var tname = Thread.CurrentThread.Name;
var tid = Thread.CurrentThread.ManagedThreadId;;
var from = new StringBuilder (new StackTrace (true).ToString ());
RuntimeNativeMethods._monodroid_lref_log_new (localReferenceCount, r.Handle, (byte) 'L', tname, tid, from, 1);
}
return r;
}
public override void DeleteLocalReference (ref JniObjectReference value, ref int localReferenceCount)
{
if (Logger.LogLocalRef) {
var tname = Thread.CurrentThread.Name;
var tid = Thread.CurrentThread.ManagedThreadId;;
var from = new StringBuilder (new StackTrace (true).ToString ());
RuntimeNativeMethods._monodroid_lref_log_delete (localReferenceCount-1, value.Handle, (byte) 'L', tname, tid, from, 1);
}
base.DeleteLocalReference (ref value, ref localReferenceCount);
}
public override void CreatedLocalReference (JniObjectReference value, ref int localReferenceCount)
{
base.CreatedLocalReference (value, ref localReferenceCount);
if (Logger.LogLocalRef) {
var tname = Thread.CurrentThread.Name;
var tid = Thread.CurrentThread.ManagedThreadId;;
var from = new StringBuilder (new StackTrace (true).ToString ());
RuntimeNativeMethods._monodroid_lref_log_new (localReferenceCount, value.Handle, (byte) 'L', tname, tid, from, 1);
}
}
public override IntPtr ReleaseLocalReference (ref JniObjectReference value, ref int localReferenceCount)
{
var r = base.ReleaseLocalReference (ref value, ref localReferenceCount);
if (Logger.LogLocalRef) {
var tname = Thread.CurrentThread.Name;
var tid = Thread.CurrentThread.ManagedThreadId;;
var from = new StringBuilder (new StackTrace (true).ToString ());
RuntimeNativeMethods._monodroid_lref_log_delete (localReferenceCount-1, value.Handle, (byte) 'L', tname, tid, from, 1);
}
return r;
}
public override void WriteGlobalReferenceLine (string format, params object?[] args)
{
RuntimeNativeMethods._monodroid_gref_log (string.Format (CultureInfo.InvariantCulture, format, args));
}
public override JniObjectReference CreateGlobalReference (JniObjectReference value)
{
var r = base.CreateGlobalReference (value);
var log = Logger.LogGlobalRef;
var ctype = log ? GetObjectRefType (value.Type) : (byte) '*';
var ntype = log ? GetObjectRefType (r.Type) : (byte) '*';
var tname = log ? Thread.CurrentThread.Name : null;
var tid = log ? Thread.CurrentThread.ManagedThreadId : 0;
var from = log ? new StringBuilder (new StackTrace (true).ToString ()) : null;
int gc = RuntimeNativeMethods._monodroid_gref_log_new (value.Handle, ctype, r.Handle, ntype, tname, tid, from, 1);
if (gc >= JNIEnvInit.gref_gc_threshold) {
Logger.Log (LogLevel.Info, "monodroid-gc", gc + " outstanding GREFs. Performing a full GC!");
System.GC.Collect ();
}
return r;
}
static byte GetObjectRefType (JniObjectReferenceType type)
{
switch (type) {
case JniObjectReferenceType.Invalid: return (byte) 'I';
case JniObjectReferenceType.Local: return (byte) 'L';
case JniObjectReferenceType.Global: return (byte) 'G';
case JniObjectReferenceType.WeakGlobal: return (byte) 'W';
default: return (byte) '*';
}
}
public override void DeleteGlobalReference (ref JniObjectReference value)
{
var log = Logger.LogGlobalRef;
var ctype = log ? GetObjectRefType (value.Type) : (byte) '*';
var tname = log ? Thread.CurrentThread.Name : null;
var tid = log ? Thread.CurrentThread.ManagedThreadId : 0;
var from = log ? new StringBuilder (new StackTrace (true).ToString ()) : null;
RuntimeNativeMethods._monodroid_gref_log_delete (value.Handle, ctype, tname, tid, from, 1);
base.DeleteGlobalReference (ref value);
}
public override JniObjectReference CreateWeakGlobalReference (JniObjectReference value)
{
var r = base.CreateWeakGlobalReference (value);
var log = Logger.LogGlobalRef;
var ctype = log ? GetObjectRefType (value.Type) : (byte) '*';
var ntype = log ? GetObjectRefType (r.Type) : (byte) '*';
var tname = log ? Thread.CurrentThread.Name : null;
var tid = log ? Thread.CurrentThread.ManagedThreadId : 0;
var from = log ? new StringBuilder (new StackTrace (true).ToString ()) : null;
RuntimeNativeMethods._monodroid_weak_gref_new (value.Handle, ctype, r.Handle, ntype, tname, tid, from, 1);
return r;
}
public override void DeleteWeakGlobalReference (ref JniObjectReference value)
{
var log = Logger.LogGlobalRef;
var ctype = log ? GetObjectRefType (value.Type) : (byte) '*';
var tname = log ? Thread.CurrentThread.Name : null;
var tid = log ? Thread.CurrentThread.ManagedThreadId : 0;
var from = log ? new StringBuilder (new StackTrace (true).ToString ()) : null;
RuntimeNativeMethods._monodroid_weak_gref_delete (value.Handle, ctype, tname, tid, from, 1);
base.DeleteWeakGlobalReference (ref value);
}
}
class AndroidTypeManager : JniRuntime.JniTypeManager {
struct JniRemappingReplacementMethod
{
public string target_type;
public string target_name;
public bool is_static;
};
bool jniAddNativeMethodRegistrationAttributePresent;
const DynamicallyAccessedMemberTypes Methods = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
const DynamicallyAccessedMemberTypes MethodsAndPrivateNested = Methods | DynamicallyAccessedMemberTypes.NonPublicNestedTypes;
public AndroidTypeManager (bool jniAddNativeMethodRegistrationAttributePresent)
{
this.jniAddNativeMethodRegistrationAttributePresent = jniAddNativeMethodRegistrationAttributePresent;
}
protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
{
foreach (var ti in base.GetTypesForSimpleReference (jniSimpleReference))
yield return ti;
var t = Java.Interop.TypeManager.GetJavaToManagedType (jniSimpleReference);
if (t != null)
yield return t;
}
protected override string? GetSimpleReference (Type type)
{
string? j = JNIEnv.TypemapManagedToJava (type);
if (j != null) {
return GetReplacementTypeCore (j) ?? j;
}
if (JNIEnvInit.IsRunningOnDesktop) {
return JavaNativeTypeManager.ToJniName (type);
}
return null;
}
protected override IEnumerable<string> GetSimpleReferences (Type type)
{
string? j = JNIEnv.TypemapManagedToJava (type);
j = GetReplacementTypeCore (j) ?? j;
if (JNIEnvInit.IsRunningOnDesktop) {
string? d = JavaNativeTypeManager.ToJniName (type);
if (j != null && d != null) {
return new[]{j, d};
}
if (d != null) {
return new[]{d};
}
}
if (j != null) {
return new[]{j};
}
return Array.Empty<string> ();
}
protected override IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference)
{
ReadOnlySpan<char> name = jniSimpleReference;
int slash = name.LastIndexOf ('/');
var desugarType = new StringBuilder (jniSimpleReference.Length + "Desugar".Length);
if (slash > 0) {
desugarType.Append (name.Slice (0, slash+1))
.Append ("Desugar")
.Append (name.Slice (slash+1));
} else {
desugarType.Append ("Desugar").Append (name);
}
var typeWithPrefix = desugarType.ToString ();
var typeWithSuffix = $"{jniSimpleReference}$-CC";
var replacements = new[]{
GetReplacementTypeCore (typeWithPrefix) ?? typeWithPrefix,
GetReplacementTypeCore (typeWithSuffix) ?? typeWithSuffix,
};
if (Logger.LogAssembly) {
var message = $"Remapping type `{jniSimpleReference}` to one one of {{ `{replacements[0]}`, `{replacements[1]}` }}";
Logger.Log (LogLevel.Debug, "monodroid-assembly", message);
}
return replacements;
}
protected override string? GetReplacementTypeCore (string jniSimpleReference)
{
if (!JNIEnvInit.jniRemappingInUse) {
return null;
}
IntPtr ret = RuntimeNativeMethods._monodroid_lookup_replacement_type (jniSimpleReference);
if (ret == IntPtr.Zero) {
return null;
}
return Marshal.PtrToStringAnsi (ret);
}
protected override JniRuntime.ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSourceType, string jniMethodName, string jniMethodSignature)
{
if (!JNIEnvInit.jniRemappingInUse) {
return null;
}
IntPtr retInfo = RuntimeNativeMethods._monodroid_lookup_replacement_method_info (jniSourceType, jniMethodName, jniMethodSignature);
if (retInfo == IntPtr.Zero) {
return null;
}
var method = new JniRemappingReplacementMethod ();
method = Marshal.PtrToStructure<JniRemappingReplacementMethod>(retInfo);
var newSignature = jniMethodSignature;
int? paramCount = null;
if (method.is_static) {
paramCount = JniMemberSignature.GetParameterCountFromMethodSignature (jniMethodSignature) + 1;
newSignature = $"(L{jniSourceType};" + jniMethodSignature.Substring ("(".Length);
}
if (Logger.LogAssembly) {
var message = $"Remapping method `{jniSourceType}.{jniMethodName}{jniMethodSignature}` to " +
$"`{method.target_type}.{method.target_name}{newSignature}`; " +
$"param-count: {paramCount}; instance-to-static? {method.is_static}";
Logger.Log (LogLevel.Debug, "monodroid-assembly", message);
}
return new JniRuntime.ReplacementMethodInfo {
SourceJniType = jniSourceType,
SourceJniMethodName = jniMethodName,
SourceJniMethodSignature = jniMethodSignature,
TargetJniType = method.target_type,
TargetJniMethodName = method.target_name,
TargetJniMethodSignature = newSignature,
TargetJniMethodParameterCount = paramCount,
TargetJniMethodInstanceToStatic = method.is_static,
};
}
delegate Delegate GetCallbackHandler ();
static MethodInfo? dynamic_callback_gen;
// See ExportAttribute.cs
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "Mono.Android.Export.dll is preserved when [Export] is used via [DynamicDependency].")]
[UnconditionalSuppressMessage ("Trimming", "IL2075", Justification = "Mono.Android.Export.dll is preserved when [Export] is used via [DynamicDependency].")]
static Delegate CreateDynamicCallback (MethodInfo method)
{
if (dynamic_callback_gen == null) {
var assembly = Assembly.Load ("Mono.Android.Export");
if (assembly == null)
throw new InvalidOperationException ("To use methods marked with ExportAttribute, Mono.Android.Export.dll needs to be referenced in the application");
var type = assembly.GetType ("Java.Interop.DynamicCallbackCodeGenerator");
if (type == null)
throw new InvalidOperationException ("The referenced Mono.Android.Export.dll does not match the expected version. The required type was not found.");
dynamic_callback_gen = type.GetMethod ("Create");
if (dynamic_callback_gen == null)
throw new InvalidOperationException ("The referenced Mono.Android.Export.dll does not match the expected version. The required method was not found.");
}
return (Delegate)dynamic_callback_gen.Invoke (null, new object [] { method })!;
}
static List<JniNativeMethodRegistration> sharedRegistrations = new List<JniNativeMethodRegistration> ();
static bool FastRegisterNativeMembers (JniType nativeClass, Type type, ReadOnlySpan<char> methods)
{
if (!MagicRegistrationMap.Filled)
return false;
bool lockTaken = false;
bool rv = false;
try {
Monitor.TryEnter (sharedRegistrations, ref lockTaken);
List<JniNativeMethodRegistration> registrations;
if (lockTaken) {
sharedRegistrations.Clear ();
registrations = sharedRegistrations;
} else {
registrations = new List<JniNativeMethodRegistration> ();
}
JniNativeMethodRegistrationArguments arguments = new JniNativeMethodRegistrationArguments (registrations, methods.ToString ());
rv = MagicRegistrationMap.CallRegisterMethod (arguments, type.FullName!);
if (registrations.Count > 0)
nativeClass.RegisterNativeMethods (registrations.ToArray ());
} finally {
if (lockTaken) {
Monitor.Exit (sharedRegistrations);
}
}
return rv;
}
class MagicRegistrationMap {
#pragma warning disable CS0649 // Field is never assigned to;
// assigned to in generated IL: https://github.com/xamarin/xamarin-android/blob/cbfa7e20acebd37b52ec4de9d5c1a4a66ddda799/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs#L204
static Dictionary<string, int>? typesMap;
#pragma warning restore CS0649
static void Prefill ()
{
// fill code added by the linker
}
static MagicRegistrationMap ()
{
Prefill ();
}
static public bool Filled {
get {
return typesMap != null && typesMap.Count > 0;
}
}
internal static bool CallRegisterMethod (JniNativeMethodRegistrationArguments arguments, string typeName)
{
int idx = 0;
if (typeName == null || !(typesMap?.TryGetValue (typeName, out idx) == true))
return false;
return CallRegisterMethodByIndex (arguments, idx);
}
static bool CallRegisterMethodByIndex (JniNativeMethodRegistrationArguments arguments, int? typeIdx)
{
// updated by the linker to register known types
return false;
}
}
public override void RegisterNativeMembers (
JniType nativeClass,
[DynamicallyAccessedMembers (MethodsAndPrivateNested)]
Type type,
string? methods) =>
RegisterNativeMembers (nativeClass, type, methods.AsSpan ());
[UnconditionalSuppressMessage ("Trimming", "IL2057", Justification = "Type.GetType() can never statically know the string value parsed from parameter 'methods'.")]
[UnconditionalSuppressMessage ("Trimming", "IL2067", Justification = "Delegate.CreateDelegate() can never statically know the string value parsed from parameter 'methods'.")]
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = "Delegate.CreateDelegate() can never statically know the string value parsed from parameter 'methods'.")]
public void RegisterNativeMembers (
JniType nativeClass,
[DynamicallyAccessedMembers (MethodsAndPrivateNested)] Type type,
ReadOnlySpan<char> methods)
{
try {
if (methods.IsEmpty) {
if (jniAddNativeMethodRegistrationAttributePresent)
base.RegisterNativeMembers (nativeClass, type, methods.ToString ());
return;
} else if (FastRegisterNativeMembers (nativeClass, type, methods)) {
return;
}
int methodCount = CountMethods (methods);
if (methodCount < 1) {
if (jniAddNativeMethodRegistrationAttributePresent) {
base.RegisterNativeMembers (nativeClass, type, methods.ToString ());
}
return;
}
JniNativeMethodRegistration [] natives = new JniNativeMethodRegistration [methodCount];
int nativesIndex = 0;
MethodInfo []? typeMethods = null;
ReadOnlySpan<char> methodsSpan = methods;
bool needToRegisterNatives = false;
while (!methodsSpan.IsEmpty) {
int newLineIndex = methodsSpan.IndexOf ('\n');
ReadOnlySpan<char> methodLine = methodsSpan.Slice (0, newLineIndex != -1 ? newLineIndex : methodsSpan.Length);
if (!methodLine.IsEmpty) {
SplitMethodLine (methodLine,
out ReadOnlySpan<char> name,
out ReadOnlySpan<char> signature,
out ReadOnlySpan<char> callbackString,
out ReadOnlySpan<char> callbackDeclaringTypeString);
Delegate? callback = null;
if (callbackString.SequenceEqual ("__export__")) {
var mname = name.Slice (2);
MethodInfo? minfo = null;
typeMethods ??= type.GetMethods (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (var mi in typeMethods)
if (mname.SequenceEqual (mi.Name) && signature.SequenceEqual (JavaNativeTypeManager.GetJniSignature (mi))) {
minfo = mi;
break;
}
if (minfo == null)
throw new InvalidOperationException (FormattableString.Invariant ($"Specified managed method '{mname.ToString ()}' was not found. Signature: {signature.ToString ()}"));
callback = CreateDynamicCallback (minfo);
needToRegisterNatives = true;
} else {
Type callbackDeclaringType = type;
if (!callbackDeclaringTypeString.IsEmpty) {
callbackDeclaringType = Type.GetType (callbackDeclaringTypeString.ToString (), throwOnError: true)!;
}
while (callbackDeclaringType.ContainsGenericParameters) {
callbackDeclaringType = callbackDeclaringType.BaseType!;
}
GetCallbackHandler connector = (GetCallbackHandler) Delegate.CreateDelegate (typeof (GetCallbackHandler),
callbackDeclaringType, callbackString.ToString ());
callback = connector ();
}
if (callback != null) {
needToRegisterNatives = true;
natives [nativesIndex++] = new JniNativeMethodRegistration (name.ToString (), signature.ToString (), callback);
}
}
methodsSpan = newLineIndex != -1 ? methodsSpan.Slice (newLineIndex + 1) : default;
}
if (needToRegisterNatives) {
JniEnvironment.Types.RegisterNatives (nativeClass.PeerReference, natives, nativesIndex);
}
} catch (Exception e) {
JniEnvironment.Runtime.RaisePendingException (e);
}
bool ShouldRegisterDynamically (string callbackTypeName, string callbackString, string typeName, string callbackName)
{
if (String.Compare (typeName, callbackTypeName, StringComparison.Ordinal) != 0) {
return false;
}
return String.Compare (callbackName, callbackString, StringComparison.Ordinal) == 0;
}
}
static int CountMethods (ReadOnlySpan<char> methodsSpan)
{
int count = 0;
while (!methodsSpan.IsEmpty) {
count++;
int newLineIndex = methodsSpan.IndexOf ('\n');
methodsSpan = newLineIndex != -1 ? methodsSpan.Slice (newLineIndex + 1) : default;
}
return count;
}
static void SplitMethodLine (
ReadOnlySpan<char> methodLine,
out ReadOnlySpan<char> name,
out ReadOnlySpan<char> signature,
out ReadOnlySpan<char> callback,
out ReadOnlySpan<char> callbackDeclaringType)
{
int colonIndex = methodLine.IndexOf (':');
name = methodLine.Slice (0, colonIndex);
methodLine = methodLine.Slice (colonIndex + 1);
colonIndex = methodLine.IndexOf (':');
signature = methodLine.Slice (0, colonIndex);
methodLine = methodLine.Slice (colonIndex + 1);
colonIndex = methodLine.IndexOf (':');
callback = methodLine.Slice (0, colonIndex != -1 ? colonIndex : methodLine.Length);
callbackDeclaringType = colonIndex != -1 ? methodLine.Slice (colonIndex + 1) : default;
}
}
class AndroidValueManager : JniRuntime.JniValueManager {
Dictionary<IntPtr, IdentityHashTargets> instances = new Dictionary<IntPtr, IdentityHashTargets> ();
public override void WaitForGCBridgeProcessing ()
{
AndroidRuntimeInternal.WaitForBridgeProcessing ();
}
public override IJavaPeerable? CreatePeer (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type? targetType)
{
if (!reference.IsValid)
return null;
var peer = Java.Interop.TypeManager.CreateInstance (reference.Handle, JniHandleOwnership.DoNotTransfer, targetType) as IJavaPeerable;
JniObjectReference.Dispose (ref reference, options);
return peer;
}
public override void AddPeer (IJavaPeerable value)
{
if (value == null)
throw new ArgumentNullException (nameof (value));
if (!value.PeerReference.IsValid)
throw new ArgumentException ("Must have a valid JNI object reference!", nameof (value));
var reference = value.PeerReference;
var hash = JNIEnv.IdentityHash (reference.Handle);
AddPeer (value, reference, hash);
}
internal void AddPeer (IJavaPeerable value, JniObjectReference reference, IntPtr hash)
{
lock (instances) {
if (!instances.TryGetValue (hash, out var targets)) {
targets = new IdentityHashTargets (value);
instances.Add (hash, targets);
return;
}
bool found = false;
for (int i = 0; i < targets.Count; ++i) {
IJavaPeerable? target;
var wref = targets [i];
if (ShouldReplaceMapping (wref!, reference, out target)) {
found = true;
targets [i] = IdentityHashTargets.CreateWeakReference (value);
break;
}
if (JniEnvironment.Types.IsSameObject (value.PeerReference, target!.PeerReference)) {
found = true;
if (Logger.LogGlobalRef) {
Logger.Log (LogLevel.Info, "monodroid-gref", FormattableString.Invariant (
$"warning: not replacing previous registered handle {target.PeerReference} with handle {reference} for key_handle 0x{hash:x}"));
}
}
}
if (!found) {
targets.Add (value);
}
}
}
internal void AddPeer (IJavaPeerable value, IntPtr handle, JniHandleOwnership transfer, out IntPtr handleField)
{
if (handle == IntPtr.Zero) {
handleField = handle;
return;
}
var transferType = transfer & (JniHandleOwnership.DoNotTransfer | JniHandleOwnership.TransferLocalRef | JniHandleOwnership.TransferGlobalRef);
switch (transferType) {
case JniHandleOwnership.DoNotTransfer:
handleField = JNIEnv.NewGlobalRef (handle);
break;
case JniHandleOwnership.TransferLocalRef:
handleField = JNIEnv.NewGlobalRef (handle);
JNIEnv.DeleteLocalRef (handle);
break;
case JniHandleOwnership.TransferGlobalRef:
handleField = handle;
break;
default:
throw new ArgumentOutOfRangeException ("transfer", transfer,
"Invalid `transfer` value: " + transfer + " on type " + value.GetType ());
}
if (handleField == IntPtr.Zero)
throw new InvalidOperationException ("Unable to allocate Global Reference for object '" + value.ToString () + "'!");
IntPtr hash = JNIEnv.IdentityHash (handleField);
value.SetJniIdentityHashCode ((int) hash);
if ((transfer & JniHandleOwnership.DoNotRegister) == 0) {
AddPeer (value, new JniObjectReference (handleField, JniObjectReferenceType.Global), hash);
}
if (Logger.LogGlobalRef) {
RuntimeNativeMethods._monodroid_gref_log (
FormattableString.Invariant (
$"handle 0x{handleField:x}; key_handle 0x{hash:x}: Java Type: `{JNIEnv.GetClassNameFromInstance (handleField)}`; MCW type: `{value.GetType ().FullName}`\n"));
}
}
bool ShouldReplaceMapping (WeakReference<IJavaPeerable> current, JniObjectReference reference, out IJavaPeerable? target)
{
target = null;
if (current == null)
return true;
// Target has been GC'd; see also FIXME, above, in finalizer
if (!current.TryGetTarget (out target) || target == null)
return true;
// It's possible that the instance was GC'd, but the finalizer
// hasn't executed yet, so the `instances` entry is stale.
if (!target.PeerReference.IsValid)
return true;
if (!JniEnvironment.Types.IsSameObject (target.PeerReference, reference))
return false;
// JNIEnv.NewObject/JNIEnv.CreateInstance() compatibility.
// When two MCW's are created for one Java instance [0],
// we want the 2nd MCW to replace the 1st, as the 2nd is
// the one the dev created; the 1st is an implicit intermediary.
//
// [0]: If Java ctor invokes overridden virtual method, we'll
// transition into managed code w/o a registered instance, and
// thus will create an "intermediary" via
// (IntPtr, JniHandleOwnership) .ctor.
if ((target.JniManagedPeerState & JniManagedPeerStates.Replaceable) == JniManagedPeerStates.Replaceable)
return true;
return false;
}
public override void RemovePeer (IJavaPeerable value)
{
if (value == null)
throw new ArgumentNullException (nameof (value));
var reference = value.PeerReference;
if (!reference.IsValid) {
// Likely an idempotent DIspose(); ignore.
return;
}
var hash = JNIEnv.IdentityHash (reference.Handle);
RemovePeer (value, hash);
}
internal void RemovePeer (IJavaPeerable value, IntPtr hash)
{
lock (instances) {
if (!instances.TryGetValue (hash, out var targets)) {
return;
}
for (int i = targets.Count - 1; i >= 0; i--) {
var wref = targets [i];
if (!wref!.TryGetTarget (out var target)) {
// wref is invalidated; remove it.
targets.RemoveAt (i);
continue;
}
if (!object.ReferenceEquals (target, value)) {
continue;
}
targets.RemoveAt (i);
}
if (targets.Count == 0) {
instances.Remove (hash);
}
}
}
public override IJavaPeerable? PeekPeer (JniObjectReference reference)
{
if (!reference.IsValid)
return null;
var hash = JNIEnv.IdentityHash (reference.Handle);
lock (instances) {
if (instances.TryGetValue (hash, out var targets)) {
for (int i = targets.Count - 1; i >= 0; i--) {
var wref = targets [i];
if (!wref!.TryGetTarget (out var result) || !result.PeerReference.IsValid) {
targets.RemoveAt (i);
continue;
}
if (!JniEnvironment.Types.IsSameObject (reference, result.PeerReference))
continue;
return result;
}
}
}
return null;
}
public override void ActivatePeer (IJavaPeerable? self, JniObjectReference reference, ConstructorInfo cinfo, object? []? argumentValues)
{
Java.Interop.TypeManager.Activate (reference.Handle, cinfo, argumentValues);
}
protected override bool TryUnboxPeerObject (IJavaPeerable value, [NotNullWhen (true)]out object? result)
{
var proxy = value as JavaProxyThrowable;
if (proxy != null) {
result = proxy.InnerException;
return true;
}
return base.TryUnboxPeerObject (value, out result);
}
internal Exception? UnboxException (IJavaPeerable value)
{
object? r;
if (TryUnboxPeerObject (value, out r) && r is Exception e) {
return e;
}
return null;
}
public override void CollectPeers ()
{
GC.Collect ();
}
public override void FinalizePeer (IJavaPeerable value)
{
if (value == null)
throw new ArgumentNullException (nameof (value));
if (Logger.LogGlobalRef) {
RuntimeNativeMethods._monodroid_gref_log ($"Finalizing handle {value.PeerReference}\n");
}
// FIXME: need hash cleanup mechanism.
// Finalization occurs after a test of java persistence. If the
// handle still contains a java reference, we can't finalize the
// object and should "resurrect" it.
if (value.PeerReference.IsValid) {
GC.ReRegisterForFinalize (value);
} else {
RemovePeer (value, (IntPtr) value.JniIdentityHashCode);
value.SetPeerReference (new JniObjectReference ());
value.Finalized ();
}
}
public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()
{
lock (instances) {
var surfacedPeers = new List<JniSurfacedPeerInfo> (instances.Count);
foreach (var e in instances) {
for (int i = 0; i < e.Value.Count; i++) {
var value = e.Value [i];
surfacedPeers.Add (new JniSurfacedPeerInfo (e.Key.ToInt32 (), value!));
}
}
return surfacedPeers;
}
}
}
class InstancesKeyComparer : IEqualityComparer<IntPtr>
{
public bool Equals (IntPtr x, IntPtr y)
{
return x == y;
}
public int GetHashCode (IntPtr value)
{
return value.GetHashCode ();
}
}
class IdentityHashTargets {
WeakReference<IJavaPeerable>? first;
List<WeakReference<IJavaPeerable>?>? rest;
public static WeakReference<IJavaPeerable> CreateWeakReference (IJavaPeerable value)
{
return new WeakReference<IJavaPeerable> (value, trackResurrection: true);
}
public IdentityHashTargets (IJavaPeerable value)
{
first = CreateWeakReference (value);
}
public int Count => (first != null ? 1 : 0) + (rest != null ? rest.Count : 0);
public WeakReference<IJavaPeerable>? this [int index] {
get {
if (index == 0)
return first;
index -= 1;
if (rest == null || index >= rest.Count)
return null;
return rest [index];
}
set {
if (index == 0) {
first = value;
return;
}
index -= 1;
if (rest != null)
rest [index] = value;
}
}
public void Add (IJavaPeerable value)
{
if (first == null) {
first = CreateWeakReference (value);
return;
}
if (rest == null)
rest = new List<WeakReference<IJavaPeerable>?> ();
rest.Add (CreateWeakReference (value));
}
public void RemoveAt (int index)
{
if (index == 0) {
first = null;
if (rest?.Count > 0) {
first = rest [0];
rest.RemoveAt (0);
}
return;
}
index -= 1;
rest?.RemoveAt (index);
}
}
}
#endif // JAVA_INTEROP