Top Description Inners Constructors Methods
javassist.expr

public Class ExprEditor

extends Object
Class Inheritance
Imports
javassist.CannotCompileException, .CtClass, javassist.bytecode.BadBytecode, .CodeAttribute, .CodeIterator, .ExceptionTable, .MethodInfo, .Opcode

A 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 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.

See Also
javassist.CtClass#instrument(ExprEditor), javassist.CtMethod#instrument(ExprEditor), javassist.CtConstructor#instrument(ExprEditor), MethodCall, NewExpr, FieldAccess, javassist.CodeConverter

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class

Constructor Summary

AccessConstructor and Description
public
ExprEditor()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
doit(CtClass clazz, MethodInfo minfo)

Undocumented method.

pack-priv boolean
doit(CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context, CodeIterator iterator, int endPos)

Visits each bytecode in the given range.

public void
edit(NewExpr
the new expression creating an object.
e
)

Edits a new expression (overridable).

public void
edit(NewArray
the new expression for creating an array.
a
)

Edits an expression for array creation (overridable).

public void
edit(MethodCall m)

Edits a method call (overridable).

public void
edit(ConstructorCall c)

Edits a constructor call (overridable).

public void
edit(FieldAccess f)

Edits a field-access expression (overridable).

public void
edit(Instanceof i)

Edits an instanceof expression (overridable).

public void
edit(Cast c)

Edits an expression for explicit type casting (overridable).

public void
edit(Handler h)

Edits a catch clause (overridable).

pack-priv final boolean
loopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ExprEditorback to summary
public ExprEditor()

Default constructor. It does nothing.

Method Detail

doitback to summary
public boolean doit(CtClass clazz, MethodInfo minfo) throws CannotCompileException

Undocumented method. Do not use; internal-use only.

doitback to summary
pack-priv boolean doit(CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context, CodeIterator iterator, int endPos) throws CannotCompileException

Visits each bytecode in the given range.

editback to summary
public void edit(NewExpr e) throws CannotCompileException

Edits a new expression (overridable). The default implementation performs nothing.

Parameters
e:NewExpr

the new expression creating an object.

editback to summary
public void edit(NewArray a) throws CannotCompileException

Edits an expression for array creation (overridable). The default implementation performs nothing.

Parameters
a:NewArray

the new expression for creating an array.

editback to summary
public void edit(MethodCall m) throws CannotCompileException

Edits a method call (overridable). The default implementation performs nothing.

editback to summary
public void edit(ConstructorCall c) throws CannotCompileException

Edits a constructor call (overridable). The constructor call is either super() or this() included in a constructor body. The default implementation performs nothing.

See Also
edit(NewExpr)
editback 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.

editback to summary
public void edit(Instanceof i) throws CannotCompileException

Edits an instanceof expression (overridable). The default implementation performs nothing.

editback to summary
public void edit(Cast c) throws CannotCompileException

Edits an expression for explicit type casting (overridable). The default implementation performs nothing.

editback to summary
public void edit(Handler h) throws CannotCompileException

Edits a catch clause (overridable). The default implementation performs nothing.

loopBodyback to summary
pack-priv final boolean loopBody(CodeIterator iterator, CtClass clazz, MethodInfo minfo, ExprEditor.LoopContext context) throws CannotCompileException
javassist.expr back to summary

pack-priv final Class ExprEditor.LoopContext

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv int
pack-priv ExprEditor.NewOp

Constructor Summary

AccessConstructor and Description
pack-priv
LoopContext(int locals)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
updateMax(int locals, int stack)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxLocalsback to summary
pack-priv int maxLocals
maxStackback to summary
pack-priv int maxStack
newListback to summary
pack-priv ExprEditor.NewOp newList

Constructor Detail

LoopContextback to summary
pack-priv LoopContext(int locals)

Method Detail

updateMaxback to summary
pack-priv void updateMax(int locals, int stack)
javassist.expr back to summary

pack-priv final Class ExprEditor.NewOp

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv ExprEditor.NewOp
pack-priv int
pack-priv String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nextback to summary
pack-priv ExprEditor.NewOp next
posback to summary
pack-priv int pos
typeback to summary
pack-priv String type

Constructor Detail

NewOpback to summary
pack-priv NewOp(ExprEditor.NewOp n, int p, String t)