Skip to content

Commit 16e9711

Browse files
committed
Add CallableWrapperReaderOptions.
1 parent 72c7b50 commit 16e9711

File tree

5 files changed

+74
-57
lines changed

5 files changed

+74
-57
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.Adapters/CecilImporter.cs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ namespace Java.Interop.Tools.JavaCallableWrappers.Adapters;
1414

1515
public class CecilImporter
1616
{
17-
public static CallableWrapperType CreateType (TypeDefinition type, IMetadataResolver resolver, string? outerType = null, JavaCallableMethodClassifier? methodClassifier = null)
17+
// Don't expose internal "outerType" parameter to the public API
18+
public static CallableWrapperType CreateType (TypeDefinition type, IMetadataResolver resolver, CallableWrapperReaderOptions? options = null)
19+
=> CreateType (type, resolver, options, null);
20+
21+
static CallableWrapperType CreateType (TypeDefinition type, IMetadataResolver resolver, CallableWrapperReaderOptions? options = null, string? outerType = null)
1822
{
1923
if (type.IsEnum || type.IsInterface || type.IsValueType)
2024
Diagnostic.Error (4200, CecilExtensions.LookupSource (type), Localization.Resources.JavaCallableWrappers_XA4200, type.FullName);
@@ -31,6 +35,8 @@ public static CallableWrapperType CreateType (TypeDefinition type, IMetadataReso
3135

3236
ExtractJavaNames (jniName, out var package, out var name);
3337

38+
options ??= new CallableWrapperReaderOptions ();
39+
3440
if (string.IsNullOrEmpty (package) &&
3541
(type.IsSubclassOf ("Android.App.Activity", resolver) ||
3642
type.IsSubclassOf ("Android.App.Application", resolver) ||
@@ -43,6 +49,9 @@ public static CallableWrapperType CreateType (TypeDefinition type, IMetadataReso
4349
IsApplication = JavaNativeTypeManager.IsApplication (type, resolver),
4450
IsInstrumentation = JavaNativeTypeManager.IsInstrumentation (type, resolver),
4551
IsAbstract = type.IsAbstract,
52+
ApplicationJavaClass = options.DefaultApplicationJavaClass,
53+
GenerateOnCreateOverrides = options.DefaultGenerateOnCreateOverrides,
54+
MonoRuntimeInitialization = options.DefaultMonoRuntimeInitialization,
4655
};
4756

4857
// Type annotations
@@ -70,18 +79,18 @@ public static CallableWrapperType CreateType (TypeDefinition type, IMetadataReso
7079
var baseRegisteredMethod = CecilExtensions.GetBaseRegisteredMethod (minfo, resolver);
7180

7281
if (baseRegisteredMethod is not null)
73-
AddMethod (cwt, type, baseRegisteredMethod, minfo, methodClassifier, resolver);
82+
AddMethod (cwt, type, baseRegisteredMethod, minfo, options.MethodClassifier, resolver);
7483
else if (minfo.AnyCustomAttributes ("Java.Interop.JavaCallableAttribute")) {
75-
AddMethod (cwt, type, null, minfo, methodClassifier, resolver);
84+
AddMethod (cwt, type, null, minfo, options.MethodClassifier, resolver);
7685
cwt.HasExport = true;
7786
} else if (minfo.AnyCustomAttributes ("Java.Interop.JavaCallableConstructorAttribute")) {
78-
AddMethod (cwt, type, null, minfo, methodClassifier, resolver);
87+
AddMethod (cwt, type, null, minfo, options.MethodClassifier, resolver);
7988
cwt.HasExport = true;
8089
} else if (minfo.AnyCustomAttributes (typeof (ExportFieldAttribute))) {
81-
AddMethod (cwt, type, null, minfo, methodClassifier, resolver);
90+
AddMethod (cwt, type, null, minfo, options.MethodClassifier, resolver);
8291
cwt.HasExport = true;
8392
} else if (minfo.AnyCustomAttributes (typeof (ExportAttribute))) {
84-
AddMethod (cwt, type, null, minfo, methodClassifier, resolver);
93+
AddMethod (cwt, type, null, minfo, options.MethodClassifier, resolver);
8594
cwt.HasExport = true;
8695
}
8796
}
@@ -106,7 +115,7 @@ public static CallableWrapperType CreateType (TypeDefinition type, IMetadataReso
106115
if (imethod.IsStatic)
107116
continue;
108117

109-
AddMethod (cwt, type, imethod, imethod, methodClassifier, resolver);
118+
AddMethod (cwt, type, imethod, imethod, options.MethodClassifier, resolver);
110119
}
111120
}
112121

@@ -148,7 +157,7 @@ public static CallableWrapperType CreateType (TypeDefinition type, IMetadataReso
148157
AddConstructors (cwt, ctorTypes [i], type, outerType, baseCtors, curCtors, false, resolver);
149158
}
150159

151-
AddNestedTypes (cwt, type, resolver, methodClassifier);
160+
AddNestedTypes (cwt, type, resolver, options);
152161

153162
return cwt;
154163
}
@@ -201,21 +210,19 @@ static CallableWrapperConstructor CreateConstructor (string name, CallableWrappe
201210
signature = signature ?? throw new ArgumentNullException ("`connector` cannot be null.", nameof (connector));
202211
var method_name = "n_" + name + ":" + signature + ":" + connector;
203212

204-
var method = new CallableWrapperConstructor (name, method_name, signature);
213+
var method = new CallableWrapperConstructor (type, name, method_name, signature);
205214

206215
PopulateMethod (method, signature, managedParameters, outerType, superCall);
207216

208217
method.Name = type.Name;
209-
method.CannotRegisterInStaticConstructor = type.CannotRegisterInStaticConstructor;
210-
method.PartialAssemblyQualifiedName = type.PartialAssemblyQualifiedName;
211218

212219
return method;
213220
}
214221

215222
// Method with a [Register] attribute
216-
static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, RegisterAttribute register, string? managedParameters, string? outerType, IMetadataResolver resolver, bool shouldBeDynamicallyRegistered = true)
223+
static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, CallableWrapperType declaringType, RegisterAttribute register, string? managedParameters, string? outerType, IMetadataResolver resolver, bool shouldBeDynamicallyRegistered = true)
217224
{
218-
var method = CreateMethod (register.Name, register.Signature, register.Connector, managedParameters, outerType, null);
225+
var method = CreateMethod (register.Name, declaringType, register.Signature, register.Connector, managedParameters, outerType, null);
219226

220227
method.Annotations.AddRange (CreateAnnotations (methodDefinition, resolver));
221228
method.IsDynamicallyRegistered = shouldBeDynamicallyRegistered;
@@ -224,9 +231,9 @@ static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, Re
224231
}
225232

226233
// Method with an [Export] attribute
227-
static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, ExportAttribute export, string? managedParameters, IMetadataResolver resolver)
234+
static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, CallableWrapperType declaringType, ExportAttribute export, string? managedParameters, IMetadataResolver resolver)
228235
{
229-
var method = CreateMethod (methodDefinition.Name, JavaNativeTypeManager.GetJniSignature (methodDefinition, resolver), "__export__", null, null, export.SuperArgumentsString);
236+
var method = CreateMethod (methodDefinition.Name, declaringType, JavaNativeTypeManager.GetJniSignature (methodDefinition, resolver), "__export__", null, null, export.SuperArgumentsString);
230237

231238
method.IsExport = true;
232239
method.IsStatic = methodDefinition.IsStatic;
@@ -240,9 +247,9 @@ static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, Ex
240247
}
241248

