Package com.tonic.analysis.source.editor
Class ExpressionEditor
- java.lang.Object
-
- com.tonic.analysis.source.editor.ExpressionEditor
-
public class ExpressionEditor extends Object
Expression-only view over ASTEditor.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceExpressionEditor.FieldAccessReplacerFunctional interface for field access replacement.static interfaceExpressionEditor.MethodCallReplacerFunctional interface for method call replacement.static interfaceExpressionEditor.NewExprReplacerFunctional interface for new expression replacement.
-
Constructor Summary
Constructors Constructor Description ExpressionEditor(BlockStmt methodBody, String methodName, String methodDescriptor, String ownerClass)Creates an expression 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<Expression>findArrayAccesses()List<Expression>findExpressions(ExprMatcher matcher)Collects the expressions a matcher selects.List<Expression>findFieldAccesses()List<Expression>findMethodCalls()List<Expression>findMethodCalls(String methodName)Collects the calls to one method name, whatever the owner.List<Expression>findMethodCalls(String ownerClass, String methodName)Collects the calls to one method on one owner.List<Expression>findNewExpressions(String className)Collects the allocations of one class.ASTEditorgetDelegate()ExpressionEditoronAnyExpr(ExpressionHandler handler)Registers a handler for every expression in the body.ExpressionEditoronArrayAccess(ArrayAccessHandler handler)Registers a handler for array accesses, reads and stores alike.ExpressionEditoronArrayRead(ArrayAccessHandler handler)Registers a handler for array reads only.ExpressionEditoronArrayStore(ArrayAccessHandler handler)Registers a handler for array stores only.ExpressionEditoronBinaryExpr(BinaryExprHandler handler)Registers a handler for binary expressions.ExpressionEditoronCast(CastHandler handler)Registers a handler for cast expressions.ExpressionEditoronExpr(ExprMatcher matcher, ExpressionHandler handler)Registers a handler for the expressions a matcher selects.ExpressionEditoronFieldAccess(FieldAccessHandler handler)Registers a handler for field access expressions.ExpressionEditoronInstanceOf(InstanceOfHandler handler)Registers a handler for instanceof expressions.ExpressionEditoronMethodCall(MethodCallHandler handler)Registers a handler for method call expressions.ExpressionEditoronNewArray(NewArrayHandler handler)Registers a handler for array allocation expressions.ExpressionEditoronNewExpr(NewExprHandler handler)Registers a handler for object allocation expressions.ExpressionEditoronUnaryExpr(UnaryExprHandler handler)Registers a handler for unary expressions.ExpressionEditorremoveMethodCall(String ownerClass, String methodName)Removes all method calls matching the criteria.ExpressionEditorreplaceFieldAccess(String ownerClass, String fieldName, ExpressionEditor.FieldAccessReplacer replacer)Replaces all field accesses matching the criteria.ExpressionEditorreplaceMethodCall(String ownerClass, String methodName, ExpressionEditor.MethodCallReplacer replacer)Replaces all method calls matching the criteria.ExpressionEditorreplaceNewExpr(String className, ExpressionEditor.NewExprReplacer replacer)Replaces all new expressions of a specific class.
-
-
-
Constructor Detail
-
ExpressionEditor
public ExpressionEditor(BlockStmt methodBody, String methodName, String methodDescriptor, String ownerClass)
Creates an expression 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
-
onMethodCall
public ExpressionEditor onMethodCall(MethodCallHandler handler)
Registers a handler for method call expressions.- Parameters:
handler- the handler to run on each method call- Returns:
- this editor
-
onFieldAccess
public ExpressionEditor onFieldAccess(FieldAccessHandler handler)
Registers a handler for field access expressions.- Parameters:
handler- the handler to run on each field access- Returns:
- this editor
-
onNewExpr
public ExpressionEditor onNewExpr(NewExprHandler handler)
Registers a handler for object allocation expressions.- Parameters:
handler- the handler to run on each new expression- Returns:
- this editor
-
onNewArray
public ExpressionEditor onNewArray(NewArrayHandler handler)
Registers a handler for array allocation expressions.- Parameters:
handler- the handler to run on each new array expression- Returns:
- this editor
-
onCast
public ExpressionEditor onCast(CastHandler handler)
Registers a handler for cast expressions.- Parameters:
handler- the handler to run on each cast- Returns:
- this editor
-
onInstanceOf
public ExpressionEditor onInstanceOf(InstanceOfHandler handler)
Registers a handler for instanceof expressions.- Parameters:
handler- the handler to run on each instanceof test- Returns:
- this editor
-
onBinaryExpr
public ExpressionEditor onBinaryExpr(BinaryExprHandler handler)
Registers a handler for binary expressions.- Parameters:
handler- the handler to run on each binary expression- Returns:
- this editor
-
onUnaryExpr
public ExpressionEditor onUnaryExpr(UnaryExprHandler handler)
Registers a handler for unary expressions.- Parameters:
handler- the handler to run on each unary expression- Returns:
- this editor
-
onArrayAccess
public ExpressionEditor onArrayAccess(ArrayAccessHandler handler)
Registers a handler for array accesses, reads and stores alike.- Parameters:
handler- the handler to run on each access; its context says whether it is a read or a store- Returns:
- this editor
-
onArrayRead
public ExpressionEditor onArrayRead(ArrayAccessHandler handler)
Registers a handler for array reads only.- Parameters:
handler- the handler to run on each array read- Returns:
- this editor
-
onArrayStore
public ExpressionEditor onArrayStore(ArrayAccessHandler handler)
Registers a handler for array stores only.- Parameters:
handler- the handler to run on each array store- Returns:
- this editor
-
onExpr
public ExpressionEditor onExpr(ExprMatcher matcher, ExpressionHandler handler)
Registers a handler for the expressions a matcher selects.- Parameters:
matcher- selects which expressions reach the handlerhandler- the handler to run on each match- Returns:
- this editor
-
onAnyExpr
public ExpressionEditor onAnyExpr(ExpressionHandler handler)
Registers a handler for every expression in the body.- Parameters:
handler- the handler to run on each expression- Returns:
- this editor
-
replaceMethodCall
public ExpressionEditor replaceMethodCall(String ownerClass, String methodName, ExpressionEditor.MethodCallReplacer replacer)
Replaces all method calls matching the criteria.- Parameters:
ownerClass- the owner class to matchmethodName- the method name to matchreplacer- function to create replacement expression- Returns:
- this editor for chaining
-
removeMethodCall
public ExpressionEditor removeMethodCall(String ownerClass, String methodName)
Removes all method calls matching the criteria.- Parameters:
ownerClass- the owner class to matchmethodName- the method name to match- Returns:
- this editor for chaining
-
replaceFieldAccess
public ExpressionEditor replaceFieldAccess(String ownerClass, String fieldName, ExpressionEditor.FieldAccessReplacer replacer)
Replaces all field accesses matching the criteria.- Parameters:
ownerClass- the owner class to matchfieldName- the field name to matchreplacer- function to create replacement expression- Returns:
- this editor for chaining
-
replaceNewExpr
public ExpressionEditor replaceNewExpr(String className, ExpressionEditor.NewExprReplacer replacer)
Replaces all new expressions of a specific class.- Parameters:
className- the class name to matchreplacer- function to create replacement expression- Returns:
- this editor for chaining
-
findExpressions
public List<Expression> findExpressions(ExprMatcher matcher)
Collects the expressions a matcher selects.- Parameters:
matcher- selects which expressions to collect- Returns:
- the matching expressions
-
findMethodCalls
public List<Expression> findMethodCalls()
- Returns:
- every method call expression in the body
-
findMethodCalls
public List<Expression> findMethodCalls(String methodName)
Collects the calls to one method name, whatever the owner.- Parameters:
methodName- the method name to match- Returns:
- the matching call expressions
-
findMethodCalls
public List<Expression> findMethodCalls(String ownerClass, String methodName)
Collects the calls to one method on one owner.- Parameters:
ownerClass- the owning class to matchmethodName- the method name to match- Returns:
- the matching call expressions
-
findFieldAccesses
public List<Expression> findFieldAccesses()
- Returns:
- every field access expression in the body
-
findNewExpressions
public List<Expression> findNewExpressions(String className)
Collects the allocations of one class.- Parameters:
className- the allocated class to match- Returns:
- the matching new expressions
-
findArrayAccesses
public List<Expression> findArrayAccesses()
- Returns:
- every array access expression in the body
-
apply
public void apply()
Applies all registered handlers and modifies the AST in place.
-
getDelegate
public ASTEditor getDelegate()
- Returns:
- the underlying editor, for operations this wrapper does not expose
-
-