Skip to content

Commit f9a16d5

Browse files
authored
[Java.Interop.Tools.JavaCallableWrappers] [Export]+params (#1161)
Fixes: dotnet/android#7554 Placing `[Export]` on a constructor with parameters did not work as expected. Consider: public class Example : Java.Lang.Object { [Export(SuperArgumentsString = "")] public Example (int value) { this.value = value; } int value; } This *does* generate a Java Callable Wrapper with the desired constructor, but it *doesn't* invoke the correct C# constructor, because of the `TypeManager.Activate()` invocation: // Java JCW /* partial */ class Example extends java.lang.Object { public Example(int p0) { super (); if (getClass () == Example.class) { mono.android.TypeManager.Activate ( /* typeName */ "Namespace.Example, Assembly", /* signature */ "", /* instance */ this, /* parameterList */ new java.lang.Object[] { p0 }); } } } In particular, because `signature` is the empty string, `TypeManager.Activate()` will lookup and invoke the *default* constructor, rather than the `Example(int)` constructor. (This despite the fact that `parameterList` contains arguments!) Consequently, when Java invokes the `Example(int)` constructor, an exception is thrown, as the default constructor it wants doesn't exist: Could not activate JNI Handle 0x7ffd2bd94e50 (key_handle 0x65d856e) of Java type 'namespace/Example' as managed type 'Namespace.Example'. Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown. Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown. --- End of managed Java.Lang.Error stack trace --- java.lang.Error: Java callstack: at mono.android.TypeManager.n_activate(Native Method) at mono.android.TypeManager.Activate(TypeManager.java:7) at namespace.Example.<init>(Example.java:0) … Update `JavaCallableWrapperGenerator.AddConstructor()` so that the `managedParameters` value is provided to the `Signature` instance for the `[Export]` constructor. This value is then inserted as the `signature` parameter value, which will allow the correct constructor to be invoked: // Fixed Java JCW /* partial */ class Example extends java.lang.Object { public Example(int p0) { super (); if (getClass () == Example.class) { mono.android.TypeManager.Activate ( /* typeName */ "Namespace.Example, Assembly", /* signature */ "System.Int32, System.Runtime", /* instance */ this, /* parameterList */ new java.lang.Object[] { p0 }); } } }
1 parent 28849ec commit f9a16d5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ void AddConstructor (MethodDefinition ctor, TypeDefinition type, string? outerTy
295295
if (!string.IsNullOrEmpty (eattr.Name)) {
296296
// Diagnostic.Warning (log, "Use of ExportAttribute.Name property is invalid on constructors");
297297
}
298-
ctors.Add (new Signature (ctor, eattr, cache));
298+
ctors.Add (new Signature (ctor, eattr, managedParameters, cache));
299299
curCtors.Add (ctor);
300300
return;
301301
}
@@ -481,7 +481,7 @@ void AddMethod (MethodDefinition? registeredMethod, MethodDefinition implemented
481481
if (type.HasGenericParameters)
482482
Diagnostic.Error (4206, LookupSource (implementedMethod), Localization.Resources.JavaCallableWrappers_XA4206);
483483

484-
var msig = new Signature (implementedMethod, attr, cache);
484+
var msig = new Signature (implementedMethod, attr, managedParameters: null, cache: cache);
485485
if (!string.IsNullOrEmpty (attr.SuperArgumentsString)) {
486486
// Diagnostic.Warning (log, "Use of ExportAttribute.SuperArgumentsString property is invalid on methods");
487487
}
@@ -834,14 +834,15 @@ public Signature (MethodDefinition method, RegisterAttribute register, string? m
834834
IsDynamicallyRegistered = shouldBeDynamicallyRegistered;
835835
}
836836

837-
public Signature (MethodDefinition method, ExportAttribute export, IMetadataResolver cache)
837+
public Signature (MethodDefinition method, ExportAttribute export, string? managedParameters, IMetadataResolver cache)
838838
: this (method.Name, GetJniSignature (method, cache), "__export__", null, null, export.SuperArgumentsString)
839839
{
840840
IsExport = true;
841841
IsStatic = method.IsStatic;
842842
JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask);
843843
ThrownTypeNames = export.ThrownNames;
844844
JavaNameOverride = export.Name;
845+
ManagedParameters = managedParameters;
845846
Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache);
846847
}
847848

tests/Java.Interop.Tools.JavaCallableWrappers-Tests/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGeneratorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public ExportsConstructors (int p0)
449449
{
450450
super (p0);
451451
if (getClass () == ExportsConstructors.class) {
452-
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { p0 });
452+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", ""System.Int32, System.Private.CoreLib"", this, new java.lang.Object[] { p0 });
453453
}
454454
}
455455
@@ -505,7 +505,7 @@ public ExportsThrowsConstructors (int p0) throws java.lang.Throwable
505505
{
506506
super (p0);
507507
if (getClass () == ExportsThrowsConstructors.class) {
508-
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { p0 });
508+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", ""System.Int32, System.Private.CoreLib"", this, new java.lang.Object[] { p0 });
509509
}
510510
}
511511
@@ -514,7 +514,7 @@ public ExportsThrowsConstructors (java.lang.String p0)
514514
{
515515
super (p0);
516516
if (getClass () == ExportsThrowsConstructors.class) {
517-
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { p0 });
517+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", ""System.String, System.Private.CoreLib"", this, new java.lang.Object[] { p0 });
518518
}
519519
}
520520

0 commit comments

Comments
 (0)