Bug 10135 - InvalidProgramException when trying to implement a Parcelable base class
Summary: InvalidProgramException when trying to implement a Parcelable base class
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.4.x
Hardware: PC Windows
: Highest critical
Target Milestone: ---
Assignee: Rodrigo Kumpera
URL:
: 11177 ()
Depends on:
Blocks:
 
Reported: 2013-02-07 16:18 UTC by Paul Ryland
Modified: 2013-06-24 12:10 UTC (History)
4 users (show)

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


Attachments
Test project (VS2010) that reproduces the InvalidProgramException (11.76 KB, application/x-zip-compressed)
2013-02-07 16:18 UTC, Paul Ryland
Details


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 Paul Ryland 2013-02-07 16:18:11 UTC
Created attachment 3343 [details]
Test project (VS2010) that reproduces the InvalidProgramException

The following simple generic helper base class for creating parcelable objects results in an InvalidProgramException at runtime:

	/// <summary>A helper base class for creating parcelable objects</summary>
	public abstract class Parcelable<T> :Java.Lang.Object, IParcelable where T: Parcelable<T>, new()
	{
		/// <summary>Derived types should write their contents into 'dst'</summary>
		protected abstract void ToParcel(Parcel dst, ParcelableWriteFlags flags);

		/// <summary>Derived types should populate their fields from 'src'</summary>
		protected abstract void FromParcel(Parcel src);

		#region IParcelable implementation

		public sealed class Creator :Java.Lang.Object, IParcelableCreator
		{
			public Java.Lang.Object CreateFromParcel(Parcel source) { var t = new T(); t.FromParcel(source); return t; }
			public Java.Lang.Object[] NewArray(int size)            { return new T[size]; }
		}
		private static readonly Creator m_Creator = new Creator();

		[ExportField("CREATOR")] public static Creator GetCreator()        { return m_Creator; }
		public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags) { ToParcel(dest, flags); }
		public int DescribeContents()                                      { return 0; }

		#endregion
	}

The attached zip file is an example program that reproduces the exception.
Comment 1 Miguel de Icaza [MSFT] 2013-05-22 14:54:04 UTC
We are generating invalid code, take out the try/catch to see the error in all of its glory.
Comment 2 Jonathan Pryor 2013-05-31 19:59:03 UTC
The error in all it's glory!

> I/mono-stdout(19253): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: A type load exception has occurred.
> I/mono-stdout(19253):   at (wrapper managed-to-native) System.Delegate:CreateDelegate_internal (System.Type,object,System.Reflection.MethodInfo,bool)
> I/mono-stdout(19253):   at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed)
> I/mono-stdout(19253):   at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure)
> I/mono-stdout(19253):   at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method)
> I/mono-stdout(19253):   at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType)
> I/mono-stdout(19253):   at Java.Interop.DynamicCallbackCodeGenerator.GenerateNativeCallbackDelegate ()
> I/mono-stdout(19253):   at Java.Interop.DynamicCallbackCodeGenerator.GetCallback ()
> I/mono-stdout(19253):   at Java.Interop.DynamicCallbackCodeGenerator.Create (System.Reflection.MethodInfo method)
> I/mono-stdout(19253):   at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
> I/mono-stdout(19253):   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
> I/mono-stdout(19253):   --- End of inner exception stack trace ---
> I/mono-stdout(19253):   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
> I/mono-stdout(19253):   at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters)
> I/mono-stdout(19253):   at Android.Runtime.JNIEnv.CreateDynamicCallback (System.Reflection.MethodInfo method)
> I/mono-stdout(19253):   at Android.Runtime.JNIEnv.RegisterJniNatives (IntPtr typeName_ptr, Int32 typeName_len, IntPtr jniClass, IntPtr methods_ptr, Int32 methods_len)
> I/mono-stdout(19253):   at (wrapper managed-to-native) object:wrapper_native_0x407add49 (intptr,intptr,string,string)
> I/mono-stdout(19253):   at (wrapper delegate-invoke) <Module>:invoke_intptr__this___intptr_intptr_string_string (intptr,intptr,string,string)
> I/mono-stdout(19253):   at Android.Runtime.JNIEnv.GetMethodID (IntPtr kls, System.String name, System.String signature)
> I/mono-stdout(19253):   at Android.Runtime.JNIEnv.CreateInstance (IntPtr jniClass, System.String signature, Android.Runtime.JValue[] constructorParameters)
> I/mono-stdout(19253):   at Android.Runtime.JNIEnv.CreateInstance (System.Type type, System.String signature, Android.Runtime.JValue[] constructorParameters)
> I/mono-stdout(19253):   at Java.Lang.Object..ctor ()
> I/mono-stdout(19253):   at BugTest.Parcelable`1[BugTest.Thing]..ctor ()
> I/mono-stdout(19253):   at BugTest.Thing..ctor ()
> I/mono-stdout(19253):   at BugTest.Activity1.OnCreate (Android.OS.Bundle bundle)
Comment 3 Rodrigo Kumpera 2013-06-03 17:02:29 UTC
The problem here is an undocumented limitation of Xamarin.Android regarding
generics and subclasses of Java.Lang.Object.

A generic subclass of Java.Lang.Object must not introduce any export fields
or methods as those cannot be represented in java, which has no concept of
reified generic.

Our tooling have been improved to provide a clear error message (4207) at
build time and as a workaround for your code you can move the GetCreator
method to the subclass, having something like this:


public abstract class Parcelable<T> :Java.Lang.Object, IParcelable
	where T: Parcelable<T>, new()
{
	protected abstract void ToParcel(Parcel dst, ParcelableWriteFlags flags);

	protected abstract void FromParcel(Parcel src);

	public sealed class Creator :Java.Lang.Object, IParcelableCreator
	{
		public Java.Lang.Object CreateFromParcel(Parcel source) {
			var t = new T(); t.FromParcel(source);
			return t;
		}

		public Java.Lang.Object[] NewArray(int size) {
			return new T[size];
		}
	}

	protected static readonly Creator m_Creator = new Creator();

	public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags) {
		ToParcel(dest, flags);
	}
	public int DescribeContents() {
		return 0;
	}
}

public sealed class Thing :Parcelable<Thing>
{
	public int One;
	public int Two;

	protected override void ToParcel(Parcel dst, ParcelableWriteFlags flags)
	{
		dst.WriteInt(One);
		dst.WriteInt(Two);
	}

	protected override void FromParcel(Parcel src)
	{
		One = src.ReadInt();
		Two = src.ReadInt();
	}

	[ExportField("CREATOR")] public static Creator GetCreator() {
		return m_Creator;
	}
}
Comment 4 Paul Ryland 2013-06-03 19:57:20 UTC
I will give this workaround a try.
Thanks for the update.
Regards,
Paul
Comment 5 Jonathan Pryor 2013-06-24 12:10:19 UTC
*** Bug 11177 has been marked as a duplicate of this bug. ***