Skip to content

CD2Pojo does not handle reserved Java keywords derived from class names #124

Description

@schmalzing

Issue description

When generating code for an association, CD2Pojo derives the Java role name from the associated class name by converting its first character to lowercase.

Consequently, the valid CD class names Public and Class result in the Java role names public and class, respectively. While the original class names are valid Java type names, the derived role names are Java keywords and therefore cannot be used as field identifiers.

CD2Pojo currently emits these names unchanged, causing invalid Java code to be generated instead of adapting the generated identifiers. This holds true for other keywords reserved by hava as well.

Reproduction

Example CD:

classdiagram CD1 {
  class Class { }

  class Public { }

  class A { }

  association A -> Public;
  association A -> Class;
}

Generate the corresponding Java sources using CD2Pojo.

Actual behavior

CD2Pojo accepts the class diagram and derives association role names directly from the target class names.

The generated ABuilder contains invalid Java code equivalent to:

protected test.CD1.Public public;

This produces a Java compiler error:

.../ABuilder.java:52: error: <identifier> expected
protected test.CD1.Public public;
                         ^

Expected behavior

CD2Pojo should generate valid Java identifiers when class names result in reserved Java keywords after deriving association role names.

For example, associations targeting Public and Class could result in role names with a generator-defined affix:

protected test.CD1.Public public_;
protected test.CD1.Class class_;

The same transformation must be applied consistently to all generated references, including fields, constructors, accessors, builders, etc. And it should be ensured that the affix is unique, i.e., does not clash with user defined attribute names.

Impact

CDs containing otherwise valid class names can generate Java code that does not compile when associations derive role names from these classes. The error occurs only during Java compilation and does not clearly indicate the responsible CD element.

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