[Java.Interop.Tools.JavaCallableWrappers] [Export]+params#1161
Merged
Conversation
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 }); } } }
jonathanpeppers
approved these changes
Nov 13, 2023
jonathanpeppers
pushed a commit
that referenced
this pull request
Nov 13, 2023
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 }); } } }
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: dotnet/android#7554
Placing
[Export]on a constructor with parameters did not work as expected. Consider: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:In particular, because
signatureis the empty string,TypeManager.Activate()will lookup and invoke the default constructor, rather than theExample(int)constructor. (This despite the fact thatparameterListcontains arguments!)Consequently, when Java invokes the
Example(int)constructor, an exception is thrown, as the default constructor it wants doesn't exist:Update
JavaCallableWrapperGenerator.AddConstructor()so that themanagedParametersvalue is provided to theSignatureinstance for the[Export]constructor. This value is then inserted as thesignatureparameter value, which will allow the correct constructor to be invoked: