Skip to content

CS1503 changing string to JLO via managedType #967

@jpobst

Description

@jpobst

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugComponent does not function as intendedgeneratorIssues binding a Java library (generator, class-parse, etc.)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions