Skip to content

[Java.Interop.Tools.JavaCallableWrappers] [Export]+params #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

jonpryor
Copy link
Contributor

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 });
        }
    }
}

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 });
	        }
	    }
	}
@jonpryor jonpryor merged commit f9a16d5 into main Nov 13, 2023
@jonpryor jonpryor deleted the dev/jonp/jonp-ctor+ExportAttr-params branch November 13, 2023 16:59
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 });
	        }
	    }
	}
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using the Export attribute with a constructor with parameter generates a wrong ACW
2 participants