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 in
CtMethod, 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 in ExprEdit.
edit() can inspect and modify the given expression.
The modification is reflected on the original method body. If
edit() 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 cm and it prints the names and the line numbers of the
methods declared in class Point. This code does not modify
the body of the method represented by cm. If the method
body must be modified, call replace()
in MethodCall.
javassist.CtClass#instrument(ExprEditor) , javassist.CtMethod#instrument(ExprEditor) , javassist.CtConstructor#instrument(ExprEditor) , MethodCall, NewExpr, FieldAccess, javassist.CodeConverter
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| pack-priv boolean | doit(CtClass clazz, MethodInfo minfo, ExprEditor.
Visits each bytecode in the given range. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| pack-priv final boolean |
| ExprEditor | back to summary |
|---|---|
| public ExprEditor() Default constructor. It does nothing. | |
| doit | back to summary |
|---|---|
| public boolean doit(CtClass clazz, MethodInfo minfo) throws CannotCompileException Undocumented method. Do not use; internal-use only. | |
| doit | back to summary |
|---|---|
| pack-priv boolean doit(CtClass clazz, MethodInfo minfo, ExprEditor. Visits each bytecode in the given range. | |
| edit | back to summary |
|---|---|
| public void edit(NewExpr e) throws CannotCompileException Edits a
| |
| edit | back to summary |
|---|---|
| public void edit(NewArray a) throws CannotCompileException Edits an expression for array creation (overridable). The default implementation performs nothing.
| |
| edit | back to summary |
|---|---|
| public void edit(MethodCall m) throws CannotCompileException Edits a method call (overridable). The default implementation performs nothing. | |
| edit | back to summary |
|---|---|
| public void edit(ConstructorCall c) throws CannotCompileException Edits a constructor call (overridable).
The constructor call is either
| |
| edit | back to summary |
|---|---|
| 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. | |
| edit | back to summary |
|---|---|
| public void edit(Instanceof i) throws CannotCompileException Edits an instanceof expression (overridable). The default implementation performs nothing. | |
| edit | back to summary |
|---|---|
| public void edit(Cast c) throws CannotCompileException Edits an expression for explicit type casting (overridable). The default implementation performs nothing. | |
| edit | back to summary |
|---|---|
| public void edit(Handler h) throws CannotCompileException Edits a catch clause (overridable). The default implementation performs nothing. | |
| loopBody | back to summary |
|---|---|
| pack-priv final boolean loopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, ExprEditor. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| pack-priv int | |
| pack-priv ExprEditor. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void |
| maxLocals | back to summary |
|---|---|
| pack-priv int maxLocals | |
| maxStack | back to summary |
|---|---|
| pack-priv int maxStack | |
| newList | back to summary |
|---|---|
| pack-priv ExprEditor. | |
| LoopContext | back to summary |
|---|---|
| pack-priv LoopContext(int locals) | |
| updateMax | back to summary |
|---|---|
| pack-priv void updateMax(int locals, int stack) | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv ExprEditor. | |
| pack-priv int | |
| pack-priv String |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| next | back to summary |
|---|---|
| pack-priv ExprEditor. | |
| pos | back to summary |
|---|---|
| pack-priv int pos | |
| type | back to summary |
|---|---|
| pack-priv String type | |
| NewOp | back to summary |
|---|---|
| pack-priv NewOp(ExprEditor. | |