Affects PMD Version: 7.18.0
Rule: NullAssignment
Description:
If you have a final field that might be null or not, depending on the called constructor or depending on the arguments given to the constructor, you must set it to null explicitly in the constructor.
If you set it at declaration time, you cannot set it to a different value.
So assigning null to a final field in a constructor should not trigger a finding.
This does sometimes work as expected for example when directly assigning null, but if it is done as part of a ternary expression for example, the rule complains.
Code Sample demonstrating the issue:
https://github.com/Vampire/command-framework/blob/4e8f3137014399bc3e0673077239dbd641b63b20/src/main/java/net/kautler/command/api/Version.java#L95
import java.time.Instant;
public class Version {
private final Instant buildTimestamp;
public Version(String buildTimestamp) {
this.buildTimestamp = "$buildTimestamp".equals(buildTimestamp) ? null : Instant.parse(buildTimestamp);
}
}
Expected outcome:
PMD reports a violation at line 95, but that's wrong. That's a false positive.
Running PMD through: Gradle
Affects PMD Version: 7.18.0
Rule: NullAssignment
Description:
If you have a
finalfield that might benullor not, depending on the called constructor or depending on the arguments given to the constructor, you must set it tonullexplicitly in the constructor.If you set it at declaration time, you cannot set it to a different value.
So assigning
nullto afinalfield in a constructor should not trigger a finding.This does sometimes work as expected for example when directly assigning
null, but if it is done as part of a ternary expression for example, the rule complains.Code Sample demonstrating the issue:
https://github.com/Vampire/command-framework/blob/4e8f3137014399bc3e0673077239dbd641b63b20/src/main/java/net/kautler/command/api/Version.java#L95
Expected outcome:
PMD reports a violation at line 95, but that's wrong. That's a false positive.
Running PMD through: Gradle