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

Fix problem with private members. #8

Merged
merged 2 commits into from Jun 22, 2022

Conversation

jlkeesey
Copy link
Contributor

The existing code encodes all the properties of a data class even ones not
in the primary constructor but only ever uses the primary constructor.
Any private properties cause this code to fail as we do not have access to
them (the isAccessible trick doesn't work on private properties that do not
have a backing store).

This changes the code to only encode the properties of the primary constructor's
parameters. As data classes only have property parameters in the primary
constructor, this is sufficient.

As part of reading the values the isAccessible flag is adjusted to make the
values accessible and now the logic resets the isAccessible flag to its original
value after processing.

Also added a check for IllegalAccessException that displays the parameter that
caused the problem to help with debugging in the future. In theory this won't
happen, but if something changes in how properties are handled in the future
this should help identify the issue.

The existing code encodes all the properties of a data class even ones not
in the primary constructor but only ever uses the primary constructor.
Any private properties cause this code to fail as we do not have access to
them (the isAccessible trick doesn't work on private properties that do not
have a backing store).

This changes the code to only encode the properties of the primary constructor's
parameters. As data classes only have property parameters in the primary
constructor, this is sufficient.

As part of reading the values the isAccessible flag is adjusted to make the
values accessible and now the logic resets the isAccessible flag to its original
value after processing.

Also added a check for IllegalAccessException that displays the parameter that
caused the problem to help with debugging in the future. In theory this won't
happen, but if something changes in how properties are handled in the future
this should help identify the issue.
@valderman
Copy link
Owner

Great!

Just a minor nitpick, could you also add a test or two verifying that (1) this solves the problem of including properties not in the primary constructor and (2) that private properties that are in the primary constructor are still included?

@jlkeesey
Copy link
Contributor Author

Ok, test has been added. It was so simple I didn't feel that it needed two separate tests.

@valderman valderman linked an issue Jun 22, 2022 that may be closed by this pull request
@valderman valderman merged commit 39f2174 into valderman:main Jun 22, 2022
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.

Data classes with private properties not handled by encoding
2 participants