Generated Java Code for Classes

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

This page discusses:

Table 1. Legend

These tables use the following icons:

Icon Meaning
Supported
Not supported
Not applicable

Package

Element Status Description
Package declaration

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

Example of a package declaration:

Package hierarchy

When <ObjectName>.java is created within a hierarchy of packages, this hierarchy is declared on the top of the file using the package statement.

Example of a package hierarchy declaration:

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

Package visibility

Not applicable.

The package visibility concept does not exist in Java.

Class

Element Status Description
Class declaration

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

Example of a class declaration with generalization:

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

Static nested class

Not supported.

A static nested class is a class that is declared inside another class with a static keyword.

A nested class is associated with an instance of the outer class and has access to its instance variables and methods.

Example of a nested class:

/** @generated from: <class uuid> */
public class OuterClass {
    class InnerClass {
    }
}

Local nested class

Not supported.

A local nested class is a class that is declared inside a method.

A local class is defined within a block of code of a method body.

Example of a local class:

/** @generated from: <class uuid> */
public class OuterClass {
    void outerMethod() {
        class LocalClass {
        }
    }
}

Anonymous nested class

Not supported.

An anonymous class is a class without any defined name and instantiated in a single expression.

Example of an anonymous class:

/** @generated from: <class uuid> */
 public class OuterClass {
    interface Interface {
        void method();
    }

    Interface myObject = new Interface() {
        public void method() {
            // code of the anonymous class
        }
    };
}

Abstract class

A Java class is an 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.

Example of an abstract class:

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

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

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.

Example of a final class:

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

Relations

Table 2. MEMBERS DECLARATION
Element Status Description
Composition relation

(represented with a black diamond arrow)

Example of a directional composition relation:

Example of a not directional composition relation:

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

    Example of a variable in a source class for directional and not directional relation:

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

    Example of a variable in a target class for not directional relation:

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

Aggregation relation

(represented with a white diamond arrow)

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

    Example of a variable in a source class for directional and not directional relation:

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

  • Example of a not directional aggregation relation:

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

    Example of a variable in a target class for a not directional relation:

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

Association relation

Example of an association relation:

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

Example of an association between two classes:

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;
}

Realization relation

Example of an interface realization:

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

Example of a class declaration with realization:

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

Interface realization relation The interface realization relation to a specific method is not supported by Java.
Substitution relation

Not supported.

The substitution relation is not supported
Dependency relation

Not supported.

When there is a dependency relationship between two objects (UML classes or UML interfaces), an import statement for to the targeted object by the relation is generated in the source object file.

Interface

Table 3. MEMBERS DECLARATION
Element Status Description
Interface declaration

The interface methods are generated without any body.

In Java, interface methods are public and abstract.

Example of interface methods declaration:

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

Datatype: Enumeration

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

Example of an enumeration datatype

Table 4. MEMBERS DECLARATION
Element Status Description
Enumeration declaration

An enumeration is generated by the standard Java mechanism to generate enum.

Example of an enumeration declaration:

/** 
* @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;
}

Property

Example of a property declaration in a class:

Table 5. MEMBERS DECLARATION
Element Status Description
Property declaration

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

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

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

The type of the property is used as declared in the model.

The visibility of the property is used as declared in the model.

The default value is set as declared in the model.

Example of a property:

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

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 of a property with [10] multiplicity

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.

Example of an array declaration:

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

List declaration

Example of a property with “*” multiplicity:

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

A collection list is created if the following rules are applied:

  • The multiplicity is equal to "*"
  • isOrdered is set to true, to false or not set,

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

Example of a list declaration:

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

Set declaration

A collection set is created if the following rules are applied:

  • The multiplicity is equal to "*"
  • isOrdered is set to false or not set
  • isUnique is set to true

If a set is used according to the rules listed above, the import of “java.util.Set” is added in the import section.

Example of a set declaration:

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

Sorted set declaration

Not supported

A collection SortedSet is created if the following rules are applied:

  • The multiplicity is equal to "*"
  • isOrdered is set to true
  • isUnique is set to true

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

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

Operation

Example of an operation declaration in a class:

Table 6. MEMBERS DECLARATION
Element Status Description
Operation declaration

Each operation declared in a UML class or a 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 filled in the model.

The visibility of the operation is used as declared in the model.

In the context of a UML class, a user code section is inserted in the operation to let the user fill its behavior.

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

Example of an operation:

  /**
   * @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 */
  }

Constructor

Constructor methods are only generated when explicitly declared in a UML class or a UML interface.

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

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

Example of a constructor

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 */
  }
}

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

Example of a constructor with different parameters:

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 */
  }
}

Getter and 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 those attributes having isLeaf set to true.

The getter operation only is generated for attributes with isLeaf set to true, independently of their visibility, except for final attributes.

Example of a getter and setter:

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;
  }
}

Static declaration

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

Example of a static:

/**
 * @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 */
}

Final declaration

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

Example of a final:

/**
 * @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 */
}

Abstract declaration

An abstract method is a method declared without any implementation.

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

Exception Not supported
Operation behavior

Not supported

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

Example of an operation with behavior:

  void OperationWithBeh(){

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

If an operation has an activity assigned to it, then this activity should be invoked in the operation body.

Parameter

Element Status Description
Parameter declaration

The parameter type of an operation with its direction set as return is used as the return type of the operation.

The type of the parameter is used as declared in the model.

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

The direction “in” or “out” or “inout” of the parameter is used for documentation purpose.

Example of a parameter:

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

Direction

Java parameters does not support direction.

Visibility

Java parameters do not support visibility.

Array declaration Example of a parameter with finite multiplicity:

List declaration Example of a parameter with “*” multiplicity:

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

A collection list is created if the following rules are applied:

  • The multiplicity is equal to "*"
  • isOrdered is set to true
  • isUnique is set to false or not set

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

Example of a list declaration:

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

Set declaration

Not supported

A collection set is created if the following rules are applied:

  • The multiplicity is equal to "*",
  • isOrdered is set to false or not set
  • isUnique is set to true,

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

Example of a parameter as set:

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

Sorted set declaration

Not supported.

A collection SortedSet is created is created if the following rules are applied:

  • The multiplicity is equal to "*"
  • isOrdered is set to true
  • isUnique is set to true

If a SortedSet is used according to the rules above, the import of “java.util.SortedSet” is added in the import section.

Example of a parameter as set:

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