Skip to content

[java] ConstructorCallsOverridableMethod: False positive when method is from enclosing class #6163

@stevenschlansker

Description

@stevenschlansker

Affects PMD Version: 7.17.0

Rule:

Please provide the rule name and a link to the rule documentation:
ConstructorCallsOverridableMethod
https://pmd.github.io/pmd/pmd_rules_java_errorprone.html#constructorcallsoverridablemethod

Description:

[WARNING] PMD Failure: Inner:15 Rule:ConstructorCallsOverridableMethod Priority:1 Overridable method called during object construction: TestOverridableNestedClass.value().

Code Sample demonstrating the issue:

public class TestOverridableNestedClass {

    public Inner createInner() {
        return new Inner();
    }

    public String value() {
        return "42";
    }

    class Inner {
        private final String value;

        Inner() {
            this.value = value();
        }

        public String getValue() {
            return value;
        }
    }
}

Expected outcome:
No violation, the call to value() is on a fully-constructed instance of the enclosing class, not the under-construction inner class.

Running PMD through: 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