GithubHelp home page GithubHelp logo

Comments (9)

johncarl81 avatar johncarl81 commented on May 7, 2024

Thanks for the feedback @ahem, I'll have to look at your proposal. An error should be given in the example you gave that there is no default empty constructor and you do not have a constructor annotated. In order to get this to work, you will need to annotate the constructor you want to use in the given class, like so:

@Parcel
public class AClass {
    String message;
    public final String getMessage() { return this.message; }
    public void setMessage(String message) { this.message = message; }

    @ParcelConstructor
    public AClass(String message) {
        this.message = message;
    }
}

@Parcel
public class ASubClass extends AClass {
    int status;
    public int getStatus() { return status; }
    public void setStatus(int status) { this.status = status; }

    @ParcelConstructor
    public ASubClass(String message, int status) {
        super(message);
        this.status = status;
    }
}

Is the problem that Parceler is not associating the "message" parameter between the constructor parameter and the super classes' field?

from parceler.

ahem avatar ahem commented on May 7, 2024

Interestingly AClass works just fine without the @ParcelConstructor annotation. It seems that, when there is only one constructor defined on the class, then parceller defaults to that and doesn't require the annotation (at least, that is what I think this bit does: ParcelableAnalysis:258).

But, anyway, as you say, constructor annotation or no constructor annotation, when I add the subclass then the project doesn't compile anymore, and fails with this error:

Parceler: Accessor not found for property message

I can understand that this happens because there is no accessor defined for the message property in ASubClass, but there is one available in the base class, so I would think parceler would just use that?

from parceler.

johncarl81 avatar johncarl81 commented on May 7, 2024

Looking at this issue in depth. I can reproduce it and confirm that this not the expected behavior of Paceler.

First, you should be required to annotate the constructor you want to use if a single empty constructor (or default constructor) doesn't exist. Otherwise this should flag an error. Parceler does not automatically decide which constructor is appropriate to use, it needs your direction. The code you highlighted does part of that, it looks for a single empty constructor, the code that flags the error just doesn't exist (yet).

Second, and this is where I would like feedback from you @ahem, associating constructor parameters with properties in the inheritance hierarchy of a bean presents a challenge. At each level of the class hierarchy, Parceler requires the classes' properties (defined by either fields, methods or manually annotated) to be uniquely named. This is easy to enforce because you cannot name a field or method the same thing twice. Constructor parameters must match some reader in the class. This allows Parceler to write the value in the constructor and read it back from a field or method. Right now constructor parameters may only match the same hierarchy level properties. If we were to match on all hierarchy levels, we would need to check for uniqueness across the entire hierarchy. I can see a couple solutions to this:

  1. All Constructor Parameters must have unique readers across all levels of inheritance.
  2. Constructor Parameters match the highest hierarchy level property reader, if duplicates exist

I'm starting to lean towards matching the highest level reader because we would not have to do a deep duplicate check.

What are your thoughts on this?

from parceler.

ahem avatar ahem commented on May 7, 2024

I think that the way constructor parameters are supported now is very easy to understand - it seems very natural that you just have to name the parameter the same as the field or getter function, and then Parceler automatically connects them.

I really would like it if Parceler would match property accessors on other hierarchy levels, though, and I agree with you that the best way to handle duplicated property names would be to use the accessor on the highest level.

from parceler.

johncarl81 avatar johncarl81 commented on May 7, 2024

I decided to go with option 1 as it's less mysterious and easier to fix when problems come up. I also was able to make your use case pass, with the @ParcelConstructor parameters added. I've deployed the Parceler SNAPSHOT to maven central under 0.2.8-SNAPSHOT, could you give it a try and let me know if it works for you?

from parceler.

johncarl81 avatar johncarl81 commented on May 7, 2024

@ahem I'm thinking about cutting a release soon, I'd appreciate your feedback before then.

from parceler.

johncarl81 avatar johncarl81 commented on May 7, 2024

Closing this issue, feel free to reopen if this fix didn't work.

from parceler.

ahem avatar ahem commented on May 7, 2024

@johncarl81 Sorry for the delayed feedback - I was on vacation over easter, and internet access turned out to be pretty difficult to find.

Anyways, I have tested you changes and they work perfectly. Thank you very much for the quick response and fix :-)

from parceler.

johncarl81 avatar johncarl81 commented on May 7, 2024

Fantastic. Thank you for reporting it!

from parceler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.