Class ASTFactory


  • public final class ASTFactory
    extends Object
    Static factory of AST nodes with sensible type defaults.
    • Method Detail

      • intLit

        public static LiteralExpr intLit​(int value)
        Builds an int literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • longLit

        public static LiteralExpr longLit​(long value)
        Builds a long literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • floatLit

        public static LiteralExpr floatLit​(float value)
        Builds a float literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • doubleLit

        public static LiteralExpr doubleLit​(double value)
        Builds a double literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • boolLit

        public static LiteralExpr boolLit​(boolean value)
        Builds a boolean literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • charLit

        public static LiteralExpr charLit​(char value)
        Builds a char literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • stringLit

        public static LiteralExpr stringLit​(String value)
        Builds a String literal node.
        Parameters:
        value - the literal value
        Returns:
        the literal expression
      • nullLit

        public static LiteralExpr nullLit()
        Builds a null literal node.
        Returns:
        the null literal expression
      • varRef

        public static VarRefExpr varRef​(String name,
                                        SourceType type)
        Builds a variable reference node.
        Parameters:
        name - the variable name
        type - the variable type
        Returns:
        the variable reference
      • intVar

        public static VarRefExpr intVar​(String name)
        Builds an int-typed variable reference node.
        Parameters:
        name - the variable name
        Returns:
        the variable reference
      • boolVar

        public static VarRefExpr boolVar​(String name)
        Builds a boolean-typed variable reference node.
        Parameters:
        name - the variable name
        Returns:
        the variable reference
      • objectVar

        public static VarRefExpr objectVar​(String name,
                                           String className)
        Builds a reference-typed variable reference node.
        Parameters:
        name - the variable name
        className - the class of the variable's type
        Returns:
        the variable reference
      • binary

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

        public static BinaryExpr add​(Expression left,
                                     Expression right)
        Builds an addition node typed from the left operand.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • sub

        public static BinaryExpr sub​(Expression left,
                                     Expression right)
        Builds a subtraction node typed from the left operand.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • mul

        public static BinaryExpr mul​(Expression left,
                                     Expression right)
        Builds a multiplication node typed from the left operand.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • div

        public static BinaryExpr div​(Expression left,
                                     Expression right)
        Builds a division node typed from the left operand.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • mod

        public static BinaryExpr mod​(Expression left,
                                     Expression right)
        Builds a modulo node typed from the left operand.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • eq

        public static BinaryExpr eq​(Expression left,
                                    Expression right)
        Builds a boolean equality comparison node.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • ne

        public static BinaryExpr ne​(Expression left,
                                    Expression right)
        Builds a boolean inequality comparison node.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • lt

        public static BinaryExpr lt​(Expression left,
                                    Expression right)
        Builds a boolean less-than comparison node.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • le

        public static BinaryExpr le​(Expression left,
                                    Expression right)
        Builds a boolean less-or-equal comparison node.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • gt

        public static BinaryExpr gt​(Expression left,
                                    Expression right)
        Builds a boolean greater-than comparison node.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • ge

        public static BinaryExpr ge​(Expression left,
                                    Expression right)
        Builds a boolean greater-or-equal comparison node.
        Parameters:
        left - the left operand
        right - the right operand
        Returns:
        the binary expression
      • and

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

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

        public static BinaryExpr assign​(Expression left,
                                        Expression right)
        Builds an assignment expression node typed from the target.
        Parameters:
        left - the assignment target
        right - the assigned value
        Returns:
        the binary expression
      • unary

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

        public static UnaryExpr not​(Expression operand)
        Builds a logical negation node.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • neg

        public static UnaryExpr neg​(Expression operand)
        Builds an arithmetic negation node typed from the operand.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • preIncr

        public static UnaryExpr preIncr​(Expression operand)
        Builds a pre-increment node typed from the operand.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • preDecr

        public static UnaryExpr preDecr​(Expression operand)
        Builds a pre-decrement node typed from the operand.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • postIncr

        public static UnaryExpr postIncr​(Expression operand)
        Builds a post-increment node typed from the operand.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • postDecr

        public static UnaryExpr postDecr​(Expression operand)
        Builds a post-decrement node typed from the operand.
        Parameters:
        operand - the operand
        Returns:
        the unary expression
      • ternary

        public static TernaryExpr ternary​(Expression condition,
                                          Expression thenExpr,
                                          Expression elseExpr)
        Builds a ternary conditional node typed from the then branch.
        Parameters:
        condition - the condition
        thenExpr - the value when true
        elseExpr - the value when false
        Returns:
        the ternary expression
      • cast

        public static CastExpr cast​(SourceType targetType,
                                    Expression expr)
        Builds a cast node.
        Parameters:
        targetType - the type cast to
        expr - the expression being cast
        Returns:
        the cast expression
      • instanceOf

        public static InstanceOfExpr instanceOf​(Expression expr,
                                                SourceType checkType)
        Builds an instanceof test node.
        Parameters:
        expr - the tested expression
        checkType - the type tested against
        Returns:
        the instanceof expression
      • instanceOf

        public static InstanceOfExpr instanceOf​(Expression expr,
                                                SourceType checkType,
                                                String patternVar)
        Builds an instanceof test node with a pattern variable.
        Parameters:
        expr - the tested expression
        checkType - the type tested against
        patternVar - the binding variable name
        Returns:
        the instanceof expression
      • arrayAccess

        public static ArrayAccessExpr arrayAccess​(Expression array,
                                                  Expression index,
                                                  SourceType elementType)
        Builds an array element access node.
        Parameters:
        array - the array expression
        index - the index expression
        elementType - the element type
        Returns:
        the array access expression
      • newObj

        public static NewExpr newObj​(String className,
                                     Expression... args)
        Builds an object instantiation node.
        Parameters:
        className - the class being instantiated
        args - the constructor arguments
        Returns:
        the new expression
      • newArray

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

        public static ArrayInitExpr arrayInit​(SourceType elementType,
                                              Expression... elements)
        Builds an array initializer node.
        Parameters:
        elementType - the element type
        elements - the initial elements
        Returns:
        the array initializer expression
      • thisExpr

        public static ThisExpr thisExpr​(SourceType type)
        Builds a this reference node.
        Parameters:
        type - the enclosing class type
        Returns:
        the this expression
      • classExpr

        public static ClassExpr classExpr​(SourceType classType)
        Builds a class literal node.
        Parameters:
        classType - the type whose class is referenced
        Returns:
        the class expression
      • fieldAccess

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

        public static FieldAccessExpr staticField​(String ownerClass,
                                                  String fieldName,
                                                  SourceType type)
        Builds a static field access node.
        Parameters:
        ownerClass - the class declaring the field
        fieldName - the field name
        type - the field type
        Returns:
        the field access expression
      • methodCall

        public static MethodCallExpr methodCall​(Expression receiver,
                                                String methodName,
                                                String ownerClass,
                                                SourceType returnType,
                                                Expression... args)
        Builds an instance method call node.
        Parameters:
        receiver - the receiver expression
        methodName - the method name
        ownerClass - the class declaring the method
        returnType - the return type
        args - the call arguments
        Returns:
        the method call expression
      • staticCall

        public static MethodCallExpr staticCall​(String ownerClass,
                                                String methodName,
                                                SourceType returnType,
                                                Expression... args)
        Builds a static method call node.
        Parameters:
        ownerClass - the class declaring the method
        methodName - the method name
        returnType - the return type
        args - the call arguments
        Returns:
        the method call expression
      • block

        public static BlockStmt block​(Statement... statements)
        Builds a block statement node.
        Parameters:
        statements - the contained statements
        Returns:
        the block statement
      • block

        public static BlockStmt block​(List<Statement> statements)
        Builds a block statement node.
        Parameters:
        statements - the contained statements
        Returns:
        the block statement
      • ifStmt

        public static IfStmt ifStmt​(Expression condition,
                                    Statement thenBranch)
        Builds an if statement node without an else branch.
        Parameters:
        condition - the condition
        thenBranch - the branch taken when true
        Returns:
        the if statement
      • ifElse

        public static IfStmt ifElse​(Expression condition,
                                    Statement thenBranch,
                                    Statement elseBranch)
        Builds an if-else statement node.
        Parameters:
        condition - the condition
        thenBranch - the branch taken when true
        elseBranch - the branch taken when false
        Returns:
        the if statement
      • whileLoop

        public static WhileStmt whileLoop​(Expression condition,
                                          Statement body)
        Builds a while loop node.
        Parameters:
        condition - the loop condition
        body - the loop body
        Returns:
        the while statement
      • doWhile

        public static DoWhileStmt doWhile​(Statement body,
                                          Expression condition)
        Builds a do-while loop node.
        Parameters:
        body - the loop body
        condition - the loop condition
        Returns:
        the do-while statement
      • forLoop

        public static ForStmt forLoop​(List<Statement> init,
                                      Expression condition,
                                      List<Expression> update,
                                      Statement body)
        Builds a for loop node.
        Parameters:
        init - the initializer statements
        condition - the loop condition
        update - the update expressions
        body - the loop body
        Returns:
        the for statement
      • infiniteLoop

        public static ForStmt infiniteLoop​(Statement body)
        Builds an infinite for loop node.
        Parameters:
        body - the loop body
        Returns:
        the for statement
      • forEach

        public static ForEachStmt forEach​(VarDeclStmt variable,
                                          Expression iterable,
                                          Statement body)
        Builds an enhanced-for loop node.
        Parameters:
        variable - the loop variable declaration
        iterable - the iterated expression
        body - the loop body
        Returns:
        the for-each statement
      • returnStmt

        public static ReturnStmt returnStmt​(Expression value)
        Builds a value-returning return statement node.
        Parameters:
        value - the returned expression
        Returns:
        the return statement
      • returnVoid

        public static ReturnStmt returnVoid()
        Builds a void return statement node.
        Returns:
        the return statement
      • throwStmt

        public static ThrowStmt throwStmt​(Expression exception)
        Builds a throw statement node.
        Parameters:
        exception - the thrown expression
        Returns:
        the throw statement
      • breakStmt

        public static BreakStmt breakStmt()
        Builds an unlabeled break statement node.
        Returns:
        the break statement
      • breakStmt

        public static BreakStmt breakStmt​(String label)
        Builds a labeled break statement node.
        Parameters:
        label - the target label
        Returns:
        the break statement
      • continueStmt

        public static ContinueStmt continueStmt()
        Builds an unlabeled continue statement node.
        Returns:
        the continue statement
      • continueStmt

        public static ContinueStmt continueStmt​(String label)
        Builds a labeled continue statement node.
        Parameters:
        label - the target label
        Returns:
        the continue statement
      • exprStmt

        public static ExprStmt exprStmt​(Expression expr)
        Builds an expression statement node.
        Parameters:
        expr - the wrapped expression
        Returns:
        the expression statement
      • varDecl

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

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

        public static VarDeclStmt finalVar​(SourceType type,
                                           String name,
                                           Expression initializer)
        Builds a final local variable declaration node.
        Parameters:
        type - the variable type
        name - the variable name
        initializer - the initial value
        Returns:
        the declaration statement
      • labeled

        public static LabeledStmt labeled​(String label,
                                          Statement statement)
        Builds a labeled statement node.
        Parameters:
        label - the label name
        statement - the labeled statement
        Returns:
        the labeled statement
      • synchronizedStmt

        public static SynchronizedStmt synchronizedStmt​(Expression lock,
                                                        Statement body)
        Builds a synchronized block node.
        Parameters:
        lock - the monitor expression
        body - the guarded body
        Returns:
        the synchronized statement
      • tryCatch

        public static TryCatchStmt tryCatch​(Statement tryBlock,
                                            List<CatchClause> catches)
        Builds a try-catch statement node.
        Parameters:
        tryBlock - the guarded block
        catches - the catch clauses
        Returns:
        the try-catch statement
      • tryCatchFinally

        public static TryCatchStmt tryCatchFinally​(Statement tryBlock,
                                                   List<CatchClause> catches,
                                                   Statement finallyBlock)
        Builds a try-catch-finally statement node.
        Parameters:
        tryBlock - the guarded block
        catches - the catch clauses
        finallyBlock - the finally block
        Returns:
        the try-catch statement
      • switchStmt

        public static SwitchStmt switchStmt​(Expression selector,
                                            List<SwitchCase> cases)
        Builds a switch statement node.
        Parameters:
        selector - the switched expression
        cases - the case groups
        Returns:
        the switch statement
      • refType

        public static ReferenceSourceType refType​(String className)
        Builds a reference type node.
        Parameters:
        className - the referenced class
        Returns:
        the reference type
      • arrayType

        public static ArraySourceType arrayType​(SourceType elementType)
        Builds a one-dimensional array type node.
        Parameters:
        elementType - the element type
        Returns:
        the array type
      • arrayType

        public static ArraySourceType arrayType​(SourceType elementType,
                                                int dimensions)
        Builds a multi-dimensional array type node.
        Parameters:
        elementType - the element type
        dimensions - the number of dimensions
        Returns:
        the array type