UML and SysML to Java - Class Code Generator Specification

This section describes the UML and SysML to Java code generation for structured classes.

This page discusses:

Glossary

Acronym Definition
SPO Software Production Operation
BL Behavior Layer
ML Middleware Layer
BSM Behavior Specific Middleware
TSM Target Specific Middleware

Referenced Documents

Document Internal Reference
Java Code Architecture Specification [1]
UML & SysML to Java - Operation Specification [2]

Document History

Version Date Change Owner Definition
1.0 2025-05-24 A. GAUTHIER First version

Goal of the document

This document describes the detailed requirements of the UML Class code generator involved in the java code generation for a UML Class.

Audience of the document

People who need to understand principles of the code generation for UML Class.

General Concepts

UML & SysML to Java code generator is an operation generating Java code from UML and SysML design. See [2] for more information.

This specification document is more focused on the Java code generation from UML class diagram.

Supported Elements

Here is an exhaustive list of elements used by the code generator to produce the Code with a summary of the purpose and impact.

Element Description
Class Element for which a java file is generated. A Class contains a list of methods and attributes.
Interface Element for which a java file is generated. An Interface declares a list of methods to be implemented.
Enumeration Element for which a java file is generated. Data type used to define a collection of constants.
Association An Association is a relation between 2 classes. Can be directed or bidirectional. Can be public, protected or private
Aggregation An Aggregation is a kind of association with a meaning of containment. Can be directed or bidirectional. Can be public, protected or private
Composition A Composition means that the root class is composed and another class. Can be directed or bidirectional. Can be public, protected or private
Flow Property A Flow Property is a member of an Interface Block referenced by a Proxy Port and representing a data transmitted via that port. Flow Properties are not yet supported
Generalization A Class can inherit from one and only one class via a generalization relation. An Interface can inherit from multiple interfaces.
Interface Realization Relation describing the realization of an interface by classes. A Class can implement multiple interfaces.
Operation There is 2 ways to use the Operation element: - Member of a class representing a method. - Member of an Interface representing an abstract method.
Property A property can be a member of a class representing an attribute. Can be public, protected or private.
Classifier / Behavior A behavior can be a statemachine, an activity or an opaque behavior. The behavior can be in a class itself and attached to an operation. not yet supported
Interface Block Element describing elements transmitted through a Proxy Port element not yet supported

Validation Constraints

N/A

Code Generator Specification

This section is divided in 2 parts:

  • The generated files specification
  • The behavior specification

Files Specifications

Code generator generates one java file for each element which can be a UML class, a UML interface or a UML enumeration:

  • <ClassName>.java
  • <InterfaceName>.java
  • <EnumerationName>.java

General requirements related to file content are described in [1].

<ObjectName>.java

<ClassName>.java, <InterfaceName>.java and <EnumerationName>.java are described in the same way. For the following chapter: ClassName, InterfaceName and EnumerationName are replaced by ObjectName which points to the name of the Class or the Interface or the Enumeration.

<ObjectName>.java file describes the content of a class entity. This file declares and implements the elements associated to a class.

File <ObjectName>.java is generated for the each UML class / UML interface / UML Enumeration element involved in the code generation.

The file name <ObjectName>.java matches the name of the element.

File is generated in a folder reflecting the structure of package of the UML project.

Java folder hierarchy

The package in which the file is generated is declared on top of file after the header.

package root.myPachage;

Example: package declaration

Import of other classes, Interfaces, enumerations on which the current class depends depending on relations - inheritance, composition, aggregation, usage - are generated if any.

import root.otherpackage.LockableElement;

Example: Imports declaration

See chapter Semantic Code Specifications for more details.

The class is described using the regular java declaration mechanism.

The interface is described using the regular java declaration mechanism.

The enumeration is described using the regular java declaration mechanism.

See the dedicated section in chapter Semantic Code Specifications.

/**
 * @generated_from <class uiid>
 */ 
public class LockManager {
    <MEMBERS DECLARATION – See chapter Semantic Code Specifications>
}

Example: Class declaration

/**
 * @generated_from <interface uiid>
*/ 
public interface MyInterface {
    <MEMBERS DECLARATION – See chapter Semantic Code Specifications>
}

