Class ExprEditor
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
.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
doit
(CtClass clazz, MethodInfo minfo) Undocumented method.void
Edits an expression for explicit type casting (overridable).void
Edits a constructor call (overridable).void
edit
(FieldAccess f) Edits a field-access expression (overridable).void
Edits a catch clause (overridable).void
edit
(Instanceof i) Edits an instanceof expression (overridable).void
edit
(MethodCall m) Edits a method call (overridable).void
Edits an expression for array creation (overridable).void
Edits anew
expression (overridable).
-
Constructor Details
-
ExprEditor
public ExprEditor()Default constructor. It does nothing.
-
-
Method Details
-
doit
Undocumented method. Do not use; internal-use only.- Throws:
CannotCompileException
-
edit
Edits anew
expression (overridable). The default implementation performs nothing.- Parameters:
e
- thenew
expression creating an object.- Throws:
CannotCompileException
-
edit
Edits an expression for array creation (overridable). The default implementation performs nothing.- Parameters:
a
- thenew
expression for creating an array.- Throws:
CannotCompileException
-
edit
Edits a method call (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
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
Edits a field-access expression (overridable). Field access means both read and write. The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
Edits an instanceof expression (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
Edits an expression for explicit type casting (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-
edit
Edits a catch clause (overridable). The default implementation performs nothing.- Throws:
CannotCompileException
-