Class ASTFactory
- java.lang.Object
-
- com.tonic.analysis.source.editor.util.ASTFactory
-
public class ASTFactory extends Object
Factory for building source AST expressions and statements programmatically.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classASTFactory.MethodCallBuilderBuilder for method call expressions.static classASTFactory.NewExprBuilderBuilder for new object expressions.
-
Constructor Summary
Constructors Constructor Description ASTFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BinaryExpradd(Expression left, Expression right, SourceType type)Creates an addition expression.BinaryExprand(Expression left, Expression right)Creates a logical AND expression typed boolean.ArrayAccessExprarrayAccess(Expression array, Expression index, SourceType componentType)Creates an array access expression.BinaryExprassign(Expression left, Expression right, SourceType type)Creates an assignment expression.BinaryExprbinary(Expression left, BinaryOperator op, Expression right, SourceType type)Creates a binary expression.BlockStmtblock(Statement... stmts)Creates a block statement.BlockStmtblock(List<Statement> stmts)Creates a block statement.LiteralExprboolLiteral(boolean value)Creates a boolean literal.CastExprcast(SourceType targetType, Expression expr)Creates a cast expression.CastExprcast(String typeName, Expression expr)Creates a cast expression, parsing the target from its name.LiteralExprcharLiteral(char value)Creates a character literal.BinaryExprdivide(Expression left, Expression right, SourceType type)Creates a division expression.LiteralExprdoubleLiteral(double value)Creates a double literal.BinaryExprequals(Expression left, Expression right)Creates an equality comparison typed boolean.ExprStmtexprStmt(Expression expr)Wraps an expression as a statement.FieldAccessExprfieldAccess(Expression target, String fieldName, String ownerClass, SourceType type)Creates an instance field access.LiteralExprfloatLiteral(float value)Creates a float literal.BinaryExprgreaterThan(Expression left, Expression right)Creates a greater-than comparison typed boolean.IfStmtifElseStmt(Expression condition, Statement thenBranch, Statement elseBranch)Creates an if statement with an else arm.IfStmtifStmt(Expression condition, Statement thenBranch)Creates an if statement without an else arm.MethodCallExprinstanceCall(Expression receiver, String ownerClass, String methodName, SourceType returnType, Expression... args)Creates an instance method call.InstanceOfExprinstanceOf(Expression expr, SourceType checkedType)Creates an instanceof test.InstanceOfExprinstanceOf(Expression expr, String typeName)Creates an instanceof test, parsing the tested type from its name.LiteralExprintLiteral(int value)Creates an integer literal.BinaryExprlessThan(Expression left, Expression right)Creates a less-than comparison typed boolean.LiteralExprlongLiteral(long value)Creates a long literal.ASTFactory.MethodCallBuildermethodCall(String methodName)Starts a method call for incremental configuration.BinaryExprmultiply(Expression left, Expression right, SourceType type)Creates a multiplication expression.UnaryExprnegate(Expression operand, SourceType type)Creates an arithmetic negation.NewArrayExprnewArray(SourceType elementType, Expression size)Creates a sized array allocation.NewArrayExprnewArray(String typeName, Expression size)Creates a sized array allocation, parsing the element type from its name.NewExprnewExpr(String className, Expression... args)Creates an allocation expression.ASTFactory.NewExprBuildernewInstance(String className)Starts an allocation for incremental configuration.UnaryExprnot(Expression operand)Creates a logical NOT typed boolean.BinaryExprnotEquals(Expression left, Expression right)Creates an inequality comparison typed boolean.LiteralExprnullLiteral()Creates a null literal.BinaryExpror(Expression left, Expression right)Creates a logical OR expression typed boolean.SourceTypeparseType(String typeName)Parses a type from a string representation.UnaryExprpostIncrement(Expression operand, SourceType type)Creates a post-increment expression.UnaryExprpreIncrement(Expression operand, SourceType type)Creates a pre-increment expression.ReturnStmtreturnStmt(Expression value)Creates a return statement with a value.ReturnStmtreturnVoid()Creates a valueless return statement.MethodCallExprstaticCall(String ownerClass, String methodName, SourceType returnType, Expression... args)Creates a static method call.FieldAccessExprstaticField(String ownerClass, String fieldName, SourceType type)Creates a static field access.LiteralExprstringLiteral(String value)Creates a string literal.BinaryExprsubtract(Expression left, Expression right, SourceType type)Creates a subtraction expression.TernaryExprternary(Expression condition, Expression thenExpr, Expression elseExpr, SourceType type)Creates a ternary expression.ThrowStmtthrowStmt(Expression exception)Creates a throw statement.UnaryExprunary(UnaryOperator op, Expression operand, SourceType type)Creates a unary expression.VarDeclStmtvarDecl(SourceType type, String name)Creates an uninitialized local variable declaration.VarDeclStmtvarDecl(SourceType type, String name, Expression initializer)Creates an initialized local variable declaration.VarDeclStmtvarDecl(String typeName, String name, Expression initializer)Creates an initialized local variable declaration, parsing the type from its name.VarRefExprvariable(String name)Creates a variable reference typed as java.lang.Object.VarRefExprvariable(String name, SourceType type)Creates a variable reference.
-
-
-
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 nametype- 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 expressionfieldName- the field nameownerClass- the internal name of the declaring classtype- 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 classfieldName- the field nametype- 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 expressionindex- the index expressioncomponentType- 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 classmethodName- the method namereturnType- the return typeargs- 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 expressionownerClass- the internal name of the declaring classmethodName- the method namereturnType- the return typeargs- 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 allocateargs- 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 typesize- 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 namesize- 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 operandop- the operatorright- the right operandtype- 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 operandright- the right operandtype- 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 operandright- the right operandtype- 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 operandright- the right operandtype- 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 operandright- the right operandtype- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 operandright- 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 targetright- the assigned valuetype- the result type- Returns:
- the binary expression
-
unary
public UnaryExpr unary(UnaryOperator op, Expression operand, SourceType type)
Creates a unary expression.- Parameters:
op- the operatoroperand- the operandtype- the result type- Returns:
- the unary expression
-
negate
public UnaryExpr negate(Expression operand, SourceType type)
Creates an arithmetic negation.- Parameters:
operand- the operandtype- 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 operandtype- the result type- Returns:
- the unary expression
-
postIncrement
public UnaryExpr postIncrement(Expression operand, SourceType type)
Creates a post-increment expression.- Parameters:
operand- the operandtype- the result type- Returns:
- the unary expression
-
cast
public CastExpr cast(SourceType targetType, Expression expr)
Creates a cast expression.- Parameters:
targetType- the type cast toexpr- 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 nameexpr- the operand- Returns:
- the cast expression
-
instanceOf
public InstanceOfExpr instanceOf(Expression expr, SourceType checkedType)
Creates an instanceof test.- Parameters:
expr- the operandcheckedType- 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 operandtypeName- 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 conditionthenExpr- the value when the condition holdselseExpr- the value otherwisetype- 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 guardthenBranch- 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 guardthenBranch- the statement run when the guard holdselseBranch- 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 typename- the variable nameinitializer- 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 namename- the variable nameinitializer- the initial value- Returns:
- the statement
-
varDecl
public VarDeclStmt varDecl(SourceType type, String name)
Creates an uninitialized local variable declaration.- Parameters:
type- the declared typename- 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
-
-