Example: Interface declaration

/**
 * @generated_from <enumeration uiid>
*/ 
public enumeration MyEnum {
    <MEMBERS DECLARATION – See chapter Semantic Code Specifications>
}

Example: Enumeration declaration

Semantic Code Specifications

This section describes the behavior of the code generator depending on the UML element.

Package

In Java, a package is a logical grouping of classes, interfaces, and enumerations that are related to each other.

The concept of "visibility of a package" does not exist in Java.

Package declaration

When ObjectName is created within a hierarchy of packages, This hierarchy is declared on the top of the file using the package statement.

package package_name.sub_package_1.sub_package_2;
/**
 * @generated_from <class uuid> 
*/
class A {
}

Example: Declaration of a package hierarchy

Class

If generalization relation is created from a class to another one, then this inheritance relation is reproduced in class declaration.

Class declaration with generalization

/** @generated from: <class uuid> */
public class A extends B {
}

Abstract class

A java class is abstract when it contains at least one abstract method.

Abstract classes cannot be instantiated directly; they serve as blueprints for subclasses to inherit and provide implementations for their abstract methods.

If the isAbstract attribute of the class is set to true, then the class is generated as an abstract class by adding the keyword abstract to the class declaration.

/** @generated from: <class uuid> */
public abstract class A {

  /**Abstract method*/
  public abstract method ();
}

Example: Abstract class

Final class

A final class is a class that cannot be subclassed or extended.

If the isLeaf or isFinalSpecialization attribute of the class is set to true, then the class is generated as a final class by adding the keyword final to the class declaration.

/**
 * @generated_from <class uuid> 
*/
public final class A {
}

Example: Final class

Composition relation (black diamond)

Not directional composition relation

Directional composition relation

[MEMBERS DECLARATION]

If a composition relation is created from a class to another one as directional or not directional, a variable is created in the source class of relation to point to target class.

If a composition relation is created from a class to another one as not directional, a variable is created in the target class of relation to point to the source class.

class A {
    /**
     * @brief Attribute for composition relation to B:b
     * @generated_from <property uuid>
     */
    private B b;
}

Example: Variable in source class for directional and not directional relation

class B {
    /**
     * @brief Attribute for aggregation relation to A:a
     * @generated_from <property uuid>
     */
    private A a;
}

Example: Variable in target class for not directional relation

Aggregation relation (white diamond)

Not directional aggregation relation

[MEMBERS DECLARATION]

If an aggregation relation is created from a class to another one as directional or not directional, a variable is created in the source class of relation to point to target class.

If an aggregation relation is created from a class to another one as not directional, a variable is created in the target class of relation to point to the source class.

class A {
    /**
     * @brief Attribute for association relation to B:b
     * @generated from <property uuid>
     */
    public B b; 
}

Example: Variable in source class

class B {
    /**
     * @brief Attribute for aggregation relation to A:a
     * @generated from <property uuid>
     */
    public A a;
}

Example: Variable in target class for not directional relation

Association relation

Association relation

[MEMBERS DECLARATION]

If an association relation is created between 2 classes, a variable is created in each class to point to the other class.

class A {
    /**
     * @brief Attribute for association relation to B:b
     * @generated from <property uuid>
     */
    public B a;
}

class B {
    /**
     * @brief Attribute for association relation to A:a
     * @generated from <property uuid>   
       */
    public A a;
}

Example: Association between 2 classes

Realization relation

Interface realization

If realization relation is created from a class to one or multiple interfaces, then this implementation relation is reproduced in class declaration.

/** @generated from: <class uuid> */
public class A implements B,C {
}

Example: Class declaration with realization

Interface Realization relation

Interface Realization relation to a specific method is not supported by java. This element is not supported by java code generator.

Substitution relation

Substitution relation is not supported.

Dependency relation

About Dependency relation

Interface

[MEMBERS DECLARATION]

Interface methods are generated without any body.

In Java, interface methods are public and abstract.

/**
 * interface ContactOperations
 * @generated_from <interface uuid>
 */
