Skip to content

[generator] Add string cast to prevent CS1503. #970

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
Apr 19, 2022
Merged

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Apr 18, 2022

Fixes: #967
Context: dotnet/android-libraries#512

If you need to change a string to JLO for a method parameter, the generated code produces a CS1503 error because it generates this code:

// Metadata.xml XPath method reference: path="/api/package[@name='androidx.activity.result.contract']/class[@name='ActivityResultContracts.RequestPermission']/method[@name='createIntent' and count(parameter)=2 and parameter[1][@type='android.content.Context'] and parameter[2][@type='java.lang.String']]"
[Register ("createIntent", "(Landroid/content/Context;Ljava/lang/String;)Landroid/content/Intent;", "")]
public override unsafe global::Android.Content.Intent CreateIntent (global::Android.Content.Context context, global::Java.Lang.Object input)
{
	const string __id = "createIntent.(Landroid/content/Context;Ljava/lang/String;)Landroid/content/Intent;";
	IntPtr native_input = JNIEnv.NewString (input);
	try {
		JniArgumentValue* __args = stackalloc JniArgumentValue [2];
		__args [0] = new JniArgumentValue ((context == null) ? IntPtr.Zero : ((global::Java.Lang.Object) context).Handle);
		__args [1] = new JniArgumentValue (native_input);
		var __rm = _members.InstanceMethods.InvokeAbstractObjectMethod (__id, this, __args);
		return global::Java.Lang.Object.GetObject<global::Android.Content.Intent> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
	} finally {
		JNIEnv.DeleteLocalRef (native_input);
		global::System.GC.KeepAlive (context);
		global::System.GC.KeepAlive (input);
	}
}

This produces:

Error CS1503 Argument 1: cannot convert from 'Java.Lang.Object' to 'string?'

For this line:

  IntPtr native_input = JNIEnv.NewString (input);

Although there is a cast from JLO to string, the cast is explicit, meaning we need to use this:

  IntPtr native_input = JNIEnv.NewString ((string?)input);

The solution is to add the explicit (string?) cast in generator.

@jpobst jpobst marked this pull request as ready for review April 18, 2022 21:41
@jpobst jpobst requested a review from jonpryor April 18, 2022 21:41
@jonpryor jonpryor merged commit 05eddd9 into main Apr 19, 2022
@jonpryor jonpryor deleted the string-cast branch April 19, 2022 00:06
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 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.

CS1503 changing string to JLO via managedType
2 participants