Add methods declaration

Now we want to generate a Java method for each operation defined on a Class:

  1. Read the Rhapsody model using the Rhapsody Application reader.
  2. Select the type Class.
  3. Select the Class Order and show its children.

You can see the Class Order has an operation findLineItem linked through the reference operations.

We will start by adding a declaration script on operation to generate the declaration for an operation.

  1. Click File > New > Script.
  2. Type JavaGeneration/src in the Source folder field.
  3. Type tutorial.java in the Package field.
  4. Type rhapsody.Operation in the Type field.
  5. Type declaration in the Name field.
  6. Click Text in the Language group.

  7. Click Finish.

A file rhapsody_Operation.tgs is created that allows us to define TGL scripts on the metatype Operation. Change its contents to:

[#package tutorial.java]

[#metatype rhapsody.Operation]

[#script public declaration]
    public Object ${self.name}() {
        return null;
    }
    
[/#script]

The declaration script can now be called from the main template.

[#package tutorial.java]

[#template public JavaSource(class : rhapsody.Class)]
[#file]generated/${class.name}.java[/#file]
public class ${class.name} {

[#-- Attributes declaration --]
[#foreach attr : rhapsody.Attribute in class.attributes]
    private ${attr.type.name} ${attr.name};
[/#foreach]

[#-- Relations declaration --]
[#foreach rel : rhapsody.Relation in class.relations]
    private ${rel.javaType} ${rel.name};
[/#foreach]

[#-- Operations declaration --]
[#foreach operation : rhapsody.Operation in class.operations]
    ${operation.declaration}[#trim]
[/#foreach]
}
[/#template]