public interface ContactOperations{
    /**
     * add method
     * @generated_from <operation uuid>
     */
    Contact add(Contact /*in*/ contact);
}

Example: Interface methods declaration

Datatype Enumeration

Enumeration is a special data type that allows a variable to be a set of predefined constants.

Enumeration datatype

[MEMBERS DECLARATION]

Enumeration is generated via the standard Java mechanism to generate enums.

/** 
 * @generated_from <enumeration uuid>
 */
private enum  Group {
    /**
     * @generated_from <litteral uuid>
     */
    FAMILY,
    /**
     * @generated_from <litteral uuid>
     */
    FRIEND,
    /**
     * @generated_from <litteral uuid>
     */
    COLLEAGUE,
    /**
     * @generated_from <litteral uuid>
     */
    UNKNOWN;
}

Example: Enumeration declaration

Property

Property declaration in class

[MEMBERS DECLARATION]

Each property declared in a UML Class is replicated in the generated code.

A brief description is generated for any property describing type and name of the variable.

Additional documentation is generated if the documentation section is field in the model.

Type of the property is used as declared in the model.

Visibility of the property is used as declared in the model.

Default value is set as declared in the model.

/**
 * This is my documentation for attribute
 * @brief Attribute for String:attribute
 * @generated_from <property uuid>
 */
private String attribute = “attr”;

Example: Property

Final declaration

If the isReadOnly attribute of the property is set to true, the property is marked as final, and it must be initialized.

/**
 * @brief Attribute for String:attribute
 * @generated_from <property uuid>
 */
private final String attribute = “attr”;
Static declaration

If the isStatic attribute of the property is set to true, the property is marked as static, it belongs to the class itself, rather than to any specific instance of the class, it can be accessed directly using the class name.

/**
 * @brief Attribute for String:attribute
 * @generated_from <property uuid>
 */
private static String attribute;
Array declaration
Example: Property with multiplicity 10

If the multiplicity corresponds to a finite number or an interval of finite numbers, an array is created with a length equal to the upper value.

/**
 * @generated_from <property uuid>
 */
public int[] number = new int [10];

Example: Array declaration

List declaration
Property with "*" multiplicity

If the multiplicity is equal to "*", isOrdered and isUnique attributes are not set or set to false, a collection List is created.

If the multiplicity is equal to "*" if isOrdered is set to true and isOrdered is set to false or not set, a collection List is created.

If a List is used according to above rules, import of java.util.List is added in import section.

/**
 * @generated_from <property uuid>
 */
public List<Integer> number;

Example: List declaration

Set declaration

If the multiplicity is equal to "*", isOrdered is set to false or not set and isUnique is set to true, a collection Set is created.

If a Set is used according to above rules, import of java.util.Set is added in import section.

/**
 * @generated_from <property uuid>
 */
public Set<Integer> number;

Example: Set declaration

Sorted Set declaration
N/A

Operation

[MEMBERS DECLARATION]

Operation declaration in class

Each operation declared in a UML Class or UML Interface is generated within their body.

A brief description is generated for any property describing type and name of the variable.

Additional documentation is generated if the documentation section is field in the model.

Visibility of the operation is used as declared in the model.

In the context of an UML Class, user code section is inserted in operation to let the user fills his behavior.

If any, a default return code is inserted in the user code section.

/**
 * @generated_from <operation uuid>
 */
public boolean lockDoors(){
    /* BEG_USER_CODE<operation uuid>) do not remove */
    // Add your code here
    return true;
    /* END_USER_CODE(<operation uuid>) do not remove */
    }

Example: Operation

Constructor

Constructor methods are only generated when explicitly declared in UML Class or UML Interface.

If an operation has the same name as the class, it is considered a constructor of the class.

If operation parameters have the same name as the class attributes, the assignement of the value is done automatically.

class Door{
    private String attr;
    /**
     * @brief Constructor of Door class
     * @generated from  <operation uuid>
     */ 
    public Door (String attr) {
        this.attr=attr;
        /* BEG_USER_CODE(<operation uuid>) do not remove */
        // Add your code here
        /* END_USER_CODE(<operation uuid>) do not remove */
    }
}

Example: Constructor

