Skip to content

[java] Fix #4008 - ImmutableField FN with field assigned only in initializer#4018

Merged
adangel merged 13 commits into
pmd:masterfrom
oowekyala:issue4008_immutable_field
Jun 23, 2022
Merged

[java] Fix #4008 - ImmutableField FN with field assigned only in initializer#4018
adangel merged 13 commits into
pmd:masterfrom
oowekyala:issue4008_immutable_field

Conversation

@oowekyala

@oowekyala oowekyala commented Jun 17, 2022

Copy link
Copy Markdown
Member

Describe the PR

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@oowekyala oowekyala added this to the 6.47.0 milestone Jun 17, 2022
@ghost

ghost commented Jun 18, 2022

Copy link
Copy Markdown
1 Message
📖 No regression tested rules have been changed.
Compared to master:
This changeset changes 0 violations,
introduces 462 new violations, 0 new errors and 0 new configuration errors,
removes 764 violations, 2 errors and 6 configuration errors.
Full report

Generated by 🚫 Danger

@oowekyala

Copy link
Copy Markdown
Member Author

Seems like my change removes violations on fields that are never assigned (even in their initializer). Should we report them? It may be set via reflection, but also be a legit mistake. It doesn't feel like the correct rule to report that though. In any case the error message should be different in this case...

@HaelC

HaelC commented Jun 18, 2022

Copy link
Copy Markdown

Thanks @oowekyala. It seems that with the code change, defaultSuppressionAnnotations() is not necessary in ImmutableFieldRule now. I tried to delete line 38-49 and line 7-8, and the build remains successful. Do we want to remove those lines?

@oowekyala

Copy link
Copy Markdown
Member Author

Thanks for pointing this out. I think it makes sense, especially since these issues keep coming... (#4020)

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

I've verified the other issues with annotations and this PR solves them all.

Now ImmutableField only considers fields, that are initialized once and never changed - either in the constructor or in the declaration.
If the field is not initialized at all (which is the typical case when a framework will set the value by reflection), the rule won't trigger now for such fields.

I'll remove the suppressed annotations added lately, as they are not needed anymore.

We can think for the next version about removing/deprecating the property ignoredAnnotations for this rule entirely. Not sure about the class-level lombok annotations though...

@adangel adangel merged commit 18dd7f1 into pmd:master Jun 23, 2022
@jsotuyod

jsotuyod commented Jun 23, 2022

Copy link
Copy Markdown
Member

Sorry I'm late, but I was curious about the solution not needing to whitelist annotations individually. I love the approach, I think it's the right one, and I don't think we need to deprecate or remove the ignoredAnnotations property (that is standard, and there may still be some edge cases that can be covered that way).

However, upon code review, I think that this is missing initializations on non-static initializers, ie:

class Foo {
  private int myImmutableField;

  {
    myImmutableField = 5;
  }

  public int getMyImmutableField() {
    return myImmutableField;
  }
}

I reckon non-static initializers are not common, but we should still consider them.

@oowekyala

Copy link
Copy Markdown
Member Author

However, upon code review, I think that this is missing initializations on non-static initializers

I think you're right Juan, would you open an issue? I think it is rare enough that it has low priority though. The rule also does not handle very complicated control flow yet. In pmd 7 it uses the new data flow logic which is more robust, and which already handles initializers properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment