Package com.tonic.analysis.source.editor
Class ASTEditor
- java.lang.Object
-
- com.tonic.analysis.source.editor.ASTEditor
-
public class ASTEditor extends Object
Fluent editor that walks a method body, dispatching registered handlers to rewrite AST nodes.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply()Applies all registered handlers and modifies the AST in place.BlockStmtapplyAndReturn()Applies all registered handlers and returns the modified method body.List<Expression>findExpressions(ExprMatcher matcher)Finds all expressions matching the given matcher.List<Statement>findStatements(StmtMatcher matcher)Finds all statements matching the given matcher.ASTEditoronArrayAccess(ArrayAccessHandler handler)Registers a handler for array access expressions.ASTEditoronArrayRead(ArrayAccessHandler handler)Registers a handler for array read operations only.ASTEditoronArrayStore(ArrayAccessHandler handler)Registers a handler for array store operations only.ASTEditoronAssignment(AssignmentHandler handler)Registers a handler for assignment expressions.ASTEditoronBinaryExpr(BinaryExprHandler handler)Registers a handler for binary expressions.ASTEditoronCast(CastHandler handler)Registers a handler for cast expressions.ASTEditoronExpr(ExprMatcher matcher, ExpressionHandler handler)Registers a handler for expressions matching the given matcher.ASTEditoronFieldAccess(FieldAccessHandler handler)Registers a handler for field access expressions.ASTEditoronIf(IfHandler handler)Registers a handler for if statements.ASTEditoronInstanceOf(InstanceOfHandler handler)Registers a handler for instanceof expressions.ASTEditoronLoop(LoopHandler handler)Registers a handler for loop statements (for, while, do-while, for-each).ASTEditoronMethodCall(MethodCallHandler handler)Registers a handler for method call expressions.ASTEditoronNewArray(NewArrayHandler handler)Registers a handler for new array expressions.ASTEditoronNewExpr(NewExprHandler handler)Registers a handler for new object expressions.ASTEditoronReturn(ReturnHandler handler)Registers a handler for return statements.ASTEditoronStmt(StmtMatcher matcher, StatementHandler handler)Registers a handler for statements matching the given matcher.ASTEditoronThrow(ThrowHandler handler)Registers a handler for throw statements.ASTEditoronTryCatch(TryCatchHandler handler)Registers a handler for try-catch statements.ASTEditoronUnaryExpr(UnaryExprHandler handler)Registers a handler for unary expressions.
-
-
-
Constructor Detail
-
ASTEditor
public ASTEditor(BlockStmt methodBody, String methodName, String methodDescriptor, String ownerClass)
Creates a new AST editor for the given method body.- Parameters:
methodBody- the block to editmethodName- the enclosing method's name, exposed to handlersmethodDescriptor- the enclosing method's descriptor, exposed to handlersownerClass- the internal name of the declaring class, exposed to handlers
-
ASTEditor
public ASTEditor(BlockStmt methodBody)
Creates a new AST editor with minimal context.- Parameters:
methodBody- the block to edit
-
-
Method Detail
-
onMethodCall
public ASTEditor onMethodCall(MethodCallHandler handler)
Registers a handler for method call expressions.- Parameters:
handler- the handler to run on every call site- Returns:
- this editor
-
onFieldAccess
public ASTEditor onFieldAccess(FieldAccessHandler handler)
Registers a handler for field access expressions.- Parameters:
handler- the handler to run on every field access- Returns:
- this editor
-
onNewExpr
public ASTEditor onNewExpr(NewExprHandler handler)
Registers a handler for new object expressions.- Parameters:
handler- the handler to run on every allocation- Returns:
- this editor
-
onNewArray
public ASTEditor onNewArray(NewArrayHandler handler)
Registers a handler for new array expressions.- Parameters:
handler- the handler to run on every array allocation- Returns:
- this editor
-
onCast
public ASTEditor onCast(CastHandler handler)
Registers a handler for cast expressions.- Parameters:
handler- the handler to run on every cast- Returns:
- this editor
-
onInstanceOf
public ASTEditor onInstanceOf(InstanceOfHandler handler)
Registers a handler for instanceof expressions.- Parameters:
handler- the handler to run on every instanceof test- Returns:
- this editor
-
onBinaryExpr
public ASTEditor onBinaryExpr(BinaryExprHandler handler)
Registers a handler for binary expressions.- Parameters:
handler- the handler to run on every binary expression- Returns:
- this editor
-
onUnaryExpr
public ASTEditor onUnaryExpr(UnaryExprHandler handler)
Registers a handler for unary expressions.- Parameters:
handler- the handler to run on every unary expression- Returns:
- this editor
-
onArrayAccess
public ASTEditor onArrayAccess(ArrayAccessHandler handler)
Registers a handler for array access expressions.- Parameters:
handler- the handler to run on every array access- Returns:
- this editor
-
onArrayRead
public ASTEditor onArrayRead(ArrayAccessHandler handler)
Registers a handler for array read operations only.- Parameters:
handler- the handler to run on array accesses whose type is READ- Returns:
- this editor
-
onArrayStore
public ASTEditor onArrayStore(ArrayAccessHandler handler)
Registers a handler for array store operations only.- Parameters:
handler- the handler to run on array accesses whose type is STORE- Returns:
- this editor
-
onReturn
public ASTEditor onReturn(ReturnHandler handler)
Registers a handler for return statements.- Parameters:
handler- the handler to run on every return- Returns:
- this editor
-
onThrow
public ASTEditor onThrow(ThrowHandler handler)
Registers a handler for throw statements.- Parameters:
handler- the handler to run on every throw- Returns:
- this editor
-
onIf
public ASTEditor onIf(IfHandler handler)
Registers a handler for if statements.- Parameters:
handler- the handler to run on every if- Returns:
- this editor
-
onLoop
public ASTEditor onLoop(LoopHandler handler)
Registers a handler for loop statements (for, while, do-while, for-each).- Parameters:
handler- the handler to run on every loop- Returns:
- this editor
-
onTryCatch
public ASTEditor onTryCatch(TryCatchHandler handler)
Registers a handler for try-catch statements.- Parameters:
handler- the handler to run on every try-catch- Returns:
- this editor
-
onAssignment
public ASTEditor onAssignment(AssignmentHandler handler)
Registers a handler for assignment expressions.- Parameters:
handler- the handler to run on every assignment- Returns:
- this editor
-
onExpr
public ASTEditor onExpr(ExprMatcher matcher, ExpressionHandler handler)
Registers a handler for expressions matching the given matcher.- Parameters:
matcher- selects which expressions the handler seeshandler- the handler to run on each match- Returns:
- this editor
-
onStmt
public ASTEditor onStmt(StmtMatcher matcher, StatementHandler handler)
Registers a handler for statements matching the given matcher.- Parameters:
matcher- selects which statements the handler seeshandler- the handler to run on each match- Returns:
- this editor
-
apply
public void apply()
Applies all registered handlers and modifies the AST in place.
-
applyAndReturn
public BlockStmt applyAndReturn()
Applies all registered handlers and returns the modified method body.- Returns:
- the method body, edited in place
-
findExpressions
public List<Expression> findExpressions(ExprMatcher matcher)
Finds all expressions matching the given matcher.- Parameters:
matcher- the predicate expressions are tested against- Returns:
- the matching expressions in traversal order
-
findStatements
public List<Statement> findStatements(StmtMatcher matcher)
Finds all statements matching the given matcher.- Parameters:
matcher- the predicate statements are tested against- Returns:
- the matching statements in traversal order
-
-