[java] Fix #4008 - ImmutableField FN with field assigned only in initializer#4018
Conversation
Generated by 🚫 Danger |
|
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... |
|
Thanks @oowekyala. It seems that with the code change, |
|
Thanks for pointing this out. I think it makes sense, especially since these issues keep coming... (#4020) |
adangel
left a comment
There was a problem hiding this comment.
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...
|
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 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. |
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 |
Describe the PR
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)