Skip to content

[java] ImmutableField reports fields annotated with @Value (Spring) #3981

@HaelC

Description

@HaelC

Affects PMD Version:
6.44.0 and 6.45.0

Rule:
ImmutableField
https://pmd.github.io/pmd-6.45.0/pmd_rules_java_design.html#immutablefield

Description:

Similar to the issue #3874, when annotating fields with Spring's @Value annotation, PMD (starting from 6.44.0) will report "the field could be made final. It is only initialized in the declaration or constructor." However, the fields need to be non-final in order for the reflection.

Code Sample demonstrating the issue:

@Configuration
public class SomeConfig {

    @Value("${someConfigValue}")
    private String someConfigField;
}

Expected outcome:

PMD reports a violation for someConfigField, but that's wrong. That's a false positive.

Running PMD through:
CLI and Maven

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions