-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathBoundInterface.cs
More file actions
292 lines (228 loc) · 10.7 KB
/
BoundInterface.cs
File metadata and controls
292 lines (228 loc) · 10.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MonoDroid.Generation;
using Xamarin.SourceWriter;
using CodeGenerationTarget = Xamarin.Android.Binder.CodeGenerationTarget;
namespace generator.SourceWriters
{
public class BoundInterface : InterfaceWriter
{
readonly List<TypeWriter> pre_sibling_types = new List<TypeWriter> ();
readonly List<ISourceWriter> post_sibling_types = new List<ISourceWriter> ();
readonly bool dont_generate;
public BoundInterface (InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo genInfo)
{
context.ContextTypes.Push (iface);
Name = iface.Name;
AddNestedSiblingTypes (iface, opt, context, genInfo);
AddAlternativesClass (iface, opt, context);
// If this interface is just fields and we can't generate any of them
// then we don't need to write the interface. We still keep this type
// because it may have nested types or need an InterfaceMemberAlternativeClass.
if (iface.IsConstSugar (opt) && iface.GetGeneratableFields (opt).Count () == 0) {
dont_generate = true;
return;
}
IsPartial = true;
UsePriorityOrder = true;
SetVisibility (iface.Visibility);
iface.JavadocInfo?.AddJavadocs (Comments);
Comments.Add ($"// Metadata.xml XPath interface reference: path=\"{iface.MetadataXPathReference}\"");
SourceWriterExtensions.AddObsolete (Attributes, iface.DeprecatedComment, iface.IsDeprecated);
if (!iface.IsConstSugar (opt)) {
var signature = string.IsNullOrWhiteSpace (iface.Namespace)
? iface.FullName.Replace ('.', '/')
: iface.Namespace + "." + iface.FullName.Substring (iface.Namespace.Length + 1).Replace ('.', '/');
var noAcw = false;
var memberType = (MemberTypes?) null;
if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1) {
noAcw = true;
memberType = MemberTypes.TypeInfo;
}
Attributes.Add (new RegisterAttr (iface.RawJniName, string.Empty, signature + "Invoker", noAcw, additionalProperties: iface.AdditionalAttributeString ()) {
MemberType = memberType,
});
}
if (iface.TypeParameters != null && iface.TypeParameters.Any ())
Attributes.Add (new CustomAttr (iface.TypeParameters.ToGeneratedAttributeString ()));
AddInheritedInterfaces (iface, opt);
AddClassHandle (iface, opt);
AddFields (iface, opt, context);
AddProperties (iface, opt);
AddMethods (iface, opt);
AddNestedTypes (iface, opt, context, genInfo);
// If this interface is just constant fields we don't need to add all the invoker bits
if (iface.IsConstSugar (opt))
return;
if (!iface.AssemblyQualifiedName.Contains ('/')) {
if (iface.Methods.Any (m => m.CanHaveStringOverload) || iface.Methods.Any (m => m.Asyncify))
post_sibling_types.Add (new InterfaceExtensionsClass (iface, null, opt));
}
if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1) {
// Worry about later; https://github.com/xamarin/java.interop/issues/910
post_sibling_types.Add (new InterfaceInvokerClass (iface, opt, context));
}
AddInterfaceEventHandler (iface, opt, context);
context.ContextTypes.Pop ();
}
void AddNestedSiblingTypes (InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo genInfo)
{
// Generate sibling types for nested types we don't want to nest
foreach (var nest in iface.NestedTypes.Where (t => t.Unnest))
pre_sibling_types.Add (SourceWriterExtensions.BuildManagedTypeModel (nest, opt, context, genInfo));
}
void AddAlternativesClass (InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context)
{
if (iface.NoAlternatives)
return;
var staticMethods = iface.Methods.Where (m => m.IsStatic);
if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1 &&
(iface.Fields.Any () || staticMethods.Any ())) {
pre_sibling_types.Add (new InterfaceMemberAlternativeClass (iface, opt, context));
}
}
void AddInterfaceEventHandler (InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context)
{
if (!iface.IsListener)
return;
foreach (var method in iface.Methods.Where (m => m.EventName != string.Empty)) {
if (method.RetVal.IsVoid || method.IsEventHandlerWithHandledProperty) {
if (!method.IsSimpleEventHandler || method.IsEventHandlerWithHandledProperty) {
var event_args_class = post_sibling_types.OfType<InterfaceEventArgsClass> ().SingleOrDefault (c => c.Name == iface.GetArgsName (method));
// Check if there's an existing EventArgs class to add to
if (event_args_class is null) {
event_args_class = new InterfaceEventArgsClass (iface, method);
post_sibling_types.Add (event_args_class);
}
event_args_class.AddMembersFromMethod (iface, method, opt);
}
} else {
var del = new DelegateWriter {
Name = iface.GetEventDelegateName (method),
Type = new TypeReferenceWriter (opt.GetTypeReferenceName (method.RetVal)),
IsPublic = true
};
SourceWriterExtensions.AddMethodParameters (del, method.Parameters, opt);
post_sibling_types.Add (del);
}
}
post_sibling_types.Add (new InterfaceEventHandlerImplClass (iface, opt, context));
}
void AddInheritedInterfaces (InterfaceGen iface, CodeGenerationOptions opt)
{
foreach (var isym in iface.Interfaces) {
var igen = (isym is GenericSymbol ? (isym as GenericSymbol).Gen : isym) as InterfaceGen;
// igen *should not* be null here because we *should* only be inheriting interfaces, but
// in the case of constants on that interface we create a C# *class* that is registered for the
// Java *interface*. Thus when we do type resolution, we are actually pointed to the
// Foo abstract class instead of the IFoo interface.
if (igen is null || igen.IsConstSugar (opt) || igen.RawVisibility != "public")
continue;
Implements.Add (opt.GetOutputName (isym.FullName));
}
if (Implements.Count == 0 && !iface.IsConstSugar (opt)) {
if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1) {
Implements.Add ("IJavaObject");
}
Implements.Add ("IJavaPeerable");
}
}
void AddClassHandle (InterfaceGen iface, CodeGenerationOptions opt)
{
if (opt.SupportDefaultInterfaceMethods && (iface.HasDefaultMethods || iface.HasStaticMethods || iface.HasFieldsAsProperties))
Fields.Add (new PeerMembersField (opt, iface.RawJniName, iface.Name, true));
}
void AddFields (InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context)
{
// Interface fields are only supported with DIM
if (!opt.SupportInterfaceConstants && !opt.SupportDefaultInterfaceMethods)
return;
var seen = new HashSet<string> ();
var fields = iface.GetGeneratableFields (opt).ToList ();
SourceWriterExtensions.AddFields (this, iface, fields, seen, opt, context);
}
void AddProperties (InterfaceGen iface, CodeGenerationOptions opt)
{
foreach (var prop in iface.Properties.Where (p => !p.Getter.IsStatic && !p.Getter.IsInterfaceDefaultMethod))
Properties.Add (new BoundInterfacePropertyDeclaration (iface, prop, iface.AssemblyQualifiedName + "Invoker", opt));
if (!opt.SupportDefaultInterfaceMethods)
return;
var dim_properties = iface.Properties.Where (p => p.Getter.IsInterfaceDefaultMethod || p.Getter.IsStatic);
foreach (var prop in dim_properties) {
if (prop.Getter.IsAbstract) {
var baseProp = iface.BaseSymbol != null ? iface.BaseSymbol.GetPropertyByName (prop.Name, true) : null;
if (baseProp != null) {
if (baseProp.Type != prop.Getter.Return) {
// This may not be required if we can change generic parameter support to return constrained type (not just J.L.Object).
//writer.WriteLine ("{0}// skipped generating property {1} because its Java method declaration is variant that we cannot represent in C#", indent, property.Name);
return;
}
}
var bound_property = new BoundAbstractProperty (iface, prop, opt);
Properties.Add (bound_property);
if (prop.Type.StartsWith ("Java.Lang.ICharSequence", StringComparison.Ordinal) && !bound_property.IsOverride)
Properties.Add (new BoundPropertyStringVariant (prop, opt));
} else {
var bound_property = new BoundProperty (iface, prop, opt, true, false);
Properties.Add (bound_property);
if (prop.Type.StartsWith ("Java.Lang.ICharSequence", StringComparison.Ordinal) && !bound_property.IsOverride)
Properties.Add (new BoundPropertyStringVariant (prop, opt));
}
}
}
void AddMethods (InterfaceGen iface, CodeGenerationOptions opt)
{
foreach (var m in iface.Methods.Where (m => !m.IsStatic && !m.IsInterfaceDefaultMethod)) {
if (m.Name == iface.Name || iface.ContainsProperty (m.Name, true))
m.Name = "Invoke" + m.Name;
Methods.Add (new BoundInterfaceMethodDeclaration (m, iface.AssemblyQualifiedName + "Invoker", opt));
}
if (!opt.SupportDefaultInterfaceMethods)
return;
foreach (var method in iface.Methods.Where (m => m.IsInterfaceDefaultMethod || m.IsStatic)) {
if (!method.IsValid)
continue;
Methods.Add (new BoundMethod (iface, method, opt, true));
var name_and_jnisig = method.JavaName + method.JniSignature.Replace ("java/lang/CharSequence", "java/lang/String");
var gen_string_overload = !method.IsOverride && method.Parameters.HasCharSequence && !iface.ContainsMethod (name_and_jnisig);
if (gen_string_overload || method.IsReturnCharSequence)
Methods.Add (new BoundMethodStringOverload (method, opt));
if (method.Asyncify)
Methods.Add (new MethodAsyncWrapper (method, opt));
}
}
void AddNestedTypes (InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo genInfo)
{
// Generate nested types for supported nested types. This is a new addition in C#8.
// Prior to this, types nested in an interface had to be generated as sibling types.
// The "Unnest" property is used to support backwards compatibility with pre-C#8 bindings.
foreach (var nest in iface.NestedTypes.Where (t => !t.Unnest))
NestedTypes.Add (SourceWriterExtensions.BuildManagedTypeModel (nest, opt, context, genInfo));
}
public override void Write (CodeWriter writer)
{
WritePreSiblingClasses (writer);
if (!dont_generate)
base.Write (writer);
WritePostSiblingClasses (writer);
}
public void WritePreSiblingClasses (CodeWriter writer)
{
foreach (var sibling in pre_sibling_types) {
sibling.Write (writer);
writer.WriteLine ();
}
}
public void WritePostSiblingClasses (CodeWriter writer)
{
foreach (var sibling in post_sibling_types) {
writer.WriteLine ();
sibling.Write (writer);
}
}
}
}