Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonObjectConverter: Ignore all final backing-fields #298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

stangls
Copy link

@stangls stangls commented Feb 10, 2020

Problem:
Assume a class, that has two constructors (see test TwoConstructorsWithFinalField):

  • one primary constructor with value-parameters. (JVM final fields)
  • a secondary constructor.

When instantiated using Klaxon from JSON, the final fields of the primary constructor can not be filled , which may result in a non-neccessary Exception.
java.lang.IllegalArgumentException: Can not set final {{typeX}} field {{...}} to {{typeX or typeY}}

Solution:
Ignore final fields after instantiating the class in Klaxon object converter.

Reasoning:
Final fields can not be set to a new value.
All final fields can be ignored by Klaxon after instantiation, because (in JVM) they must have already been initialized during construction of the object.

@stangls stangls changed the title Do not try to set final fields ObjectConverter: Do not try to set final fields Feb 10, 2020
@stangls stangls requested a review from cbeust February 10, 2020 17:46
@stangls stangls changed the title ObjectConverter: Do not try to set final fields JsonObjectConverter: Ignore all final backing-fields Feb 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant