javassist

Class CtBehavior

public abstract class CtBehavior extends CtMember

CtBehavior represents a method, a constructor, or a static constructor (class initializer). It is the abstract super class of CtMethod and CtConstructor.
Method Summary
voidaddCatch(String src, CtClass exceptionType)
Adds a catch clause that handles an exception thrown in the body.
voidaddCatch(String src, CtClass exceptionType, String exceptionName)
Adds a catch clause that handles an exception thrown in the body.
voidaddLocalVariable(String name, CtClass type)
Declares a new local variable.
Object[]getAnnotations()
Returns the annotations associated with this method or constructor.
byte[]getAttribute(String name)
Obtains an attribute with the given name.
Object[]getAvailableAnnotations()
Returns the annotations associated with this method or constructor.
Object[][]getAvailableParameterAnnotations()
Returns the parameter annotations associated with this method or constructor.
CtClass[]getExceptionTypes()
Obtains exceptions that this method/constructor may throw.
abstract StringgetLongName()
Returns the method or constructor name followed by parameter types such as javassist.CtBehavior.stBody(String).
MethodInfogetMethodInfo()
Returns the MethodInfo representing this method/constructor in the class file.
MethodInfogetMethodInfo2()
Returns the MethodInfo representing the method/constructor in the class file (read only).
intgetModifiers()
Obtains the modifiers of the method/constructor.
Object[][]getParameterAnnotations()
Returns the parameter annotations associated with this method or constructor.
CtClass[]getParameterTypes()
Obtains parameter types of this method/constructor.
StringgetSignature()
Returns the method signature (the parameter types and the return type).
voidinsertAfter(String src)
Inserts bytecode at the end of the body.
voidinsertAfter(String src, boolean asFinally)
Inserts bytecode at the end of the body.
intinsertAt(int lineNum, String src)
Inserts bytecode at the specified line in the body.
intinsertAt(int lineNum, boolean modify, String src)
Inserts bytecode at the specified line in the body.
voidinsertBefore(String src)
Inserts bytecode at the beginning of the body.
voidinstrument(CodeConverter converter)
Modifies the method/constructor body.
voidinstrument(ExprEditor editor)
Modifies the method/constructor body.
abstract booleanisEmpty()
Returns true if the body is empty.
voidsetAttribute(String name, byte[] data)
Adds an attribute.
voidsetBody(String src)
Sets a method/constructor body.
voidsetBody(String src, String delegateObj, String delegateMethod)
Sets a method/constructor body.
voidsetExceptionTypes(CtClass[] types)
Sets exceptions that this method/constructor may throw.
voidsetModifiers(int mod)
Sets the encoded modifiers of the method/constructor.
voiduseCflow(String name)
Declares to use $cflow for this method/constructor.

Method Detail

addCatch

public void addCatch(String src, CtClass exceptionType)
Adds a catch clause that handles an exception thrown in the body. The catch clause must end with a return or throw statement.

Parameters: src the source code representing the catch clause. It must be a single statement or block. exceptionType the type of the exception handled by the catch clause.

addCatch

public void addCatch(String src, CtClass exceptionType, String exceptionName)
Adds a catch clause that handles an exception thrown in the body. The catch clause must end with a return or throw statement.

Parameters: src the source code representing the catch clause. It must be a single statement or block. exceptionType the type of the exception handled by the catch clause. exceptionName the name of the variable containing the caught exception, for example, $e.

addLocalVariable

public void addLocalVariable(String name, CtClass type)
Declares a new local variable. The scope of this variable is the whole method body. The initial value of that variable is not set. The declared variable can be accessed in the code snippet inserted by insertBefore(), insertAfter(), etc.

If the second parameter asFinally to insertAfter() is true, the declared local variable is not visible from the code inserted by insertAfter().

Parameters: name the name of the variable type the type of the variable

See Also: insertBefore insertAfter

getAnnotations

public Object[] getAnnotations()
Returns the annotations associated with this method or constructor.

Returns: an array of annotation-type objects.

Since: 3.1

See Also: getAvailableAnnotations

getAttribute

public byte[] getAttribute(String name)
Obtains an attribute with the given name. If that attribute is not found in the class file, this method returns null.

Note that an attribute is a data block specified by the class file format. It is not an annotation. See AttributeInfo.

Parameters: name attribute name

getAvailableAnnotations

public Object[] getAvailableAnnotations()
Returns the annotations associated with this method or constructor. If any annotations are not on the classpath, they are not included in the returned array.

Returns: an array of annotation-type objects.

Since: 3.3

See Also: getAnnotations

getAvailableParameterAnnotations

public Object[][] getAvailableParameterAnnotations()
Returns the parameter annotations associated with this method or constructor. If any annotations are not on the classpath, they are not included in the returned array.

Returns: an array of annotation-type objects. The length of the returned array is equal to the number of the formal parameters. If each parameter has no annotation, the elements of the returned array are empty arrays.

Since: 3.3

See Also: getParameterAnnotations getAvailableAnnotations

getExceptionTypes

public CtClass[] getExceptionTypes()
Obtains exceptions that this method/constructor may throw.

Returns: a zero-length array if there is no throws clause.

getLongName

public abstract String getLongName()
Returns the method or constructor name followed by parameter types such as javassist.CtBehavior.stBody(String).

Since: 3.5

getMethodInfo

public MethodInfo getMethodInfo()
Returns the MethodInfo representing this method/constructor in the class file.

getMethodInfo2

public MethodInfo getMethodInfo2()
Returns the MethodInfo representing the method/constructor in the class file (read only). Normal applications do not need calling this method. Use getMethodInfo().

The MethodInfo object obtained by this method is read only. Changes to this object might not be reflected on a class file generated by toBytecode(), toClass(), etc in CtClass.

This method is available even if the CtClass containing this method is frozen. However, if the class is frozen, the MethodInfo might be also pruned.

See Also: getMethodInfo isFrozen prune

getModifiers

public int getModifiers()
Obtains the modifiers of the method/constructor.

Returns: modifiers encoded with javassist.Modifier.

See Also: Modifier

getParameterAnnotations

public Object[][] getParameterAnnotations()
Returns the parameter annotations associated with this method or constructor.

Returns: an array of annotation-type objects. The length of the returned array is equal to the number of the formal parameters. If each parameter has no annotation, the elements of the returned array are empty arrays.

Since: 3.1

See Also: getAvailableParameterAnnotations getAnnotations

getParameterTypes

public CtClass[] getParameterTypes()
Obtains parameter types of this method/constructor.

getSignature

public String getSignature()
Returns the method signature (the parameter types and the return type). The method signature is represented by a character string called method descriptor, which is defined in the JVM specification. If two methods/constructors have the same parameter types and the return type, getSignature() returns the same string (the return type of constructors is void).

Note that the returned string is not the type signature contained in the SignatureAttirbute. It is a descriptor. To obtain a type signature, call the following methods:

See Also: Descriptor SignatureAttribute

insertAfter

public void insertAfter(String src)
Inserts bytecode at the end of the body. The bytecode is inserted just before every return insturction. It is not executed when an exception is thrown.

Parameters: src the source code representing the inserted bytecode. It must be a single statement or block.

insertAfter

public void insertAfter(String src, boolean asFinally)
Inserts bytecode at the end of the body. The bytecode is inserted just before every return insturction.

Parameters: src the source code representing the inserted bytecode. It must be a single statement or block. asFinally true if the inserted bytecode is executed not only when the control normally returns but also when an exception is thrown. If this parameter is true, the inserted code cannot access local variables.

insertAt

public int insertAt(int lineNum, String src)
Inserts bytecode at the specified line in the body. It is equivalent to:
insertAt(lineNum, true, src)
See this method as well.

Parameters: lineNum the line number. The bytecode is inserted at the beginning of the code at the line specified by this line number. src the source code representing the inserted bytecode. It must be a single statement or block.

Returns: the line number at which the bytecode has been inserted.

See Also: CtBehavior

insertAt

public int insertAt(int lineNum, boolean modify, String src)
Inserts bytecode at the specified line in the body.

If there is not a statement at the specified line, the bytecode might be inserted at the line including the first statement after that line specified. For example, if there is only a closing brace at that line, the bytecode would be inserted at another line below. To know exactly where the bytecode will be inserted, call with modify set to false.

Parameters: lineNum the line number. The bytecode is inserted at the beginning of the code at the line specified by this line number. modify if false, this method does not insert the bytecode. It instead only returns the line number at which the bytecode would be inserted. src the source code representing the inserted bytecode. It must be a single statement or block. If modify is false, the value of src can be null.

Returns: the line number at which the bytecode has been inserted.

insertBefore

public void insertBefore(String src)
Inserts bytecode at the beginning of the body.

If this object represents a constructor, the bytecode is inserted before a constructor in the super class or this class is called. Therefore, the inserted bytecode is subject to constraints described in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed). For example, it cannot access instance fields or methods although it may assign a value to an instance field directly declared in this class. Accessing static fields and methods is allowed. Use insertBeforeBody() in CtConstructor.

Parameters: src the source code representing the inserted bytecode. It must be a single statement or block.

See Also: insertBeforeBody

instrument

public void instrument(CodeConverter converter)
Modifies the method/constructor body.

Parameters: converter specifies how to modify.

instrument

public void instrument(ExprEditor editor)
Modifies the method/constructor body.

Parameters: editor specifies how to modify.

isEmpty

public abstract boolean isEmpty()
Returns true if the body is empty.

setAttribute

public void setAttribute(String name, byte[] data)
Adds an attribute. The attribute is saved in the class file.

Note that an attribute is a data block specified by the class file format. It is not an annotation. See AttributeInfo.

Parameters: name attribute name data attribute value

setBody

public void setBody(String src)
Sets a method/constructor body.

Parameters: src the source code representing the body. It must be a single statement or block. If it is null, the substituted body does nothing except returning zero or null.

setBody

public void setBody(String src, String delegateObj, String delegateMethod)
Sets a method/constructor body.

Parameters: src the source code representing the body. It must be a single statement or block. If it is null, the substituted body does nothing except returning zero or null. delegateObj the source text specifying the object that is called on by $proceed(). delegateMethod the name of the method that is called by $proceed().

setExceptionTypes

public void setExceptionTypes(CtClass[] types)
Sets exceptions that this method/constructor may throw.

setModifiers

public void setModifiers(int mod)
Sets the encoded modifiers of the method/constructor.

Changing the modifiers may cause a problem. For example, if a non-static method is changed to static, the method will be rejected by the bytecode verifier.

See Also: Modifier

useCflow

public void useCflow(String name)
Declares to use $cflow for this method/constructor. If $cflow is used, the class files modified with Javassist requires a support class javassist.runtime.Cflow at runtime (other Javassist classes are not required at runtime).

Every $cflow variable is given a unique name. For example, if the given name is "Point.paint", then the variable is indicated by $cflow(Point.paint).

Parameters: name $cflow name. It can include alphabets, numbers, _, $, and . (dot).

See Also: Cflow

Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.