javassist.expr package).
Instances of this class specifies how to instrument of the
bytecodes representing a method body. They are passed to
CtClass.instrument() or
CtMethod.instrument() as a parameter.
Example:
ClassPool cp = ClassPool.getDefault();
CtClass point = cp.get("Point");
CtClass singleton = cp.get("Singleton");
CtClass client = cp.get("Client");
CodeConverter conv = new CodeConverter();
conv.replaceNew(point, singleton, "makePoint");
client.instrument(conv);
This program substitutes "Singleton.makePoint()"
for all occurrences of "new Point()"
appearing in methods declared in a Client class.
javassist.CtClass#instrument(CodeConverter) , javassist.CtMethod#instrument(CodeConverter) , javassist.expr.ExprEditor
| Modifier and Type | Class and Description |
|---|---|
| public static interface | CodeConverter.
Interface containing the method names to be used as array access replacements. |
| public static class | CodeConverter.
Default implementation of the |
| Modifier and Type | Field and Description |
|---|---|
| protected Transformer |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | |
| public void | insertAfterMethod(CtMethod
the method originally invoked. origMethod, CtMethod the method invoked after
afterMethod)origMethod.Inserts a call to another method after an existing method call. |
| public void | insertBeforeMethod(CtMethod
the method originally invoked. origMethod, CtMethod the method invoked before
beforeMethod)origMethod.Insert a call to another method before an existing method call. |
| public void | redirectFieldAccess(CtField
the originally accessed field. field, CtClass the class declaring the substituted field. newClass, String the name of the substituted field. newFieldname)Modify a method body so that field read/write expressions access a different field from the original one. |
| public void | redirectMethodCall(CtMethod
original method origMethod, CtMethod substituted method substMethod)Modify method invocations in a method body so that a different method will be invoked. |
| public void | redirectMethodCall(String
the old name of the method. oldMethodName, CtMethod the method with the new name. newMethod)Correct invocations to a method that has been renamed. |
| public void | redirectMethodCallToStatic(CtMethod
original method origMethod, CtMethod static method staticMethod)Redirect non-static method invocations in a method body to a static method. |
| public void | replaceArrayAccess(CtClass
the class containing the static methods. calledClass, CodeConverter.contains the names of the methods to replace
the different kinds of array access with. namesModify a method body, so that ALL accesses to an array are replaced with calls to static methods within another class. |
| public void | replaceFieldRead(CtField
the field. field, CtClass the class in which the static method is
declared. calledClass, String the name of the static method. calledMethod)Modify a method body so that an expression reading the specified field is replaced with a call to the specified static method. |
| public void | replaceFieldWrite(CtField
the field. field, CtClass the class in which the static method is
declared. calledClass, String the name of the static method. calledMethod)Modify a method body so that an expression writing the specified field is replaced with a call to the specified static method. |
| public void | replaceNew(CtClass
the instantiated class. newClass, CtClass the class in which the static method is
declared. calledClass, String the name of the static method. calledMethod)Modify a method body so that instantiation of the specified class is replaced with a call to the specified static method. |
| public void | replaceNew(CtClass oldClass, CtClass newClass)
Modify a method body so that instantiation of the class
specified by |
| transformers | back to summary |
|---|---|
| protected Transformer transformers | |
| CodeConverter | back to summary |
|---|---|
| public CodeConverter() | |
| doit | back to summary |
|---|---|
| protected void doit(CtClass clazz, MethodInfo minfo, ConstPool cp) throws CannotCompileException Performs code conversion. | |
| insertAfterMethod | back to summary |
|---|---|
| public void insertAfterMethod(CtMethod origMethod, CtMethod afterMethod) throws CannotCompileException Inserts a call to another method after an existing method call.
That "after" method must be static. The return type must be
class Point {
Point move(int x, int y) { ... }
}
Then the after method must be something like this: class Verbose {
static void print(Point target, int x, int y) { ... }
}
The Point p2 = p.move(x + y, 0); into the bytecode equivalent to: int tmp1 = x + y; int tmp2 = 0; Point p2 = p.move(tmp1, tmp2); Verbose.print(p, tmp1, tmp2); | |
| insertBeforeMethod | back to summary |
|---|---|
| public void insertBeforeMethod(CtMethod origMethod, CtMethod beforeMethod) throws CannotCompileException Insert a call to another method before an existing method call.
That "before" method must be static. The return type must be
class Point {
Point move(int x, int y) { ... }
}
Then the before method must be something like this: class Verbose {
static void print(Point target, int x, int y) { ... }
}
The Point p2 = p.move(x + y, 0); into the bytecode equivalent to: int tmp1 = x + y; int tmp2 = 0; Verbose.print(p, tmp1, tmp2); Point p2 = p.move(tmp1, tmp2); | |
| redirectFieldAccess | back to summary |
|---|---|
| public void redirectFieldAccess(CtField field, CtClass newClass, String newFieldname) Modify a method body so that field read/write expressions access a different field from the original one. Note that this method changes only the filed name and the class declaring the field; the type of the target object does not change. Therefore, the substituted field must be declared in the same class or a superclass of the original class. Also, | |
| redirectMethodCall | back to summary |
|---|---|
| public void redirectMethodCall(CtMethod origMethod, CtMethod substMethod) throws CannotCompileException Modify method invocations in a method body so that a different method will be invoked. Note that the target object, the parameters, or the type of invocation (static method call, interface call, or private method call) are not modified. Only the method name is changed. The substituted method must have the same signature that the original one has. If the original method is a static method, the substituted method must be static. | |
| redirectMethodCall | back to summary |
|---|---|
| public void redirectMethodCall(String oldMethodName, CtMethod newMethod) throws CannotCompileException Correct invocations to a method that has been renamed. If a method is renamed, calls to that method must be also modified so that the method with the new name will be called. The method must be declared in the same class before and after it is renamed. Note that the target object, the parameters, or the type of invocation (static method call, interface call, or private method call) are not modified. Only the method name is changed.
| |
| redirectMethodCallToStatic | back to summary |
|---|---|
| public void redirectMethodCallToStatic(CtMethod origMethod, CtMethod staticMethod) Redirect non-static method invocations in a method body to a static
method. The return type must be same with the originally invoked method.
As parameters, the static method receives
the target object and all the parameters to the originally invoked
method. For example, if the originally invoked method is
class Point {
Point move(int x, int y) { ... }
}
Then the static method must be something like this: class Verbose {
static Point print(Point target, int x, int y) { ... }
}
The Point p2 = p.move(x + y, 0); into the bytecode equivalent to: Point p2 = Verbose.print(p, x + y, 0); | |
| replaceArrayAccess | back to summary |
|---|---|
| public void replaceArrayAccess(CtClass calledClass, CodeConverter. Modify a method body, so that ALL accesses to an array are replaced with
calls to static methods within another class. In the case of reading an
element from the array, this is replaced with a call to a static method with
the array and the index as arguments, the return value is the value read from
the array. If writing to an array, this is replaced with a call to a static
method with the array, index and new value as parameters, the return value of
the static method is The A default implementation of If we have the following class: class POJO{
int[] ints = new int[]{1, 2, 3, 4, 5};
long[] longs = new int[]{10, 20, 30};
Object objects = new Object[]{true, false};
Integer[] integers = new Integer[]{new Integer(10)};
}
and this is accessed as:
POJO p = new POJO(); //Write to int array p.ints[2] = 7; //Read from int array int i = p.ints[2]; //Write to long array p.longs[2] = 1000L; //Read from long array long l = p.longs[2]; //Write to Object array p.objects[2] = "Hello"; //Read from Object array Object o = p.objects[2]; //Write to Integer array Integer integer = new Integer(5); p.integers[0] = integer; //Read from Object array integer = p.integers[0];Following instrumentation we will have POJO p = new POJO(); //Write to int array ArrayAdvisor.arrayWriteInt(p.ints, 2, 7); //Read from int array int i = ArrayAdvisor.arrayReadInt(p.ints, 2); //Write to long array ArrayAdvisor.arrayWriteLong(p.longs, 2, 1000L); //Read from long array long l = ArrayAdvisor.arrayReadLong(p.longs, 2); //Write to Object array ArrayAdvisor.arrayWriteObject(p.objects, 2, "Hello"); //Read from Object array Object o = ArrayAdvisor.arrayReadObject(p.objects, 2); //Write to Integer array Integer integer = new Integer(5); ArrayAdvisor.arrayWriteObject(p.integers, 0, integer); //Read from Object array integer = ArrayAdvisor.arrayWriteObject(p.integers, 0);
| |
| replaceFieldRead | back to summary |
|---|---|
| public void replaceFieldRead(CtField field, CtClass calledClass, String calledMethod) Modify a method body so that an expression reading the specified field is replaced with a call to the specified static method. This static method receives the target object of the original read expression as a parameter. It must return a value of the same type as the field. For example, the program below Point p = new Point(); int newX = p.x + 3; can be translated into: Point p = new Point(); int newX = Accessor.readX(p) + 3; where public class Accessor {
public static int readX(Object target) { ... }
}
The type of the parameter of | |
| replaceFieldWrite | back to summary |
|---|---|
| public void replaceFieldWrite(CtField field, CtClass calledClass, String calledMethod) Modify a method body so that an expression writing the specified
field is replaced with a call to the specified static method.
This static method receives two parameters: the target object of
the original
write expression and the assigned value. The return type of the
static method is For example, the program below Point p = new Point(); p.x = 3; can be translated into: Point p = new Point(); Accessor.writeX(3); where public class Accessor {
public static void writeX(Object target, int value) { ... }
}
The type of the first parameter of | |
| replaceNew | back to summary |
|---|---|
| public void replaceNew(CtClass newClass, CtClass calledClass, String calledMethod) Modify a method body so that instantiation of the specified class
is replaced with a call to the specified static method. For example,
new Point(x, y)in the method body with: Singleton.createPoint(x, y) This enables to intercept instantiation of public static Point createPoint(int x, int y) {
if (aPoint == null)
aPoint = new Point(x, y);
return aPoint;
}
The static method call substituted for the original The return type of the substituted static method must be
the exactly same as the type of the instantiated class specified by
| |
| replaceNew | back to summary |
|---|---|
| public void replaceNew(CtClass oldClass, CtClass newClass) Modify a method body so that instantiation of the class
specified by new Point(x, y)in the method body with: new Point2(x, y) Note that | |
| Modifier and Type | Method and Description |
|---|---|
| public String | byteOrBooleanRead()
Returns the name of a static method with the signature
|
| public String | byteOrBooleanWrite()
Returns the name of a static method with the signature
|
| public String | Returns: the name of a static method with the signature(Ljava/lang/Object;I)C to replace reading from a char[].
|
| public String | charWrite()
Returns the name of a static method with the signature
|
| public String | doubleRead()
Returns the name of a static method with the signature
|
| public String | doubleWrite()
Returns the name of a static method with the signature
|
| public String | floatRead()
Returns the name of a static method with the signature
|
| public String | floatWrite()
Returns the name of a static method with the signature
|
| public String | intRead()
Returns the name of a static method with the signature
|
| public String | intWrite()
Returns the name of a static method with the signature
|
| public String | longRead()
Returns the name of a static method with the signature
|
| public String | longWrite()
Returns the name of a static method with the signature
|
| public String | objectRead()
Returns the name of a static method with the signature
|
| public String | objectWrite()
Returns the name of a static method with the signature
|
| public String | shortRead()
Returns the name of a static method with the signature
|
| public String | shortWrite()
Returns the name of a static method with the signature
|
| byteOrBooleanRead | back to summary |
|---|---|
| public String byteOrBooleanRead() Returns the name of a static method with the signature
| |
| byteOrBooleanWrite | back to summary |
|---|---|
| public String byteOrBooleanWrite() Returns the name of a static method with the signature
| |
| charRead | back to summary |
|---|---|
public String charRead()
| |
| charWrite | back to summary |
|---|---|
| public String charWrite() Returns the name of a static method with the signature
| |
| doubleRead | back to summary |
|---|---|
| public String doubleRead() Returns the name of a static method with the signature
| |
| doubleWrite | back to summary |
|---|---|
| public String doubleWrite() Returns the name of a static method with the signature
| |
| floatRead | back to summary |
|---|---|
| public String floatRead() Returns the name of a static method with the signature
| |
| floatWrite | back to summary |
|---|---|
| public String floatWrite() Returns the name of a static method with the signature
| |
| intRead | back to summary |
|---|---|
| public String intRead() Returns the name of a static method with the signature
| |
| intWrite | back to summary |
|---|---|
| public String intWrite() Returns the name of a static method with the signature
| |
| longRead | back to summary |
|---|---|
| public String longRead() Returns the name of a static method with the signature
| |
| longWrite | back to summary |
|---|---|
| public String longWrite() Returns the name of a static method with the signature
| |
| objectRead | back to summary |
|---|---|
| public String objectRead() Returns the name of a static method with the signature
| |
| objectWrite | back to summary |
|---|---|
| public String objectWrite() Returns the name of a static method with the signature
| |
| shortRead | back to summary |
|---|---|
| public String shortRead() Returns the name of a static method with the signature
| |
| shortWrite | back to summary |
|---|---|
| public String shortWrite() Returns the name of a static method with the signature
| |
ArrayAccessReplacementMethodNames
interface giving default values for method names to be used for replacing
accesses to array elements.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public String | byteOrBooleanRead()
Implements javassist. Returns "arrayReadByteOrBoolean" as the name of the static method with the signature (Ljava/lang/Object;I)B to replace reading from a byte[]. |
| public String | byteOrBooleanWrite()
Implements javassist. Returns "arrayWriteByteOrBoolean" as the name of the static method with the signature (Ljava/lang/Object;IB)V to replace writing to a byte[]. |
| public String | charRead()
Implements javassist. Returns "arrayReadChar" as the name of the static method with the signature (Ljava/lang/Object;I)C to replace reading from a char[]. |
| public String | charWrite()
Implements javassist. Returns "arrayWriteChar" as the name of the static method with the signature (Ljava/lang/Object;IC)V to replace writing to a byte[]. |
| public String | doubleRead()
Implements javassist. Returns "arrayReadDouble" as the name of the static method with the signature (Ljava/lang/Object;I)D to replace reading from a double[]. |
| public String | doubleWrite()
Implements javassist. Returns "arrayWriteDouble" as the name of the static method with the signature (Ljava/lang/Object;ID)V to replace writing to a double[]. |
| public String | floatRead()
Implements javassist. Returns "arrayReadFloat" as the name of the static method with the signature (Ljava/lang/Object;I)F to replace reading from a float[]. |
| public String | floatWrite()
Implements javassist. Returns "arrayWriteFloat" as the name of the static method with the signature (Ljava/lang/Object;IF)V to replace writing to a float[]. |
| public String | intRead()
Implements javassist. Returns "arrayReadInt" as the name of the static method with the signature (Ljava/lang/Object;I)I to replace reading from a int[]. |
| public String | intWrite()
Implements javassist. Returns "arrayWriteInt" as the name of the static method with the signature (Ljava/lang/Object;II)V to replace writing to a int[]. |
| public String | longRead()
Implements javassist. Returns "arrayReadLong" as the name of the static method with the signature (Ljava/lang/Object;I)J to replace reading from a long[]. |
| public String | longWrite()
Implements javassist. Returns "arrayWriteLong" as the name of the static method with the signature (Ljava/lang/Object;IJ)V to replace writing to a long[]. |
| public String | objectRead()
Implements javassist. Returns "arrayReadObject" as the name of the static method with the signature (Ljava/lang/Object;I)Ljava/lang/Object; to replace reading from a Object[] (or any subclass of object). |
| public String | objectWrite()
Implements javassist. Returns "arrayWriteObject" as the name of the static method with the signature (Ljava/lang/Object;ILjava/lang/Object;)V to replace writing to a Object[] (or any subclass of object). |
| public String | shortRead()
Implements javassist. Returns "arrayReadShort" as the name of the static method with the signature (Ljava/lang/Object;I)S to replace reading from a short[]. |
| public String | shortWrite()
Implements javassist. Returns "arrayWriteShort" as the name of the static method with the signature (Ljava/lang/Object;IS)V to replace writing to a short[]. |
| DefaultArrayAccessReplacementMethodNames | back to summary |
|---|---|
| public DefaultArrayAccessReplacementMethodNames() | |
| byteOrBooleanRead | back to summary |
|---|---|
| public String byteOrBooleanRead() Implements javassist. Returns "arrayReadByteOrBoolean" as the name of the static method with the signature (Ljava/lang/Object;I)B to replace reading from a byte[].
| |
| byteOrBooleanWrite | back to summary |
|---|---|
| public String byteOrBooleanWrite() Implements javassist. Returns "arrayWriteByteOrBoolean" as the name of the static method with the signature (Ljava/lang/Object;IB)V to replace writing to a byte[].
| |
| charRead | back to summary |
|---|---|
| public String charRead() Implements javassist. Returns "arrayReadChar" as the name of the static method with the signature (Ljava/lang/Object;I)C to replace reading from a char[].
| |
| charWrite | back to summary |
|---|---|
| public String charWrite() Implements javassist. Returns "arrayWriteChar" as the name of the static method with the signature (Ljava/lang/Object;IC)V to replace writing to a byte[].
| |
| doubleRead | back to summary |
|---|---|
| public String doubleRead() Implements javassist. Returns "arrayReadDouble" as the name of the static method with the signature (Ljava/lang/Object;I)D to replace reading from a double[].
| |
| doubleWrite | back to summary |
|---|---|
| public String doubleWrite() Implements javassist. Returns "arrayWriteDouble" as the name of the static method with the signature (Ljava/lang/Object;ID)V to replace writing to a double[].
| |
| floatRead | back to summary |
|---|---|
| public String floatRead() Implements javassist. Returns "arrayReadFloat" as the name of the static method with the signature (Ljava/lang/Object;I)F to replace reading from a float[].
| |
| floatWrite | back to summary |
|---|---|
| public String floatWrite() Implements javassist. Returns "arrayWriteFloat" as the name of the static method with the signature (Ljava/lang/Object;IF)V to replace writing to a float[].
| |
| intRead | back to summary |
|---|---|
| public String intRead() Implements javassist. Returns "arrayReadInt" as the name of the static method with the signature (Ljava/lang/Object;I)I to replace reading from a int[].
| |
| intWrite | back to summary |
|---|---|
| public String intWrite() Implements javassist. Returns "arrayWriteInt" as the name of the static method with the signature (Ljava/lang/Object;II)V to replace writing to a int[].
| |
| longRead | back to summary |
|---|---|
| public String longRead() Implements javassist. Returns "arrayReadLong" as the name of the static method with the signature (Ljava/lang/Object;I)J to replace reading from a long[].
| |
| longWrite | back to summary |
|---|---|
| public String longWrite() Implements javassist. Returns "arrayWriteLong" as the name of the static method with the signature (Ljava/lang/Object;IJ)V to replace writing to a long[].
| |
| objectRead | back to summary |
|---|---|
| public String objectRead() Implements javassist. Returns "arrayReadObject" as the name of the static method with the signature (Ljava/lang/Object;I)Ljava/lang/Object; to replace reading from a Object[] (or any subclass of object).
| |
| objectWrite | back to summary |
|---|---|
| public String objectWrite() Implements javassist. Returns "arrayWriteObject" as the name of the static method with the signature (Ljava/lang/Object;ILjava/lang/Object;)V to replace writing to a Object[] (or any subclass of object).
| |
| shortRead | back to summary |
|---|---|
| public String shortRead() Implements javassist. Returns "arrayReadShort" as the name of the static method with the signature (Ljava/lang/Object;I)S to replace reading from a short[].
| |
| shortWrite | back to summary |
|---|---|
| public String shortWrite() Implements javassist. Returns "arrayWriteShort" as the name of the static method with the signature (Ljava/lang/Object;IS)V to replace writing to a short[].
| |