Add operation body

Now we will change the body of the java method so that it uses the body attribute of the operation. In the case where body is empty, we will provide a default implementation.

The first step is to define a defaultMethodBody TGL script on Operation which will return null if the operation has a return type, or return an empty string.

[#script public defaultMethodBody]
    [#if self.hasReturnType]
        return null;
    [/#if]
[/#script]

We then need to create a methodBody MQL Script on Operation that will call the defaultMethodBody script.

public script methodBody() : String {
    if (self.body.length() > 0) {
        return self.body;
    } else {
        return self.defaultMethodBody();
    }
}

The methodBody script is now called from the declaration script on Operation.

[#package tutorial.java]

[#metatype rhapsody.Operation]

[#script public declaration]
    public ${self.returnType} ${self.name}() {
        ${self.methodBody}[#ltrim]
    }

[/#script]

Relaunch the generation and open the file Order.java:

public class Order {

    private RhpString date;

    private Customer customer;
    private java.util.Collection items;

    public LineItem findLineItem(RhpString productName) {
        return null;

    }
}

Next Section: Use the debug hierarchy
Prev Section: Add arguments declaration


Related reference
foreach directive
trim directive