Class JavaStatementBuilder

java.lang.Object
com.regnosys.rosetta.generator.java.statement.builder.JavaStatementBuilder
Direct Known Subclasses:
JavaBlockBuilder, JavaConditionalExpression, JavaExpression, JavaIfThenElseBuilder

public abstract class JavaStatementBuilder extends Object
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 Details

    • JavaStatementBuilder

      public JavaStatementBuilder()
  • Method Details

    • getExpressionType

      public abstract JavaType 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

      public abstract JavaStatement complete(Function<JavaExpression,JavaStatement> completer)
      Complete this statement builder by mapping all expressions to a statement.
    • completeAsReturn

      public abstract JavaStatement completeAsReturn()
      Complete this statement builder by returning all expressions.
    • completeAsExpressionStatement

      public abstract JavaStatement completeAsExpressionStatement()
      Complete this statement builder by ending all expressions with a semicolon.
    • completeAsAssignment

      public abstract JavaStatement completeAsAssignment(GeneratedIdentifier variableId)
      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. See mapExpression(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

      public abstract JavaStatementBuilder collapseToSingleExpression(JavaScope scope)
      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

      Append another statement builder to this one, and combine this expression with the other using the given operation.
    • toLambdaBody

      public abstract JavaLambdaBody toLambdaBody()
      Convert this statement builder into a valid lambda body.