Bug 58405 - Bundle getIntegerArrayList & getStringArrayList throw InvalidCastException if Bundle.get is called first
Summary: Bundle getIntegerArrayList & getStringArrayList throw InvalidCastException if...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Android+BCL Integration ()
Version: 7.3 (15.2)
Hardware: PC Windows
: --- normal
Target Milestone: 15.4
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2017-07-26 00:58 UTC by Scott Mertz
Modified: 2017-08-07 09:19 UTC (History)
2 users (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 Scott Mertz 2017-07-26 00:58:33 UTC
If calling Bundle.get(key) before Bundle.GetIntegerArrayList(key), the call to Bundle.GetIntegerArrayList will throw an InvalidCastException.

        [Test]
        public void TestBundleIntegerArrayList2()
        {
            Bundle b = new Bundle();
            b.PutIntegerArrayList("key", new List<Java.Lang.Integer>() { Java.Lang.Integer.ValueOf(1) });
            var obj = b.Get("key");
            try
            {
                var list = b.GetIntegerArrayList("key");
            }
            catch(Exception e)
            {
                Assert.Fail("GetIntegerArrayList threw {0}", e);
            }
        }

However, if you first call Bundle.GetIntegerArrayList(key), the following calls work correctly:

        [Test]
        public void TestBundleIntegerArrayList()
        {
            Bundle b = new Bundle();
            b.PutIntegerArrayList("key", new List<Java.Lang.Integer>() { Java.Lang.Integer.ValueOf(1) });
            var list = b.GetIntegerArrayList("key");
            var obj = b.Get("key");
            list = b.GetIntegerArrayList("key");
        }
Comment 1 Scott Mertz 2017-07-26 01:08:30 UTC
This also occurs for Bundle.GetStringArrayList
Comment 2 Jon Douglas [MSFT] 2017-07-26 02:58:53 UTC
Seeing that the regular "Bundle.Get" method returns a Java.Lang.Object, it seems like the proper cast needs to be applied to the jobject prior, otherwise it will come out as a Java.Lang.Object. Then the next attempt at using a method such as "GetIntegerArrayList" or "GetStringArrayList", it's an invalid cast because it's still now the base object(in JNI land).

i.e.

07-25 20:47:49.291 I/mono-stdout( 6672):   at (wrapper castclass) System.Object:__castclass_with_cache (object,intptr,intptr)
  at Android.Runtime.JavaList`1[T].FromJniHandle (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x0002a] in <d855bac285f44dda8a0d8510b679b1e2>:0 
  at Android.OS.Bundle.GetIntegerArrayList (System.String key) [0x0003d] in <d855bac285f44dda8a0d8510b679b1e2>:0 
  at UnitTestApp1.TestsSample.TestBundleIntegerArrayList2 () [0x00032] in c:\users\dougl\documents\visual studio 2017\Projects\UnitTestApp1\UnitTestApp1\TestSample.cs:39 

I'm not a JNI expert, so I am CONFIRMING this behavior as it seems like we should be able to add a fair warning to this method. I believe the best behavior is to not use the generic Get method other than a last priority. Rather it should be the proper type Get method and then eventually the generic Get method that returns Java.Lang.Object.
Comment 3 Marek Habersack 2017-08-03 19:22:39 UTC
Proposed fix: https://github.com/xamarin/xamarin-android/pull/717