-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathManagedTypeManager.cs
More file actions
209 lines (175 loc) · 8.47 KB
/
Copy pathManagedTypeManager.cs
File metadata and controls
209 lines (175 loc) · 8.47 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Java.Interop;
using Java.Interop.Tools.TypeNameMappings;
namespace Microsoft.Android.Runtime;
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "Temporary suppression for Java.Interop reflection manager base.")]
class ManagedTypeManager : JniRuntime.ReflectionJniTypeManager {
const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
internal const DynamicallyAccessedMemberTypes Methods = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
internal const DynamicallyAccessedMemberTypes MethodsAndPrivateNested = Methods | DynamicallyAccessedMemberTypes.NonPublicNestedTypes;
internal const DynamicallyAccessedMemberTypes MethodsConstructors = MethodsAndPrivateNested | Constructors;
public ManagedTypeManager ()
{
}
[return: DynamicallyAccessedMembers (Constructors)]
protected override Type? GetInvokerTypeCore (
[DynamicallyAccessedMembers (Constructors)]
Type type)
{
const string suffix = "Invoker";
// https://github.com/xamarin/xamarin-android/blob/5472eec991cc075e4b0c09cd98a2331fb93aa0f3/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs#L176-L186
const string assemblyGetTypeMessage = "'Invoker' types are preserved by the MarkJavaObjects trimmer step.";
const string makeGenericTypeMessage = "Generic 'Invoker' types are preserved by the MarkJavaObjects trimmer step.";
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = assemblyGetTypeMessage)]
[UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = assemblyGetTypeMessage)]
[return: DynamicallyAccessedMembers (Constructors)]
static Type? AssemblyGetType (Assembly assembly, string typeName) =>
assembly.GetType (typeName);
[UnconditionalSuppressMessage ("Trimming", "IL2055", Justification = makeGenericTypeMessage)]
[return: DynamicallyAccessedMembers (Constructors)]
static Type MakeGenericType (
[DynamicallyAccessedMembers (Constructors)]
Type type,
Type [] arguments) =>
// FIXME: https://github.com/dotnet/java-interop/issues/1192
#pragma warning disable IL3050
type.MakeGenericType (arguments);
#pragma warning restore IL3050
Type[] arguments = type.GetGenericArguments ();
if (arguments.Length == 0)
return AssemblyGetType (type.Assembly, type + suffix) ?? base.GetInvokerTypeCore (type);
Type definition = type.GetGenericTypeDefinition ();
int bt = definition.FullName!.IndexOf ("`", StringComparison.Ordinal);
if (bt == -1)
throw new NotSupportedException ("Generic type doesn't follow generic type naming convention! " + type.FullName);
Type? suffixDefinition = AssemblyGetType (definition.Assembly,
definition.FullName.Substring (0, bt) + suffix + definition.FullName.Substring (bt));
if (suffixDefinition == null)
return base.GetInvokerTypeCore (type);
return MakeGenericType (suffixDefinition, arguments);
}
// NOTE: suppressions below also in `src/Mono.Android/Android.Runtime/AndroidRuntime.cs`
[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'.")]
[UnconditionalSuppressMessage ("AOT", "IL3050", Justification = "JniNativeMethodRegistration[] registration path will be migrated to the blittable RegisterNatives overload in a future change.")]
public override void RegisterNativeMembers (
JniType nativeClass,
[DynamicallyAccessedMembers (MethodsAndPrivateNested)]
Type type,
ReadOnlySpan<char> methods)
{
if (methods.IsEmpty) {
base.RegisterNativeMembers (nativeClass, type, methods);
return;
}
int methodCount = CountMethods (methods);
if (methodCount < 1) {
base.RegisterNativeMembers (nativeClass, type, methods);
return;
}
JniNativeMethodRegistration [] natives = new JniNativeMethodRegistration [methodCount];
int nativesIndex = 0;
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__")) {
throw new InvalidOperationException (FormattableString.Invariant ($"Methods such as {callbackString.ToString ()} are not implemented!"));
} 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);
}
}
protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpleReference)
{
// Base class contains built-in mappings (e.g. java/lang/String → System.String)
// which must take priority over ManagedTypeMapping (which would return Java.Lang.String).
foreach (var t in base.GetTypesForSimpleReference (jniSimpleReference)) {
yield return t;
}
if (ManagedTypeMapping.TryGetType (jniSimpleReference, out var target)) {
yield return target;
}
}
[UnconditionalSuppressMessage ("Trimming", "IL2068", Justification = "Temporary suppression until ManagedTypeMapping type entries carry DAM annotations.")]
[return: DynamicallyAccessedMembers (MethodsConstructors)]
protected override Type? GetTypeForSimpleReference (string jniSimpleReference)
{
var type = base.GetTypeForSimpleReference (jniSimpleReference);
if (type != null) {
return type;
}
return ManagedTypeMapping.TryGetType (jniSimpleReference, out var target) ? target : null;
}
protected override IEnumerable<string> GetSimpleReferences (Type type)
{
foreach (var r in base.GetSimpleReferences (type)) {
yield return r;
}
if (ManagedTypeMapping.TryGetJniName (type, out var jniName)) {
yield return jniName;
}
}
protected override IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference)
{
return JniRemappingLookup.GetStaticMethodFallbackTypes (jniSimpleReference, useReplacementTypes: false);
}
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;
}
delegate Delegate GetCallbackHandler ();
}