Bug 4904 - Improve managedReturn support for .jar binding
Summary: Improve managedReturn support for .jar binding
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Tools and Addins ()
Version: 4.0
Hardware: PC Mac OS
: --- enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-05-05 15:21 UTC by Jonathan Pryor
Modified: 2012-12-12 10:48 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jonathan Pryor 2012-05-05 15:21:35 UTC
Background:

The //method/@managedReturn attribute can be added via Metadata.xml to "fix" the return type of a Java method:

http://docs.xamarin.com/android/tutorials/Binding_a_Java_Library_(.jar)#API_Metadata

For example:

> <attr 
>     path="/api/package[@name='android.text']/class[@name='SpannableStringBuilder']/method[@name='append']"
>     name="managedReturn">Java.Lang.IAppendable</attr>

However, the managedReturn attribute has two requirements which makes it less usable:

1. The Java-side return type must still be a bound type.
2. There must be an implicit conversion between the bound return type and the managedReturn type.

For example, for the above SpannableStringBuilder.append() fixup, the resulting code:

>	static IntPtr id_append_C;
>	[Register ("append", "(C)Landroid/text/SpannableStringBuilder;", "GetAppend_CHandler")]
>	public virtual Java.Lang.IAppendable Append (char text)
>	{
>		if (id_append_C == IntPtr.Zero)
>			id_append_C = JNIEnv.GetMethodID (class_ref, "append", "(C)Landroid/text/SpannableStringBuilder;");
> 
>		if (GetType () == ThresholdType)
>			return Java.Lang.Object.GetObject<Android.Text.SpannableStringBuilder> (JNIEnv.CallObjectMethod  (Handle, id_append_C, new JValue (text)), JniHandleOwnership.TransferLocalRef);
>		else
>			return Java.Lang.Object.GetObject<Android.Text.SpannableStringBuilder> (JNIEnv.CallNonvirtualObjectMethod  (Handle, ThresholdClass, id_append_C, new JValue (text)), JniHandleOwnership.TransferLocalRef);
>	}

Note that the generated code still references the Android.Text.SpannableStringBuilder type, even though we're returning IAppendable. This works because Android.Text.SpannableStringBuilder implements IAppendable (thus the implicit conversion requirement).

The problem:

http://lists.ximian.com/pipermail/monodroid/2012-May/010056.html

In the above email, johnHolmes is trying to bind openFeint, which has a Java method:

    public java.lang.reflect.Type getType();

This method can't be bound, because we didn't bind the java.lang.reflect.Type interface.

Proposed solution: 

If the Java return type isn't valid (as would be the case for java.lang.reflect.Type), instead of erroring out, fallback to checking the managedReturn attribute. If a managedReturn method is present, use that as both the method return type and in the method implementation.

Proposed fixup:

> <attr
> path="/api/package[@name='com.openfeint.internal.vendor.org.codehaus.jackson.type']/class[@name='TypeReference']/method[@name='getType']"
> name="managedReturn">Java.Lang.Object</attr>

The binding generator could then generate method binding:

>	static IntPtr id_getType;
>	public virtual Java.Lang.Object Type {
>		get {
>			if (id_getType == IntPtr.Zero)
>				id_getType = JNIEnv.GetMethodID (class_ref, "getType", "()Ljava/lang/reflect/Type;");
> 
>			if (GetType () == ThresholdType)
>				return
>				Java.Lang.Object.GetObject<Java.Lang.Object> (JNIEnv.CallObjectMethod  (Handle, id_getType), JniHandleOwnership.TransferLocalRef);
>			else
>				return Java.Lang.Object.GetObject<Java.Lang.Object> (JNIEnv.CallNonvirtualObjectMethod  (Handle, ThresholdClass, id_getType,), JniHandleOwnership.TransferLocalRef);
>		}
>	}
Comment 1 Jonathan Pryor 2012-12-12 10:48:55 UTC
Fixed in master/b9a27e45.