Class ExprEditor
- java.lang.Object
-
- javassist.expr.ExprEditor
-
public class ExprEditor extends java.lang.ObjectA translator of method bodies.The users can define a subclass of this class to customize how to modify a method body. The overall architecture is similar to the strategy pattern.
If
instrument()is called inCtMethod, the method body is scanned from the beginning to the end. Whenever an expression, such as a method call and a new expression (object creation), is found,edit()is called inExprEdit.edit()can inspect and modify the given expression. The modification is reflected on the original method body. Ifedit()does nothing, the original method body is not changed.The following code is an example:
CtMethod cm = ...; cm.instrument(new ExprEditor() { public void edit(MethodCall m) throws CannotCompileException { if (m.getClassName().equals("Point")) { System.out.println(m.getMethodName() + " line: " + m.getLineNumber()); } });This code inspects all method calls appearing in the method represented by
cmand it prints the names and the line numbers of the methods declared in classPoint. This code does not modify the body of the method represented bycm. If the method body must be modified, callreplace()inMethodCall.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classExprEditor.LoopContext(package private) static classExprEditor.NewOp
-
Constructor Summary
Constructors Constructor Description ExprEditor()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleandoit(CtClass clazz, MethodInfo minfo)Undocumented method.(package private) booleandoit(CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context, CodeIterator iterator, int endPos)Visits each bytecode in the given range.voidedit(Cast c)Edits an expression for explicit type casting (overridable).voidedit(ConstructorCall c)Edits a constructor call (overridable).voidedit(FieldAccess f)Edits a field-access expression (overridable).voidedit(Handler h)Edits a catch clause (overridable).voidedit(Instanceof i)Edits an instanceof expression (overridable).voidedit(MethodCall m)Edits a method call (overridable).voidedit(NewArray a)Edits an expression for array creation (overridable).voidedit(NewExpr e)Edits a new expression (overridable).(package private) booleanloopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context)
-
-
-
Method Detail
-
doit
public boolean doit(CtClass clazz, MethodInfo minfo) throws CannotCompileException
Undocumented method. Do not use; internal-use only.- Throws:
CannotCompileException
-
doit
boolean doit(CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context, CodeIterator iterator, int endPos) throws CannotCompileException
Visits each bytecode in the given range.- Throws:
CannotCompileException
-
loopBody
final boolean loopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context) throws CannotCompileException
- Throws:
CannotCompileException
-
edit
public void edit(NewExpr e) throws CannotCompileException
Edits a new expression (overridable). The default implementation performs nothing.- Parameters:
e- the new expression creating an object.- Throws:
CannotCompileException
-
edit
public void edit(NewArray a) throws CannotCompileException
Edits an expression for array creation (overridable). The default implementation performs nothing.- Parameters:
a- the new expression for creating an array.- Throws:
CannotCompileException
-
edit
public void edit(MethodCall m) throws CannotCompileException
Edits a method call (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
public void edit(ConstructorCall c) throws CannotCompileException
Edits a constructor call (overridable). The constructor call is eithersuper()orthis()included in a constructor body. The default implementation performs nothing.- Throws:
CannotCompileException- See Also:
edit(NewExpr)
-
edit
public void edit(FieldAccess f) throws CannotCompileException
Edits a field-access expression (overridable). Field access means both read and write. The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
public void edit(Instanceof i) throws CannotCompileException
Edits an instanceof expression (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
public void edit(Cast c) throws CannotCompileException
Edits an expression for explicit type casting (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
public void edit(Handler h) throws CannotCompileException
Edits a catch clause (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
-