Package com.tonic.analysis.source.editor
Class StatementEditor
- java.lang.Object
-
- com.tonic.analysis.source.editor.StatementEditor
-
-
Constructor Summary
Constructors Constructor Description StatementEditor(BlockStmt methodBody, String methodName, String methodDescriptor, String ownerClass)Creates a statement editor for a method body.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply()Applies all registered handlers and modifies the AST in place.List<Statement>findIfElses()Finds all if statements that have an else branch.List<Statement>findIfs()Finds all if statements.List<Statement>findLoops()Finds all for, while, do-while and for-each statements.List<Statement>findReturns()Finds all return statements.List<Statement>findStatements(StmtMatcher matcher)Finds all statements matching the given matcher.List<Statement>findSwitches()Finds all switch statements.List<Statement>findThrows()Finds all throw statements.List<Statement>findTryCatches()Finds all try-catch statements.List<Statement>findValueReturns()Finds all returns that carry a value.List<Statement>findVarDecls()Finds all variable declaration statements.List<Statement>findVoidReturns()Finds all returns that carry no value.ASTEditorgetDelegate()StatementEditorinsertBeforeReturns(Statement... stmts)Registers a handler that inserts statements ahead of every return.StatementEditorinsertBeforeThrows(Statement... stmts)Registers a handler that inserts statements ahead of every throw.StatementEditorinsertBeforeValueReturns(Statement... stmts)Registers a handler that inserts statements ahead of every value-carrying return.StatementEditorinsertBeforeVoidReturns(Statement... stmts)Registers a handler that inserts statements ahead of every valueless return.StatementEditoronAnyStmt(StatementHandler handler)Registers a handler that sees every statement.StatementEditoronAssignment(AssignmentHandler handler)Registers a handler for assignment statements.StatementEditoronIf(IfHandler handler)Registers a handler for if statements.StatementEditoronLoop(LoopHandler handler)Registers a handler for for, while, do-while and for-each statements.StatementEditoronReturn(ReturnHandler handler)Registers a handler for return statements.StatementEditoronStmt(StmtMatcher matcher, StatementHandler handler)Registers a handler for the statements a matcher accepts.StatementEditoronThrow(ThrowHandler handler)Registers a handler for throw statements.StatementEditoronTryCatch(TryCatchHandler handler)Registers a handler for try-catch statements.StatementEditorremoveStmts(StmtMatcher matcher)Registers a handler that drops every statement the matcher accepts.StatementEditorremoveStmts(Class<? extends Statement> type)Registers a handler that drops every statement of a given node type.StatementEditorwrapLoops(Statement before, Statement after)Registers a handler that replaces every loop with a block bracketing it.
-
-
-
Constructor Detail
-
StatementEditor
public StatementEditor(BlockStmt methodBody, String methodName, String methodDescriptor, String ownerClass)
Creates a statement editor for a method body.- Parameters:
methodBody- the method body to editmethodName- the name of the methodmethodDescriptor- the method descriptorownerClass- the internal name of the owning class
-
-
Method Detail
-
onReturn
public StatementEditor onReturn(ReturnHandler handler)
Registers a handler for return statements.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
onThrow
public StatementEditor onThrow(ThrowHandler handler)
Registers a handler for throw statements.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
onIf
public StatementEditor onIf(IfHandler handler)
Registers a handler for if statements.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
onLoop
public StatementEditor onLoop(LoopHandler handler)
Registers a handler for for, while, do-while and for-each statements.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
onTryCatch
public StatementEditor onTryCatch(TryCatchHandler handler)
Registers a handler for try-catch statements.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
onAssignment
public StatementEditor onAssignment(AssignmentHandler handler)
Registers a handler for assignment statements.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
onStmt
public StatementEditor onStmt(StmtMatcher matcher, StatementHandler handler)
Registers a handler for the statements a matcher accepts.- Parameters:
matcher- the predicate statements are tested againsthandler- the handler to invoke on a match- Returns:
- this editor
-
onAnyStmt
public StatementEditor onAnyStmt(StatementHandler handler)
Registers a handler that sees every statement.- Parameters:
handler- the handler to invoke- Returns:
- this editor
-
insertBeforeReturns
public StatementEditor insertBeforeReturns(Statement... stmts)
Registers a handler that inserts statements ahead of every return.- Parameters:
stmts- the statements to insert, in order- Returns:
- this editor
-
insertBeforeValueReturns
public StatementEditor insertBeforeValueReturns(Statement... stmts)
Registers a handler that inserts statements ahead of every value-carrying return.- Parameters:
stmts- the statements to insert, in order- Returns:
- this editor
-
insertBeforeVoidReturns
public StatementEditor insertBeforeVoidReturns(Statement... stmts)
Registers a handler that inserts statements ahead of every valueless return.- Parameters:
stmts- the statements to insert, in order- Returns:
- this editor
-
wrapLoops
public StatementEditor wrapLoops(Statement before, Statement after)
Registers a handler that replaces every loop with a block bracketing it.- Parameters:
before- the statement placed ahead of the loopafter- the statement placed after the loop- Returns:
- this editor
-
insertBeforeThrows
public StatementEditor insertBeforeThrows(Statement... stmts)
Registers a handler that inserts statements ahead of every throw.- Parameters:
stmts- the statements to insert, in order- Returns:
- this editor
-
removeStmts
public StatementEditor removeStmts(Class<? extends Statement> type)
Registers a handler that drops every statement of a given node type.- Parameters:
type- the statement class to remove- Returns:
- this editor
-
removeStmts
public StatementEditor removeStmts(StmtMatcher matcher)
Registers a handler that drops every statement the matcher accepts.- Parameters:
matcher- the predicate statements are tested against- Returns:
- this editor
-
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
-
findReturns
public List<Statement> findReturns()
Finds all return statements.- Returns:
- the matching statements in traversal order
-
findValueReturns
public List<Statement> findValueReturns()
Finds all returns that carry a value.- Returns:
- the matching statements in traversal order
-
findVoidReturns
public List<Statement> findVoidReturns()
Finds all returns that carry no value.- Returns:
- the matching statements in traversal order
-
findThrows
public List<Statement> findThrows()
Finds all throw statements.- Returns:
- the matching statements in traversal order
-
findLoops
public List<Statement> findLoops()
Finds all for, while, do-while and for-each statements.- Returns:
- the matching statements in traversal order
-
findIfs
public List<Statement> findIfs()
Finds all if statements.- Returns:
- the matching statements in traversal order
-
findIfElses
public List<Statement> findIfElses()
Finds all if statements that have an else branch.- Returns:
- the matching statements in traversal order
-
findTryCatches
public List<Statement> findTryCatches()
Finds all try-catch statements.- Returns:
- the matching statements in traversal order
-
findSwitches
public List<Statement> findSwitches()
Finds all switch statements.- Returns:
- the matching statements in traversal order
-
findVarDecls
public List<Statement> findVarDecls()
Finds all variable declaration statements.- Returns:
- the matching statements in traversal order
-
apply
public void apply()
Applies all registered handlers and modifies the AST in place.
-
getDelegate
public ASTEditor getDelegate()
- Returns:
- the wrapped editor, for operations this facade does not expose
-
-