Skip to content

[java] AccessorClassGeneration false positive for private constructors #755

@dotdoom

Description

@dotdoom

Rule Set:
Design: AccessorClassGeneration

Description:
When there are both private and public constructors in inner class, with the same number of arguments but different types, PMD assumes that private is called from the outer class even public is actually called.

Code Sample demonstrating the issue:

public class Outer
{
  public static class Inner {
    private Inner(int x) { }
    public Inner(Object x) { }
  }
  
  public static void main(String[] args)
  {
    // PMD.AccessorClassGeneration
    // Avoid instantiation through private constructors from outside of the constructors class.
    new Inner(null); // complains as if Inner(int) is was called
  }
}

Running PMD through: [Gradle]

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions