johncarl81 / parceler

:package: Android Parcelables made easy through code generation.
http://parceler.org
Apache License 2.0
3.56k stars 273 forks source link

Polymorphism documentation not updated? #385

Closed WildOrangutan closed 3 years ago

WildOrangutan commented 4 years ago

I've tried polymorphism example, and it prints true in both cases.

Version: 1.1.13

WildOrangutan commented 4 years ago

Ok, I've done some experiments to figure how it actually behaves.

Looks like there is some instance caching going on, and documentation does not mention that.

~~Anyone else wondering... output of example from documentation is correct, when app process is re-created. If only simple rotation occurs, object instance stays the same, hence my original post results hold.~~

Don't know to what extent I like instance caching tough. Could be bug prone on mutable objects.

johncarl81 commented 4 years ago

Hmm, could you show an example of what you mean? We do have some caching, but it's meant for bidirectional object relationships.

WildOrangutan commented 4 years ago

As far as the first post goes, I've been trying out same example from documentation. I've just added reference checking, like so:

Example in = new Example(new Child());
System.out.println(in.p instanceof Child); // true

Example out = Parcels.unwrap(Parcels.wrap(in));
System.out.println(out.p instanceof Child); // true

System.out.println(out == in); // true

For the second post, the project I'm working on has mislead me. Not sure why atm. I can't reproduce it elsewhere, sorry about that. I'll cross-out sentences above, that are probably incorrect.

johncarl81 commented 4 years ago

Ah, this is happening because the Parcelable isn't actually going through a serialization/deserialization cycle. If you're running this on your desktop I've put together a testing utility that forces this cycle to take place here: ParcelsTestUtil. Here's a passing test if you want to try it out:

@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class TestPolymorphism {

    @Parcel
    public static class Example {
        public Parent p;
        @ParcelConstructor Example(Parent p) { this.p = p; }
    }

    @Parcel public static class Parent {}
    @Parcel public static class Child extends Parent {}

    @Test
    public void testStuff() {
        Example example = new Example(new Child());
        assertTrue(example.p instanceof Child);
        example = Parcels.unwrap(ParcelsTestUtil.wrap(example));
        assertFalse(example.p instanceof Child);
    }
}
WildOrangutan commented 4 years ago

I see. Thanks for explanation.

johncarl81 commented 4 years ago

Maybe the docs could be more explicit about this. They are fork friendly btw.