If the constructor contains parameters that are not among the attributes of the class, the user can implement their own logic inside it.

public class Door{
    private String attr;
    /**
    * @brief Constructor
    * @generated_from < operation uuid>
    */
    public Door (int a) {
        /* BEG_USER_CODE(<operation uuid>) do not remove */
        /* Add your code here */
        /* END_USER_CODE(<operation uuid>) do not remove */
    }
}

Example: Constructor with different parameters

Getter & Setter

In Java it is recommended to write getters and setters for all attributes, regardless of their visibility levels (private, protected, or public) for Encapsulation, compatibility, flexibility and Maintenance.

Getter and setter operations are generated for each attribute independently of their visibility except for attributes having isLeaf set to true.

Only Getter operation is generated for attributes with isLeaf set to true independently of their visibility except for final attributes.

public class Door {
    private String attr;
    /**
    * Getter for attribute attr
    * @generated_from <property uuid>
    */
    public  String getAttr (){
        return attr;
    }
    /**
     * Setter for attribute attr
     * @generated_from <property uuid>
     */
    public  void setAttr(String attr){
        this.attr = attr;
    }
}

Example: Getter and Setter

Static declaration

If the isStatic attribute is set to true for an operation, then the method is generated as a static method.

/**
 * @generated_from <operation uuid>
 */
static void function() {
    /* BEG_USER_CODE(<operation uuid>) do not remove */
    /* Add your code here */
    /* END_USER_CODE(<operation uuid>) do not remove */
}

Example: Static declaration

Final declaration

If the isLeaf attribute is set to true for a method, then the method is generated as a final function.

/**
 * @generated_from <operation uuid>
 */
final void function() {
    /* BEG_USER_CODE(<operation uuid>) do not remove */
    /* Add your code here */
    /* END_USER_CODE(<operation uuid>) do not remove */
}

Example: Final declaration

Abstract declaration

An abstract method is a method declared without any implementation.

If the isAbstract attribute of the operation is set to true, method is generated as abstract by adding the keyword abstract to the class declaration.

/**
 * @generated_from <operation uuid>
 */
abstract void function();

Example: Abstract method

Exception

Exception not yet supported. Add a validation rule.

Operation behavior

If an operation has an Opaque behavior assigned to it, then this opaque behavior is replicated in the operation body. Not yet supported

void OperationWithBeh(){
    /* BEG_USER_CODE(<operation uuid>) do not remove */
    MyClass c;
    c.privateOp();
    /* END_USER_CODE(<operation uuid>) do not remove */    
}

Example: Operation with behavior

Parameter

Type of parameter of operation with direction set as "return" is used as return type of the operation.

Type of the parameter is used as declared in the model.

The parameter of operation with the direction set as "in", "out", or "inout" is used as a parameter of the operation.

Direction "in" or "out" or "inout" of parameter is used for documentation purpose.

Note that parameters direction do not have any meaning in Java.

Note that parameters do not support Visibility.

/**
 * @generated_from <operation uuid>
 */
public boolean setWindowPosition(ControlEnum /*in*/ windowPosition){
}

Example: Parameters

Array declaration
Parameter with finite multiplicity

If the multiplicity corresponds to a finite number or an interval of finite numbers, an array is created.

/**
 * @generated_from <operation uuid>
*/
public double calculate(int[] /*in*/ values ){}

Example: Array declaration

List declaration
Parameter with "*" multiplicity

If the multiplicity is equal to "*", isOrdered and isUnique attributes are not set or set to false, a collection List is created.

If the multiplicity is equal to "*" if isOrdered is set to true and isOrdered is set to false or not set, a collection List is created.

If a List is used according to above rules, import of java.util.List is added in import section.

/**
 * @generated_from <operation uuid>
 */
public double calculate(List<Integer> /*in*/ a){}

Example: List declaration

Set declaration

If the multiplicity is equal to "*", isOrdered is set to false or not set and isUnique is set to true, a collection Set is created.

If a Set is used according to above rules, import of java.util.Set is added in import section.

/**
 * @generated_from <operation uuid>
*/
Set<HistoryEntry> getHistoryRange(int /*in*/ from, int /*in*/ to);

Example: Parameter as set