Skip to content

Issue #13809: Kill mutation in TokenUtil#13943

Merged
rnveach merged 1 commit intocheckstyle:masterfrom
Kevin222004:TokenUtil
Oct 29, 2023
Merged

Issue #13809: Kill mutation in TokenUtil#13943
rnveach merged 1 commit intocheckstyle:masterfrom
Kevin222004:TokenUtil

Conversation

@Kevin222004
Copy link
Copy Markdown
Contributor

Issue #13809: Kill mutation in TokenUtil

Explaintaion

public static int getIntFromField(Field field, Object object) {

we are passing object as a param so I think it will not make a difference either we pass only name or whole field

Copy link
Copy Markdown
Contributor

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

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

Item:

.filter(fld -> Modifier.isPublic(fld.getModifiers()) && fld.getType() == Integer.TYPE)
.collect(Collectors.toUnmodifiableMap(
Field::getName, fld -> getIntFromField(fld, fld.getName()))
Field::getName, fld -> getIntFromField(fld, fld))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

getIntFromField is basically a wrapper for Field#getInt. From reading the docs of Field, it looks like we should be passing the class itself to this method. I am not sure how this ever worked in the first place. Please do:

Suggested change
Field::getName, fld -> getIntFromField(fld, fld))
Field::getName, fld -> getIntFromField(fld, cls))

Copy link
Copy Markdown
Contributor

@rnveach rnveach Oct 25, 2023

Choose a reason for hiding this comment

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

I am not sure how this ever worked in the first place

This is because we are going against a static field. It has no instance to speak of, so most likely reflection is ignoring the 2nd parameter we pass. We also have similar funky uses in TokenUtilTest as getIntFromField is public when it should not be.

Truthfully this 2nd parameter should be null, as there is no instance.

Suggested change
Field::getName, fld -> getIntFromField(fld, fld))
Field::getName, fld -> getIntFromField(fld, null))

Also nothing else but 1 method uses getIntFromField and we are now saying the 2nd parameter is completely unnecessary. getIntFromField needs to be refactored.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@rnveach leaving this for you to resolve.

@romani
Copy link
Copy Markdown
Member

romani commented Oct 25, 2023

@Kevin222004 , can we put null in second parameter to make it clear it is to be ignored.

Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

Ok to merge.

@Kevin222004 , please update Checker config

Copy link
Copy Markdown
Contributor

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

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

I really don't see any other method in java.lang.reflect.Field that we can use to avoid second parameter. I am good with null to make it clear that the second parameter doesn't matter in the case of static fields.

@nrmancuso nrmancuso assigned rnveach and unassigned nrmancuso Oct 29, 2023
@nrmancuso
Copy link
Copy Markdown
Contributor

Assigned to @rnveach to complete review, I am not sure if we want to open a ticket for refactor of getIntFromField or not, I don't think it is a big deal in this case.

@rnveach
Copy link
Copy Markdown
Contributor

rnveach commented Oct 29, 2023

@Kevin222004 If you want an easy assignment, getIntFromField can be refactored as mentioned at #13943 (comment) .

@rnveach rnveach merged commit 7c0950e into checkstyle:master Oct 29, 2023
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.

4 participants