Skip to content

JsonObjectConverter: Ignore all final backing-fields#298

Open
stangls wants to merge 2 commits intocbeust:masterfrom
stangls:master
Open

JsonObjectConverter: Ignore all final backing-fields#298
stangls wants to merge 2 commits intocbeust:masterfrom
stangls:master

Conversation

@stangls
Copy link
Copy Markdown

@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