Class ASTFactory


  • public class ASTFactory
    extends Object
    Factory for building source AST expressions and statements programmatically.
    • Constructor Detail

      • ASTFactory

        public ASTFactory()
    • Method Detail

      • intLiteral

        public LiteralExpr intLiteral​(int value)
        Creates an integer literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • longLiteral

        public LiteralExpr longLiteral​(long value)
        Creates a long literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • floatLiteral

        public LiteralExpr floatLiteral​(float value)
        Creates a float literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • doubleLiteral

        public LiteralExpr doubleLiteral​(double value)
        Creates a double literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • boolLiteral

        public LiteralExpr boolLiteral​(boolean value)
        Creates a boolean literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • charLiteral

        public LiteralExpr charLiteral​(char value)
        Creates a character literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • stringLiteral

        public LiteralExpr stringLiteral​(String value)
        Creates a string literal.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • nullLiteral

        public LiteralExpr nullLiteral()
        Creates a null literal.
        Returns:
        the literal expression
      • variable

        public VarRefExpr variable​(String name,
                                   SourceType type)
        Creates a variable reference.
        Parameters:
        name - the variable name
        type - the declared type
        Returns:
        the reference expression
      • variable

        public VarRefExpr variable​(String name)
        Creates a variable reference typed as java.lang.Object.
        Parameters:
        name - the variable name
        Returns:
        the reference expression
      • fieldAccess

        public FieldAccessExpr fieldAccess​(Expression target,
                                           String fieldName,
                                           String ownerClass,
                                           SourceType type)
        Creates an instance field access.
        Parameters:
        target - the receiver expression
        fieldName - the field name
        ownerClass - the internal name of the declaring class
        type - the field type
        Returns:
        the field access expression
      • staticField

        public FieldAccessExpr staticField​(String ownerClass,
                                           String fieldName,
                                           SourceType type)
        Creates a static field access.
        Parameters:
        ownerClass - the internal name of the declaring class
        fieldName - the field name
        type - the field type
        Returns:
        the field access expression
      • arrayAccess

        public ArrayAccessExpr arrayAccess​(Expression array,
                                           Expression index,
                                           SourceType componentType)
        Creates an array access expression.
        Parameters:
        array - the array expression
        index - the index expression
        componentType - the type of the accessed element
        Returns:
        the array access expression
      • methodCall

        public ASTFactory.MethodCallBuilder methodCall​(String methodName)
        Starts a method call for incremental configuration.
        Parameters:
        methodName - the method name
        Returns:
        the builder
      • staticCall

        public MethodCallExpr staticCall​(String ownerClass,
                                         String methodName,
                                         SourceType returnType,
                                         Expression... args)
        Creates a static method call.
        Parameters:
        ownerClass - the internal name of the declaring class
        methodName - the method name
        returnType - the return type
        args - the argument expressions
        Returns:
        the call expression
      • instanceCall

        public MethodCallExpr instanceCall​(Expression receiver,
                                           String ownerClass,
                                           String methodName,
                                           SourceType returnType,
                                           Expression... args)
        Creates an instance method call.
        Parameters:
        receiver - the receiver expression
        ownerClass - the internal name of the declaring class
        methodName - the method name
        returnType - the return type
        args - the argument expressions
        Returns:
        the call expression
      • newInstance

        public ASTFactory.NewExprBuilder newInstance​(String className)
        Starts an allocation for incremental configuration.
        Parameters:
        className - the class to allocate, in dotted or internal form
        Returns:
        the builder
      • newExpr

        public NewExpr newExpr​(String className,
                               Expression... args)
        Creates an allocation expression.
        Parameters:
        className - the class to allocate
        args - the constructor argument expressions
        Returns:
        the allocation expression
      • newArray

        public NewArrayExpr newArray​(SourceType elementType,
                                     Expression size)
        Creates a sized array allocation.
        Parameters:
        elementType - the element type
        size - the length expression
        Returns:
        the allocation expression
      • newArray

        public NewArrayExpr newArray​(String typeName,
                                     Expression size)
        Creates a sized array allocation, parsing the element type from its name.
        Parameters:
        typeName - the element type name
        size - the length expression
        Returns:
        the allocation expression
      • binary

        public BinaryExpr binary​(Expression left,
                                 BinaryOperator op,
                                 Expression right,
                                 SourceType type)
        Creates a binary expression.
        Parameters:
        left - the left operand
        op - the operator
        right - the right operand
        type - the result type
        Returns:
        the binary expression
      • add

        public BinaryExpr add​(Expression left,
                              Expression right,
                              SourceType type)
        Creates an addition expression.
        Parameters:
        left - the left operand
        right - the right operand
        type - the result type
        Returns:
        the binary expression
      • subtract

        public BinaryExpr subtract​(Expression left,
                                   Expression right,
                                   SourceType type)
        Creates a subtraction expression.
        Parameters:
        left - the left operand
        right - the right operand
        type - the result type
        Returns:
        the binary expression
      • multiply

        public BinaryExpr multiply​(Expression left,
                                   Expression right,
                                   SourceType type)
        Creates a multiplication expression.
        Parameters:
        left - the left operand
        right - the right operand
        type - the result type
        Returns:
        the binary expression
      • divide

        public BinaryExpr divide​(Expression left,
                                 Expression right,
                                 SourceType type)
        Creates a division expression.
        Parameters:
        left - the left operand
        right - the right operand
        type - the result type
        Returns:
        the binary expression
      • equals

        public BinaryExpr equals​(Expression left,
                                 Expression right)
        Creates an equality comparison typed boolean.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • notEquals

        public BinaryExpr notEquals​(Expression left,
                                    Expression right)
        Creates an inequality comparison typed boolean.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • lessThan

        public BinaryExpr lessThan​(Expression left,
                                   Expression right)
        Creates a less-than comparison typed boolean.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • greaterThan

        public BinaryExpr greaterThan​(Expression left,
                                      Expression right)
        Creates a greater-than comparison typed boolean.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • and

        public BinaryExpr and​(Expression left,
                              Expression right)
        Creates a logical AND expression typed boolean.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • or

        public BinaryExpr or​(Expression left,
                             Expression right)
        Creates a logical OR expression typed boolean.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • assign

        public BinaryExpr assign​(Expression left,
                                 Expression right,
                                 SourceType type)
        Creates an assignment expression.
        Parameters:
        left - the assignment target
        right - the assigned value
        type - the result type
        Returns:
        the binary expression
      • unary

        public UnaryExpr unary​(UnaryOperator op,
                               Expression operand,
                               SourceType type)
        Creates a unary expression.
        Parameters:
        op - the operator
        operand - the operand
        type - the result type
        Returns:
        the unary expression
      • negate

        public UnaryExpr negate​(Expression operand,
                                SourceType type)
        Creates an arithmetic negation.
        Parameters:
        operand - the operand
        type - the result type
        Returns:
        the unary expression
      • not

        public UnaryExpr not​(Expression operand)
        Creates a logical NOT typed boolean.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • preIncrement

        public UnaryExpr preIncrement​(Expression operand,
                                      SourceType type)
        Creates a pre-increment expression.
        Parameters:
        operand - the operand
        type - the result type
        Returns:
        the unary expression
      • postIncrement

        public UnaryExpr postIncrement​(Expression operand,
                                       SourceType type)
        Creates a post-increment expression.
        Parameters:
        operand - the operand
        type - the result type
        Returns:
        the unary expression
      • cast

        public CastExpr cast​(SourceType targetType,
                             Expression expr)
        Creates a cast expression.
        Parameters:
        targetType - the type cast to
        expr - the operand
        Returns:
        the cast expression
      • cast

        public CastExpr cast​(String typeName,
                             Expression expr)
        Creates a cast expression, parsing the target from its name.
        Parameters:
        typeName - the target type name
        expr - the operand
        Returns:
        the cast expression
      • instanceOf

        public InstanceOfExpr instanceOf​(Expression expr,
                                         SourceType checkedType)
        Creates an instanceof test.
        Parameters:
        expr - the operand
        checkedType - the type tested against
        Returns:
        the instanceof expression
      • instanceOf

        public InstanceOfExpr instanceOf​(Expression expr,
                                         String typeName)
        Creates an instanceof test, parsing the tested type from its name.
        Parameters:
        expr - the operand
        typeName - the tested type name
        Returns:
        the instanceof expression
      • ternary

        public TernaryExpr ternary​(Expression condition,
                                   Expression thenExpr,
                                   Expression elseExpr,
                                   SourceType type)
        Creates a ternary expression.
        Parameters:
        condition - the selecting condition
        thenExpr - the value when the condition holds
        elseExpr - the value otherwise
        type - the result type
        Returns:
        the ternary expression
      • exprStmt

        public ExprStmt exprStmt​(Expression expr)
        Wraps an expression as a statement.
        Parameters:
        expr - the expression to evaluate for effect
        Returns:
        the statement
      • returnStmt

        public ReturnStmt returnStmt​(Expression value)
        Creates a return statement with a value.
        Parameters:
        value - the returned expression
        Returns:
        the statement
      • returnVoid

        public ReturnStmt returnVoid()
        Creates a valueless return statement.
        Returns:
        the statement
      • throwStmt

        public ThrowStmt throwStmt​(Expression exception)
        Creates a throw statement.
        Parameters:
        exception - the thrown expression
        Returns:
        the statement
      • block

        public BlockStmt block​(Statement... stmts)
        Creates a block statement.
        Parameters:
        stmts - the block contents, in order
        Returns:
        the statement
      • block

        public BlockStmt block​(List<Statement> stmts)
        Creates a block statement.
        Parameters:
        stmts - the block contents, in order
        Returns:
        the statement
      • ifStmt

        public IfStmt ifStmt​(Expression condition,
                             Statement thenBranch)
        Creates an if statement without an else arm.
        Parameters:
        condition - the guard
        thenBranch - the guarded statement
        Returns:
        the statement
      • ifElseStmt

        public IfStmt ifElseStmt​(Expression condition,
                                 Statement thenBranch,
                                 Statement elseBranch)
        Creates an if statement with an else arm.
        Parameters:
        condition - the guard
        thenBranch - the statement run when the guard holds
        elseBranch - the statement run otherwise
        Returns:
        the statement
      • varDecl

        public VarDeclStmt varDecl​(SourceType type,
                                   String name,
                                   Expression initializer)
        Creates an initialized local variable declaration.
        Parameters:
        type - the declared type
        name - the variable name
        initializer - the initial value
        Returns:
        the statement
      • varDecl

        public VarDeclStmt varDecl​(String typeName,
                                   String name,
                                   Expression initializer)
        Creates an initialized local variable declaration, parsing the type from its name.
        Parameters:
        typeName - the declared type name
        name - the variable name
        initializer - the initial value
        Returns:
        the statement
      • varDecl

        public VarDeclStmt varDecl​(SourceType type,
                                   String name)
        Creates an uninitialized local variable declaration.
        Parameters:
        type - the declared type
        name - the variable name
        Returns:
        the statement
      • parseType

        public SourceType parseType​(String typeName)
        Parses a type from a string representation.
        Parameters:
        typeName - the type name to parse
        Returns:
        the parsed source type