242249
// Method with an [ExportField] attribute
243-
static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, IMetadataResolver resolver)
250+
static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, CallableWrapperType declaringType, IMetadataResolver resolver)
244251
{
245-
var method = CreateMethod (methodDefinition.Name, JavaNativeTypeManager.GetJniSignature (methodDefinition, resolver), "__export__", null, null, null);
252+
var method = CreateMethod (methodDefinition.Name, declaringType, JavaNativeTypeManager.GetJniSignature (methodDefinition, resolver), "__export__", null, null, null);
246253

247254
if (methodDefinition.HasParameters)
248255
Diagnostic.Error (4205, CecilExtensions.LookupSource (methodDefinition), Localization.Resources.JavaCallableWrappers_XA4205);
@@ -259,12 +266,12 @@ static CallableWrapperMethod CreateMethod (MethodDefinition methodDefinition, IM
259266
}
260267

261268
// Common method creation code
262-
static CallableWrapperMethod CreateMethod (string name, string? signature, string? connector, string? managedParameters, string? outerType, string? superCall)
269+
static CallableWrapperMethod CreateMethod (string name, CallableWrapperType declaringType, string? signature, string? connector, string? managedParameters, string? outerType, string? superCall)
263270
{
264271
signature = signature ?? throw new ArgumentNullException ("`connector` cannot be null.", nameof (connector));
265272
var method_name = "n_" + name + ":" + signature + ":" + connector;
266273

267-
var method = new CallableWrapperMethod (name, method_name, signature);
274+
var method = new CallableWrapperMethod (declaringType, name, method_name, signature);
268275

269276
PopulateMethod (method, signature, managedParameters, outerType, superCall);
270277

@@ -320,23 +327,23 @@ static void PopulateMethod (CallableWrapperMethod method, string signature, stri
320327
method.ActivateCall = acall.ToString ();
321328
}
322329

323-
static void AddConstructors (CallableWrapperType cwt, TypeDefinition type, TypeDefinition rootType, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> curCtors, bool onlyRegisteredOrExportedCtors, IMetadataResolver cache)
330+
static void AddConstructors (CallableWrapperType declaringType, TypeDefinition type, TypeDefinition rootType, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> curCtors, bool onlyRegisteredOrExportedCtors, IMetadataResolver cache)
324331
{
325332
foreach (var ctor in type.Methods)
326333
if (ctor.IsConstructor && !ctor.IsStatic && !ctor.AnyCustomAttributes (typeof (ExportAttribute)))
327-
if (CreateConstructor (cwt, ctor, type, rootType, outerType, baseCtors, curCtors, onlyRegisteredOrExportedCtors, false, cache) is CallableWrapperConstructor c)
328-
cwt.Constructors.Add (c);
334+
if (CreateConstructor (declaringType, ctor, type, rootType, outerType, baseCtors, curCtors, onlyRegisteredOrExportedCtors, false, cache) is CallableWrapperConstructor c)
335+
declaringType.Constructors.Add (c);
329336
}
330337

331-
static CallableWrapperConstructor? CreateConstructor (CallableWrapperType cwt, MethodDefinition ctor, TypeDefinition type, TypeDefinition rootType, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> curCtors, bool onlyRegisteredOrExportedCtors, bool skipParameterCheck, IMetadataResolver cache)
338+
static CallableWrapperConstructor? CreateConstructor (CallableWrapperType declaringType, MethodDefinition ctor, TypeDefinition type, TypeDefinition rootType, string? outerType, List<MethodDefinition>? baseCtors, List<MethodDefinition> curCtors, bool onlyRegisteredOrExportedCtors, bool skipParameterCheck, IMetadataResolver cache)
332339
{
333340
// We create a parameter-less constructor for the application class, so don't use the imported one
334341
if (!ctor.HasParameters && JavaNativeTypeManager.IsApplication (rootType, cache))
335342
return null;
336343

337344
var managedParameters = GetManagedParameters (ctor, outerType, type, cache);
338345

339-
if (!skipParameterCheck && (managedParameters == null || cwt.Constructors.Any (c => c.ManagedParameters == managedParameters)))
346+
if (!skipParameterCheck && (managedParameters == null || declaringType.Constructors.Any (c => c.ManagedParameters == managedParameters)))
340347
return null;
341348

342349
// Constructor with [Export] attribute
@@ -348,18 +355,18 @@ static void AddConstructors (CallableWrapperType cwt, TypeDefinition type, TypeD
348355
}
349356

350357
curCtors.Add (ctor);
351-
return CreateConstructor (ctor, cwt, eattr, managedParameters, cache);
358+
return CreateConstructor (ctor, declaringType, eattr, managedParameters, cache);
352359
}
353360

354361
// Constructor with [Register] attribute
355362
var rattr = CecilExtensions.GetMethodRegistrationAttributes (ctor).FirstOrDefault ();
356363

357364
if (rattr != null) {
358-
if (cwt.Constructors.Any (c => c.JniSignature == rattr.Signature))
365+
if (declaringType.Constructors.Any (c => c.JniSignature == rattr.Signature))
359366
return null;
360367

361368
curCtors.Add (ctor);
362-
return CreateConstructor (ctor, cwt, rattr, managedParameters, outerType, cache);
369+
return CreateConstructor (ctor, declaringType, rattr, managedParameters, outerType, cache);
363370
}
364371

365372
if (onlyRegisteredOrExportedCtors)
@@ -371,20 +378,20 @@ static void AddConstructors (CallableWrapperType cwt, TypeDefinition type, TypeD
371378
if (jniSignature is null)
372379
return null;
373380

374-
if (cwt.Constructors.Any (c => c.JniSignature == jniSignature))
381+
if (declaringType.Constructors.Any (c => c.JniSignature == jniSignature))
375382
return null;
376383

377384
if (baseCtors is null)
378385
throw new InvalidOperationException ("`baseCtors` should not be null!");
379386

380387
if (baseCtors.Any (m => m.Parameters.AreParametersCompatibleWith (ctor.Parameters, cache))) {
381388
curCtors.Add (ctor);
382-
return CreateConstructor (".ctor", cwt, jniSignature, "", managedParameters, outerType, null);
389+
return CreateConstructor (".ctor", declaringType, jniSignature, "", managedParameters, outerType, null);
383390
}
384391

385392
if (baseCtors.Any (m => !m.HasParameters)) {
386393
curCtors.Add (ctor);
387-
return CreateConstructor (".ctor", cwt, jniSignature, "", managedParameters, outerType, "");
394+
return CreateConstructor (".ctor", declaringType, jniSignature, "", managedParameters, outerType, "");
388395
}
389396

390397
return null;
@@ -414,7 +421,7 @@ static string GetManagedParameters (MethodDefinition ctor, string? outerType, Ty
414421
return new CallableWrapperApplicationConstructor (name);
415422
}
416423

417-
static void AddNestedTypes (CallableWrapperType cwt, TypeDefinition type, IMetadataResolver cache,JavaCallableMethodClassifier? methodClassifier)
424+
static void AddNestedTypes (CallableWrapperType declaringType, TypeDefinition type, IMetadataResolver cache, CallableWrapperReaderOptions? options)
418425
{
419426
if (!type.HasNestedTypes)
420427
return;
@@ -425,14 +432,14 @@ static void AddNestedTypes (CallableWrapperType cwt, TypeDefinition type, IMetad
425432
if (!JavaNativeTypeManager.IsNonStaticInnerClass (nt, cache))
426433
continue;
427434

428-
cwt.NestedTypes.Add (CreateType (nt, cache, JavaNativeTypeManager.ToJniName (type, cache), methodClassifier));
429-
AddNestedTypes (cwt, nt, cache, methodClassifier);
435+
declaringType.NestedTypes.Add (CreateType (nt, cache, options, JavaNativeTypeManager.ToJniName (type, cache)));
436+
AddNestedTypes (declaringType, nt, cache, options);
430437
}
431438

432-
cwt.HasExport |= cwt.NestedTypes.Any (t => t.HasExport);
439+
declaringType.HasExport |= declaringType.NestedTypes.Any (t => t.HasExport);
433440
}
434441

435-
static void AddMethod (CallableWrapperType cwt, TypeDefinition type, MethodDefinition? registeredMethod, MethodDefinition implementedMethod, JavaCallableMethodClassifier? methodClassifier, IMetadataResolver cache)
442+
static void AddMethod (CallableWrapperType declaringType, TypeDefinition type, MethodDefinition? registeredMethod, MethodDefinition implementedMethod, JavaCallableMethodClassifier? methodClassifier, IMetadataResolver cache)
436443
{
437444
if (registeredMethod != null)
438445
foreach (RegisterAttribute attr in CecilExtensions.GetMethodRegistrationAttributes (registeredMethod)) {
@@ -441,33 +448,33 @@ static void AddMethod (CallableWrapperType cwt, TypeDefinition type, MethodDefin
441448
Diagnostic.Error (4217, CecilExtensions.LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4217, attr.Name);
442449

443450
var shouldBeDynamicallyRegistered = methodClassifier?.ShouldBeDynamicallyRegistered (type, registeredMethod, implementedMethod, attr.OriginAttribute) ?? true;
444-
var method = CreateMethod (implementedMethod, attr, null, null, cache, shouldBeDynamicallyRegistered);
451+
var method = CreateMethod (implementedMethod, declaringType, attr, null, null, cache, shouldBeDynamicallyRegistered);
445452

446-
if (!registeredMethod.IsConstructor && !cwt.Methods.Any (m => m.Name == method.Name && m.Params == method.Params))
447-
cwt.Methods.Add (method);
453+
if (!registeredMethod.IsConstructor && !declaringType.Methods.Any (m => m.Name == method.Name && m.Params == method.Params))
454+
declaringType.Methods.Add (method);
448455
}
449456
foreach (ExportAttribute attr in CecilExtensions.GetExportAttributes (implementedMethod, cache)) {
450457
if (type.HasGenericParameters)
451458
Diagnostic.Error (4206, CecilExtensions.LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4206);
452459

453-
var method = CreateMethod (implementedMethod, attr, null, cache);
460+
var method = CreateMethod (implementedMethod, declaringType, attr, null, cache);
454461

455462
if (!string.IsNullOrEmpty (attr.SuperArgumentsString)) {
456463
// Diagnostic.Warning (log, "Use of ExportAttribute.SuperArgumentsString property is invalid on methods");
457464
}
458465

459-
if (!implementedMethod.IsConstructor && !cwt.Methods.Any (m => m.Name == method.Name && m.Params == method.Params))
460-
cwt.Methods.Add (method);
466+
if (!implementedMethod.IsConstructor && !declaringType.Methods.Any (m => m.Name == method.Name && m.Params == method.Params))
467+
declaringType.Methods.Add (method);
461468
}
462469
foreach (ExportFieldAttribute attr in CecilExtensions.GetExportFieldAttributes (implementedMethod)) {
463470
if (type.HasGenericParameters)
464471
Diagnostic.Error (4207, CecilExtensions.LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4207);
465472

466-
var method = CreateMethod (implementedMethod, cache);
473+
var method = CreateMethod (implementedMethod, declaringType, cache);
467474

468-
if (!implementedMethod.IsConstructor && !cwt.Methods.Any (m => m.Name == method.Name && m.Params == method.Params)) {
469-
cwt.Methods.Add (method);
470-
cwt.Fields.Add (CreateField (implementedMethod, attr.Name, cache));
475+
if (!implementedMethod.IsConstructor && !declaringType.Methods.Any (m => m.Name == method.Name && m.Params == method.Params)) {
476+
declaringType.Methods.Add (method);
477+
declaringType.Fields.Add (CreateField (implementedMethod, attr.Name, cache));
471478
}
472479
}
473480
}

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.CallableWrapperMembers/CallableWrapperConstructor.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ namespace Java.Interop.Tools.JavaCallableWrappers.CallableWrapperMembers;
44

55
public class CallableWrapperConstructor : CallableWrapperMethod
66
{
7-
public bool CannotRegisterInStaticConstructor { get; set; }
8-
public string? PartialAssemblyQualifiedName { get; set; }
9-
10-
public CallableWrapperConstructor (string name, string method, string jniSignature) : base (name, method, jniSignature)
7+
public CallableWrapperConstructor (CallableWrapperType declaringType, string name, string method, string jniSignature) : base (declaringType, name, method, jniSignature)
118
{
129
}
1310

@@ -40,7 +37,7 @@ public override void Generate (TextWriter sw, CallableWrapperWriterOptions optio
4037
sw.WriteLine ("\t\tandroid.util.Log.i(\"MonoDroid-Timing\", \"{0}..ctor({1}): time: \"+java.lang.System.currentTimeMillis());", Name, Params);
4138
#endif
4239

43-
if (!CannotRegisterInStaticConstructor) {
40+
if (!DeclaringType.CannotRegisterInStaticConstructor) {
4441

4542
sw.Write ("\t\tif (getClass () == ");
4643
sw.Write (Name);
@@ -58,7 +55,7 @@ public override void Generate (TextWriter sw, CallableWrapperWriterOptions optio
5855
break;
5956
default:
6057
sw.Write ("mono.android.TypeManager.Activate (\"");
61-
sw.Write (PartialAssemblyQualifiedName);
58+
sw.Write (DeclaringType.PartialAssemblyQualifiedName);
6259
sw.Write ("\", \"");
6360
sw.Write (ManagedParameters);
6461
sw.Write ("\", this, new java.lang.Object[] { ");

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.CallableWrapperMembers/CallableWrapperMethod.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ public class CallableWrapperMethod
2222
public string? ActivateCall { get; set; }
2323
public string JavaName => JavaNameOverride ?? Name;
2424
public List<CallableWrapperTypeAnnotation> Annotations { get; } = new List<CallableWrapperTypeAnnotation> ();
25+
public CallableWrapperType DeclaringType { get; }
2526

2627
public string? ThrowsDeclaration => ThrownTypeNames?.Length > 0 ? $" throws {string.Join (", ", ThrownTypeNames)}" : null;
2728

28-
public CallableWrapperMethod (string name, string method, string jniSignature)
29+
public CallableWrapperMethod (CallableWrapperType declaringType, string name, string method, string jniSignature)
2930
{
31+
DeclaringType = declaringType;
3032
Name = name;
3133
Method = method;
3234
JniSignature = jniSignature;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Java.Interop.Tools.JavaCallableWrappers;
2+
3+
public class CallableWrapperReaderOptions
4+
{
5+
public string? DefaultApplicationJavaClass { get; set; }
6+
public bool DefaultGenerateOnCreateOverrides { get; set; }
7+
public string? DefaultMonoRuntimeInitialization { get; set; }
8+
public JavaCallableMethodClassifier? MethodClassifier { get; set; }
9+
}

0 commit comments

Comments
 (0)