Class JavaStatementBuilder
java.lang.Object
com.regnosys.rosetta.generator.java.statement.builder.JavaStatementBuilder
- Direct Known Subclasses:
JavaBlockBuilder,JavaConditionalExpression,JavaExpression,JavaIfThenElseBuilder
A convenient API for building a Java statement.
In general, a statement builder consists of a list of statements, ending with one or more expressions.
The builder can be "completed" into a Java statement by converting the ending expression(s) into
a valid Java statement, e.g., by returning the expressions or assigning them to a variable.
The simples statement builder is a single `JavaExpression`.
Usage:
*Example 1*
Say we want to build the following block:
```
{
int x = 42;
return x;
}
```
This can be done using the following Xtend code:
```
JavaExpression.from('''42''', JavaPrimitiveType.INT)
.declareAsVariable(true, "x", scope)
.completeAsReturn
```
*Example 2*
Say we want to build the following block:
```
{
int ifThenElseResult;
if (foo == bar) {
ifThenElseResult = compute(42);
} else {
ifThenElseResult = compute(0);
}
return ifThenElseResult + 1;
}
```
This can be done using the following Xtend code:
```
JavaExpression.from('''foo == bar''', JavaPrimitiveType.BOOLEAN)
.mapExpression[
new JavaIfThenElseBuilder(
it,
JavaExpression.from('''42''', JavaPrimitiveType.INT),
JavaExpression.from('''0''', JavaPrimitiveType.INT)
)
]
.mapExpression[
JavaExpression.from('''compute(«it»)''', JavaPrimitiveType.INT)
]
.collapseToSingleExpression
.mapExpression[
JavaExpression.from('''«it» + 1''', JavaPrimitiveType.INT)
]
.completeAsReturn
```
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract JavaStatementBuilderIf this statement builder ends with multiple branches, assign all expressions to a new variable and return a new builder ending with that variable.abstract JavaStatementcomplete(Function<JavaExpression, JavaStatement> completer) Complete this statement builder by mapping all expressions to a statement.abstract JavaStatementcompleteAsAssignment(GeneratedIdentifier variableId) Complete this statement builder by assigning all expressions to a given variable.abstract JavaStatementComplete this statement builder by ending all expressions with a semicolon.abstract JavaStatementComplete this statement builder by returning all expressions.abstract JavaStatementBuilderdeclareAsVariable(boolean isFinal, String variableId, JavaScope scope) Assign all expressions to a new variable, and return a new builder ending with that variable.abstract JavaTypeGet the type of the last expression of this builder, or the least common supertype of all expressions in different branches of this builder.abstract JavaStatementBuildermapExpression(Function<JavaExpression, ? extends JavaStatementBuilder> mapper) Map all expressions contained in this builder to a new builder, and append the result.mapExpressionIfNotNull(Function<JavaExpression, ? extends JavaStatementBuilder> mapper) Map all non-null expressions contained in this builder to a new builder, and append the result.abstract JavaStatementBuilderthen(JavaStatementBuilder after, BiFunction<JavaExpression, JavaExpression, JavaStatementBuilder> combineExpressions, JavaScope scope) Append another statement builder to this one, and combine this expression with the other using the given operation.abstract JavaLambdaBodyConvert this statement builder into a valid lambda body.
-
Constructor Details
-
JavaStatementBuilder
public JavaStatementBuilder()
-
-
Method Details
-
getExpressionType
Get the type of the last expression of this builder, or the least common supertype of all expressions in different branches of this builder. -
complete
Complete this statement builder by mapping all expressions to a statement. -
completeAsReturn
Complete this statement builder by returning all expressions. -
completeAsExpressionStatement
Complete this statement builder by ending all expressions with a semicolon. -
completeAsAssignment
Complete this statement builder by assigning all expressions to a given variable. -
mapExpression
public abstract JavaStatementBuilder mapExpression(Function<JavaExpression, ? extends JavaStatementBuilder> mapper) Map all expressions contained in this builder to a new builder, and append the result. This will flatten Java block statements. -
mapExpressionIfNotNull
public JavaStatementBuilder mapExpressionIfNotNull(Function<JavaExpression, ? extends JavaStatementBuilder> mapper) Map all non-null expressions contained in this builder to a new builder, and append the result. SeemapExpression(Function). -
declareAsVariable
public abstract JavaStatementBuilder declareAsVariable(boolean isFinal, String variableId, JavaScope scope) Assign all expressions to a new variable, and return a new builder ending with that variable. -
collapseToSingleExpression
If this statement builder ends with multiple branches, assign all expressions to a new variable and return a new builder ending with that variable. If this statement builder does not end in multiple branches, return itself unmodified. -
then
public abstract JavaStatementBuilder then(JavaStatementBuilder after, BiFunction<JavaExpression, JavaExpression, JavaStatementBuilder> combineExpressions, JavaScope scope) Append another statement builder to this one, and combine this expression with the other using the given operation. -
toLambdaBody
Convert this statement builder into a valid lambda body.
-