Add methods declaration
Now we want to generate a Java method for each operation defined on a Class:
- Read the Rhapsody model using the Rhapsody Application reader.
- Select the type Class.
- 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.
- Click File > New > Script.
- Type JavaGeneration/src in the Source folder field.
- Type tutorial.java in the Package field.
- Type rhapsody.Operation in the Type field.
- Type declaration in the Name field.
- Click Text in the Language group.
- 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]