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

Prefer get getters over is getters in recorders #26895

Merged
merged 1 commit into from
Jul 25, 2022

Conversation

Christopher-Chianelli
Copy link
Contributor

@Christopher-Chianelli Christopher-Chianelli commented Jul 22, 2022

Fix #26899

When both get getters and is getters exist, they can
replace the original selected getter in PropertyUtils.
This can creates inconsistencies, resulting in a field
not being set, causing a NPE later down the line that is
hard to diagnose.

To remove inconsistencies, if there is both a get method
and an is method, the get method is always preferred.
This introduces consistent behavior for recorders.

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 22, 2022

Thanks for your pull request!

The title of your pull request does not follow our editorial rules. Could you have a look?

  • title should preferably start with an uppercase character (if it makes sense!)
  • title should not contain an issue number (use Fix #1234 in the description instead)

This message is automatically generated by a bot.

@Christopher-Chianelli Christopher-Chianelli changed the title fix #26899: Prefer get getters over is getters in recorders Prefer get getters over is getters in recorders Jul 22, 2022
@quarkus-bot

This comment has been minimized.

List<Method> checkedMethodList = new ArrayList<>();

for (Method method : methods) {
if (method.getName().startsWith("get")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and thanks for given it a try :).

Comment on lines 48 to 27
Map<String, Method> getters = new HashMap<>();
Map<String, Method> setters = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought a bit more about it and I think you should have three maps here and add a isGetters map.
You would then at the end of the loop, remove everything from the isGetters map that has a key in getters (isGetters.keySet().removeAll(getters.keySet()) should do the trick). And then as a last step, you can add the isGetters content to getters.

That should simplify things a bit and would avoid going through the loop twice doing mostly the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the map; looking at how getters is used, I think it would be more clear to change

Method get = getters.get(i);

to

Method get = getters.get(i);
if (get == null) {
    get = isGetters.get(i); // If there is no "get" getter, use the "is" getter
}

(which in turn remove the need for removing the "get" getter keys from the "isGetters" map).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me :).

@gsmet
Copy link
Member

gsmet commented Jul 25, 2022

Can you squash both commits? Thanks!

When both get getters and is getters exist, they can
replace the original selected getter in PropertyUtils.
This can creates inconsistencies, resulting in a field
not being set, causing a NPE later down the line that is
hard to diagnose.

To remove inconsistencies, if there is both a get method
and an is method, the get method is always preferred.
This introduces consistent behavior for recorders.

To accomplish this, a seperate map is used for isGetters in
PropertyUtils. Using a seperate map removes the need to have a seperate
loop for identifing the actual getter method. It also
make the logic more clear (first try "get" getter, then try "is"
getter).
@Christopher-Chianelli
Copy link
Contributor Author

Squashed

@gsmet gsmet added the triage/waiting-for-ci Ready to merge when CI successfully finishes label Jul 25, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Jul 25, 2022

Failing Jobs - Building 4fa92ff

Status Name Step Failures Logs Raw logs
Native Tests - Windows - RESTEasy Jackson Run actions/checkout@v2 ⚠️ Check → Logs Raw logs

@gsmet gsmet merged commit 15ffefc into quarkusio:main Jul 25, 2022
@quarkus-bot quarkus-bot bot added this to the 2.12 - main milestone Jul 25, 2022
@quarkus-bot quarkus-bot bot removed the triage/waiting-for-ci Ready to merge when CI successfully finishes label Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants