Skip to content

Builders for subclasses cannot initialize inherited private attributes #121

Description

@schmalzing

Issue description

The class-diagram generator creates builders for all classes when ${decConfig.withBuilders().defaultApply()} is configured. For subclasses, the generated builder also initializes attributes inherited from the superclass.

However, when an inherited attribute is declared private, the generated subclass builder accesses it directly. Since private Java fields cannot be accessed from a subclass, the generated Java sources fail compilation.

Reproduction

Example CD:

classdiagram CD {
  public class A {
    private String name;
  }

  public class B extends A { }
}

Generate the Java code with:

${decConfig.withBuilders().defaultApply()}

Actual behavior

The generator creates BBuilder and emits code equivalent to:

var v = new B();
v.name = this.name;

This produces a Java compiler error:

ERROR in ...\test\CD\BBuilder.java (at line 104)
v.name = this.name;
  ^^^^
The field A.name is not visible

The generated BBuilder tries to directly initialize name, although the attribute is declared as private in superclass A.

Expected behavior

The generated builder for a subclass must not directly access private attributes declared in a superclass.

Possible valid generator behavior would be one of:

  • Initialize inherited attributes through an accessible constructor, setter, or other method declared by the superclass.
  • Delegate initialization of inherited attributes to the superclass builder.
  • Exclude private inherited attributes from the subclass builder.

Impact

Any CD containing a subclass whose superclass declares private attributes can produce Java code that does not compile when builders are enabled. This prevents the use of encapsulated attributes together with inheritance in generated POJO models.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions