Package com.tonic.analysis.source.ast
Class ASTFactory
- java.lang.Object
-
- com.tonic.analysis.source.ast.ASTFactory
-
public final class ASTFactory extends Object
Static factory of AST nodes with sensible type defaults.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BinaryExpradd(Expression left, Expression right)Builds an addition node typed from the left operand.static BinaryExprand(Expression left, Expression right)Builds a logical AND node.static ArrayAccessExprarrayAccess(Expression array, Expression index, SourceType elementType)Builds an array element access node.static ArrayInitExprarrayInit(SourceType elementType, Expression... elements)Builds an array initializer node.static ArraySourceTypearrayType(SourceType elementType)Builds a one-dimensional array type node.static ArraySourceTypearrayType(SourceType elementType, int dimensions)Builds a multi-dimensional array type node.static BinaryExprassign(Expression left, Expression right)Builds an assignment expression node typed from the target.static BinaryExprbinary(BinaryOperator op, Expression left, Expression right, SourceType type)Builds a binary expression node.static BlockStmtblock(Statement... statements)Builds a block statement node.static BlockStmtblock(List<Statement> statements)Builds a block statement node.static LiteralExprboolLit(boolean value)Builds a boolean literal node.static VarRefExprboolVar(String name)Builds a boolean-typed variable reference node.static BreakStmtbreakStmt()Builds an unlabeled break statement node.static BreakStmtbreakStmt(String label)Builds a labeled break statement node.static CastExprcast(SourceType targetType, Expression expr)Builds a cast node.static LiteralExprcharLit(char value)Builds a char literal node.static ClassExprclassExpr(SourceType classType)Builds a class literal node.static ContinueStmtcontinueStmt()Builds an unlabeled continue statement node.static ContinueStmtcontinueStmt(String label)Builds a labeled continue statement node.static BinaryExprdiv(Expression left, Expression right)Builds a division node typed from the left operand.static LiteralExprdoubleLit(double value)Builds a double literal node.static DoWhileStmtdoWhile(Statement body, Expression condition)Builds a do-while loop node.static BinaryExpreq(Expression left, Expression right)Builds a boolean equality comparison node.static ExprStmtexprStmt(Expression expr)Builds an expression statement node.static FieldAccessExprfieldAccess(Expression receiver, String fieldName, String ownerClass, SourceType type)Builds an instance field access node.static VarDeclStmtfinalVar(SourceType type, String name, Expression initializer)Builds a final local variable declaration node.static LiteralExprfloatLit(float value)Builds a float literal node.static ForEachStmtforEach(VarDeclStmt variable, Expression iterable, Statement body)Builds an enhanced-for loop node.static ForStmtforLoop(List<Statement> init, Expression condition, List<Expression> update, Statement body)Builds a for loop node.static BinaryExprge(Expression left, Expression right)Builds a boolean greater-or-equal comparison node.static BinaryExprgt(Expression left, Expression right)Builds a boolean greater-than comparison node.static IfStmtifElse(Expression condition, Statement thenBranch, Statement elseBranch)Builds an if-else statement node.static IfStmtifStmt(Expression condition, Statement thenBranch)Builds an if statement node without an else branch.static ForStmtinfiniteLoop(Statement body)Builds an infinite for loop node.static InstanceOfExprinstanceOf(Expression expr, SourceType checkType)Builds an instanceof test node.static InstanceOfExprinstanceOf(Expression expr, SourceType checkType, String patternVar)Builds an instanceof test node with a pattern variable.static LiteralExprintLit(int value)Builds an int literal node.static VarRefExprintVar(String name)Builds an int-typed variable reference node.static LabeledStmtlabeled(String label, Statement statement)Builds a labeled statement node.static BinaryExprle(Expression left, Expression right)Builds a boolean less-or-equal comparison node.static LiteralExprlongLit(long value)Builds a long literal node.static BinaryExprlt(Expression left, Expression right)Builds a boolean less-than comparison node.static MethodCallExprmethodCall(Expression receiver, String methodName, String ownerClass, SourceType returnType, Expression... args)Builds an instance method call node.static BinaryExprmod(Expression left, Expression right)Builds a modulo node typed from the left operand.static BinaryExprmul(Expression left, Expression right)Builds a multiplication node typed from the left operand.static BinaryExprne(Expression left, Expression right)Builds a boolean inequality comparison node.static UnaryExprneg(Expression operand)Builds an arithmetic negation node typed from the operand.static NewArrayExprnewArray(SourceType elementType, Expression size)Builds a sized array allocation node.static NewExprnewObj(String className, Expression... args)Builds an object instantiation node.static UnaryExprnot(Expression operand)Builds a logical negation node.static LiteralExprnullLit()Builds a null literal node.static VarRefExprobjectVar(String name, String className)Builds a reference-typed variable reference node.static BinaryExpror(Expression left, Expression right)Builds a logical OR node.static UnaryExprpostDecr(Expression operand)Builds a post-decrement node typed from the operand.static UnaryExprpostIncr(Expression operand)Builds a post-increment node typed from the operand.static UnaryExprpreDecr(Expression operand)Builds a pre-decrement node typed from the operand.static UnaryExprpreIncr(Expression operand)Builds a pre-increment node typed from the operand.static ReferenceSourceTyperefType(String className)Builds a reference type node.static ReturnStmtreturnStmt(Expression value)Builds a value-returning return statement node.static ReturnStmtreturnVoid()Builds a void return statement node.static MethodCallExprstaticCall(String ownerClass, String methodName, SourceType returnType, Expression... args)Builds a static method call node.static FieldAccessExprstaticField(String ownerClass, String fieldName, SourceType type)Builds a static field access node.static LiteralExprstringLit(String value)Builds a String literal node.static BinaryExprsub(Expression left, Expression right)Builds a subtraction node typed from the left operand.static SwitchStmtswitchStmt(Expression selector, List<SwitchCase> cases)Builds a switch statement node.static SynchronizedStmtsynchronizedStmt(Expression lock, Statement body)Builds a synchronized block node.static TernaryExprternary(Expression condition, Expression thenExpr, Expression elseExpr)Builds a ternary conditional node typed from the then branch.static ThisExprthisExpr(SourceType type)Builds a this reference node.static ThrowStmtthrowStmt(Expression exception)Builds a throw statement node.static TryCatchStmttryCatch(Statement tryBlock, List<CatchClause> catches)Builds a try-catch statement node.static TryCatchStmttryCatchFinally(Statement tryBlock, List<CatchClause> catches, Statement finallyBlock)Builds a try-catch-finally statement node.static UnaryExprunary(UnaryOperator op, Expression operand, SourceType type)Builds a unary expression node.static VarDeclStmtvarDecl(SourceType type, String name)Builds an uninitialized local variable declaration node.static VarDeclStmtvarDecl(SourceType type, String name, Expression initializer)Builds an initialized local variable declaration node.static VarRefExprvarRef(String name, SourceType type)Builds a variable reference node.static WhileStmtwhileLoop(Expression condition, Statement body)Builds a while loop node.
-
-
-
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 nametype- 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 nameclassName- 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 operatorleft- the left operandright- the right operandtype- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 targetright- 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 operatoroperand- the operandtype- 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 conditionthenExpr- the value when trueelseExpr- 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 toexpr- 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 expressioncheckType- 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 expressioncheckType- the type tested againstpatternVar- 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 expressionindex- the index expressionelementType- 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 instantiatedargs- 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 typesize- 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 typeelements- 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 expressionfieldName- the field nameownerClass- the class declaring the fieldtype- 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 fieldfieldName- the field nametype- 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 expressionmethodName- the method nameownerClass- the class declaring the methodreturnType- the return typeargs- 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 methodmethodName- the method namereturnType- the return typeargs- 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 conditionthenBranch- 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 conditionthenBranch- the branch taken when trueelseBranch- 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 conditionbody- 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 bodycondition- 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 statementscondition- the loop conditionupdate- the update expressionsbody- 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 declarationiterable- the iterated expressionbody- 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 typename- 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 typename- the variable nameinitializer- 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 typename- the variable nameinitializer- 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 namestatement- 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 expressionbody- 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 blockcatches- 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 blockcatches- the catch clausesfinallyBlock- 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 expressioncases- 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 typedimensions- the number of dimensions- Returns:
- the array type
-
-