Top Description Fields Constructors Methods
javassist

public abstract Class CtBehavior

extends CtMember
Class Inheritance
Known Direct Subclasses
javassist.CtConstructor, javassist.CtMethod
Imports
javassist.bytecode.AccessFlag, .AnnotationsAttribute, .AttributeInfo, .BadBytecode, .Bytecode, .CodeAttribute, .CodeIterator, .ConstPool, .Descriptor, .ExceptionsAttribute, .LineNumberAttribute, .LocalVariableAttribute, .LocalVariableTypeAttribute, .MethodInfo, .Opcode, .ParameterAnnotationsAttribute, .SignatureAttribute, .StackMap, .StackMapTable, javassist.compiler.CompileError, .Javac, javassist.expr.ExprEditor

CtBehavior represents a method, a constructor, or a static constructor (class initializer). It is the abstract super class of CtMethod and CtConstructor.

To directly read or modify bytecode, obtain MethodInfo objects.

See Also
getMethodInfo()

Field Summary

Modifier and TypeField and Description
protected MethodInfo
Inherited from javassist.CtMember:
declaringClassnext

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
addCatch(String
the source code representing the catch clause. It must be a single statement or block.
src
,
CtClass
the type of the exception handled by the catch clause.
exceptionType
)

Adds a catch clause that handles an exception thrown in the body.

public void
addCatch(String
the source code representing the catch clause. It must be a single statement or block.
src
,
CtClass
the type of the exception handled by the catch clause.
exceptionType
,
String
the name of the variable containing the caught exception, for example, $e.
exceptionName
)

Adds a catch clause that handles an exception thrown in the body.

public void
addLocalVariable(String
the name of the variable
name
,
CtClass
the type of the variable
type
)

Declares a new local variable.

public void
addParameter(CtClass type)

Appends a new parameter, which becomes the last parameter.

private void
addParameter2(int where, CtClass type, String desc)

pack-priv void
copy(CtBehavior src, boolean
true if this is a constructor.
isCons
,
ClassMap map)

protected void
extendToString(StringBuilder buffer)

Implements abstract javassist.CtMember.extendToString.

Invoked by toString() to add to the buffer and provide the complete value.

public Object

Returns:

the annotation if found, otherwise null.
getAnnotation
(Class<?>
the annotation class.
clz
)

Implements abstract javassist.CtMember.getAnnotation.

Returns the annotation if the class has the specified annotation class.

public Object[]

Returns:

an array of annotation-type objects.
getAnnotations
()

Implements abstract javassist.CtMember.getAnnotations.

Returns the annotations associated with this method or constructor.

private Object[]
getAnnotations(boolean ignoreNotFound)

public byte[]
getAttribute(String
attribute name
name
)

Implements abstract javassist.CtMember.getAttribute.

Obtains an attribute with the given name.

public Object[]

Returns:

an array of annotation-type objects.
getAvailableAnnotations
()

Implements abstract javassist.CtMember.getAvailableAnnotations.

Returns the annotations associated with this method or constructor.

public Object[][]

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.
getAvailableParameterAnnotations
()

Returns the parameter annotations associated with this method or constructor.

public CtClass[]

Returns:

a zero-length array if there is no throws clause.
getExceptionTypes
()

Obtains exceptions that this method/constructor may throw.

public String
getGenericSignature()

Implements abstract javassist.CtMember.getGenericSignature.

Returns the generic signature of the method.

public abstract String
getLongName()

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

public MethodInfo
getMethodInfo()

Returns the MethodInfo representing this method/constructor in the class file.

public MethodInfo
getMethodInfo2()

Returns the MethodInfo representing the method/constructor in the class file (read only).

public int

Returns:

modifiers encoded with javassist.Modifier.
getModifiers
()

Implements abstract javassist.CtMember.getModifiers.

Obtains the modifiers of the method/constructor.

public Object[][]

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.
getParameterAnnotations
()

Returns the parameter annotations associated with this method or constructor.

pack-priv Object[][]
getParameterAnnotations(boolean ignoreNotFound)

public CtClass[]
getParameterTypes()

Obtains parameter types of this method/constructor.

pack-priv CtClass
getReturnType0()

Obtains the type of the returned value.

public String
getSignature()

Implements abstract javassist.CtMember.getSignature.

Returns the method signature (the parameter types and the return type).

pack-priv int
public boolean

Returns:

true if the annotation is found, otherwise false.
hasAnnotation
(String
the name of annotation type.
typeName
)

Implements abstract javassist.CtMember.hasAnnotation.

Returns true if the class has the specified annotation type.

public void
insertAfter(String
the source code representing the inserted bytecode. It must be a single statement or block.
src
)

Inserts bytecode at the end of the body.

public void
insertAfter(String
the source code representing the inserted bytecode. It must be a single statement or block.
src
,
boolean
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.
asFinally
)

Inserts bytecode at the end of the body.

public void
insertAfter(String
the source code representing the inserted bytecode. It must be a single statement or block.
src
,
boolean
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.
asFinally
,
boolean
if true, redundant bytecode will be generated. the redundancy is necessary when some compilers (Kotlin?) generate the original bytecode. The other insertAfter methods calls this method with false for this parameter. A tip is to pass this.getDeclaringClass().isKotlin() to this parameter.
redundant
)

Inserts bytecode at the end of the body.

private int
insertAfterAdvice(Bytecode code, Javac jv, String src, ConstPool cp, CtClass rtype, int varNo)

private int
insertAfterHandler(boolean asFinally, Bytecode b, CtClass rtype, int returnVarNo, Javac javac, String src)

public int

Returns:

the line number at which the bytecode has been inserted.
insertAt
(int
the line number. The bytecode is inserted at the beginning of the code at the line specified by this line number.
lineNum
,
String
the source code representing the inserted bytecode. It must be a single statement or block.
src
)

Inserts bytecode at the specified line in the body.

public int

Returns:

the line number at which the bytecode has been inserted.
insertAt
(int
the line number. The bytecode is inserted at the beginning of the code at the line specified by this line number.
lineNum
,
boolean
if false, this method does not insert the bytecode. It instead only returns the line number at which the bytecode would be inserted.
modify
,
String
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.
src
)

Inserts bytecode at the specified line in the body.

public void
insertBefore(String
the source code representing the inserted bytecode. It must be a single statement or block.
src
)

Inserts bytecode at the beginning of the body.

private void
insertBefore(String src, boolean rebuild)

private void
insertGoto(CodeIterator iterator, int subr, int pos)

public void
insertParameter(CtClass type)

Inserts a new parameter, which becomes the first parameter.

public void
instrument(CodeConverter
specifies how to modify.
converter
)

Modifies the method/constructor body.

public void
instrument(ExprEditor
specifies how to modify.
editor
)

Modifies the method/constructor body.

public abstract boolean
isEmpty()

Returns true if the body is empty.

public void
setAttribute(String
attribute name
name
,
byte[]
attribute value
data
)

Implements abstract javassist.CtMember.setAttribute.

Adds an attribute.

public void
setBody(String
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.
src
)

Sets a method/constructor body.

public void
setBody(String
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.
src
,
String
the source text specifying the object that is called on by $proceed().
delegateObj
,
String
the name of the method that is called by $proceed().
delegateMethod
)

Sets a method/constructor body.

pack-priv static void
setBody0(CtClass srcClass, MethodInfo srcInfo, CtClass destClass, MethodInfo destInfo, ClassMap map)

public void
setExceptionTypes(CtClass[] types)

Sets exceptions that this method/constructor may throw.

public void
setGenericSignature(String
a new generic signature.
sig
)

Implements abstract javassist.CtMember.setGenericSignature.

Set the generic signature of the method.

public void
setModifiers(int mod)

Implements abstract javassist.CtMember.setModifiers.

Sets the encoded modifiers of the method/constructor.

public void
useCflow(String
$cflow name. It can include alphabets, numbers, _, $, and . (dot).
name
)

Declares to use $cflow for this method/constructor.

Inherited from javassist.CtMember:
getDeclaringClassgetNamehasAnnotationnameReplacednexttoStringvisibleFrom

Field Detail

methodInfoback to summary
protected MethodInfo methodInfo

Constructor Detail

CtBehaviorback to summary
protected CtBehavior(CtClass clazz, MethodInfo minfo)

Method Detail

addCatchback to summary
public void addCatch(String src, CtClass exceptionType) throws CannotCompileException

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:String

the source code representing the catch clause. It must be a single statement or block.

exceptionType:CtClass

the type of the exception handled by the catch clause.

addCatchback to summary
public void addCatch(String src, CtClass exceptionType, String exceptionName) throws CannotCompileException

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:String

the source code representing the catch clause. It must be a single statement or block.

exceptionType:CtClass

the type of the exception handled by the catch clause.

exceptionName:String

the name of the variable containing the caught exception, for example, $e.

addLocalVariableback to summary
public void addLocalVariable(String name, CtClass type) throws CannotCompileException

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:String

the name of the variable

type:CtClass

the type of the variable

See Also
insertBefore(String), insertAfter(String)
addParameterback to summary
public void addParameter(CtClass type) throws CannotCompileException

Appends a new parameter, which becomes the last parameter.

addParameter2back to summary
private void addParameter2(int where, CtClass type, String desc) throws BadBytecode
copyback to summary
pack-priv void copy(CtBehavior src, boolean isCons, ClassMap map) throws CannotCompileException
Parameters
isCons:boolean

true if this is a constructor.

extendToStringback to summary
protected void extendToString(StringBuilder buffer)

Implements abstract javassist.CtMember.extendToString.

Doc from javassist.CtMember.extendToString.

Invoked by toString() to add to the buffer and provide the complete value. Subclasses should invoke this method, adding a space before each token. The modifiers for the member are provided first; subclasses should provide additional data such as return type, field or method name, etc.

Annotations
@Override
getAnnotationback to summary
public Object getAnnotation(Class<?> clz) throws ClassNotFoundException

Implements abstract javassist.CtMember.getAnnotation.

Returns the annotation if the class has the specified annotation class. For example, if an annotation @Author is associated with this method/constructor, an Author object is returned. The member values can be obtained by calling methods on the Author object.

Parameters
clz:Class<?>

the annotation class.

Returns:Object

the annotation if found, otherwise null.

Annotations
@Override
Since
3.11
getAnnotationsback to summary
public Object[] getAnnotations() throws ClassNotFoundException

Implements abstract javassist.CtMember.getAnnotations.

Returns the annotations associated with this method or constructor.

Returns:Object[]

an array of annotation-type objects.

Annotations
@Override
Since
3.1
See Also
getAvailableAnnotations()
getAnnotationsback to summary
private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException
getAttributeback to summary
public byte[] getAttribute(String name)

Implements abstract javassist.CtMember.getAttribute.

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 javassist.bytecode.AttributeInfo.

Parameters
name:String

attribute name

Annotations
@Override
getAvailableAnnotationsback to summary
public Object[] getAvailableAnnotations()

Implements abstract javassist.CtMember.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:Object[]

an array of annotation-type objects.

Annotations
@Override
Since
3.3
See Also
getAnnotations()
getAvailableParameterAnnotationsback to summary
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:Object[][]

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()
getExceptionTypesback to summary
public CtClass[] getExceptionTypes() throws NotFoundException

Obtains exceptions that this method/constructor may throw.

Returns:CtClass[]

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

getGenericSignatureback to summary
public String getGenericSignature()

Implements abstract javassist.CtMember.getGenericSignature.

Returns the generic signature of the method. It represents parameter types including type variables.

Annotations
@Override
Since
3.17
See Also
SignatureAttribute#toMethodSignature(String)
getLongNameback to summary
public abstract String getLongName()

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

Since
3.5
getMethodInfoback to summary
public MethodInfo getMethodInfo()

Returns the MethodInfo representing this method/constructor in the class file.

If you modify the bytecode through the returned MethodInfo object, you might have to explicitly rebuild a stack map table. Javassist does not automatically rebuild it for avoiding unnecessary rebuilding.

See Also
javassist.bytecode.MethodInfo#rebuildStackMap(ClassPool)
getMethodInfo2back to summary
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(), CtClass#isFrozen(), CtClass#prune()
getModifiersback to summary
public int getModifiers()

Implements abstract javassist.CtMember.getModifiers.

Obtains the modifiers of the method/constructor.

Returns:int

modifiers encoded with javassist.Modifier.

Annotations
@Override
See Also
Modifier
getParameterAnnotationsback to summary
public Object[][] getParameterAnnotations() throws ClassNotFoundException

Returns the parameter annotations associated with this method or constructor.

Returns:Object[][]

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()
getParameterAnnotationsback to summary
pack-priv Object[][] getParameterAnnotations(boolean ignoreNotFound) throws ClassNotFoundException
getParameterTypesback to summary
public CtClass[] getParameterTypes() throws NotFoundException

Obtains parameter types of this method/constructor.

getReturnType0back to summary
pack-priv CtClass getReturnType0() throws NotFoundException

Obtains the type of the returned value.

getSignatureback to summary
public String getSignature()

Implements abstract javassist.CtMember.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.

Annotations
@Override
See Also
javassist.bytecode.Descriptor, getGenericSignature()
getStartPosOfBodyback to summary
pack-priv int getStartPosOfBody(CodeAttribute ca) throws CannotCompileException
hasAnnotationback to summary
public boolean hasAnnotation(String typeName)

Implements abstract javassist.CtMember.hasAnnotation.

Returns true if the class has the specified annotation type.

Parameters
typeName:String

the name of annotation type.

Returns:boolean

true if the annotation is found, otherwise false.

Annotations
@Override
Since
3.21
insertAfterback to summary
public void insertAfter(String src) throws CannotCompileException

Inserts bytecode at the end of the body. The bytecode is inserted just before every return instruction. It is not executed when an exception is thrown.

Parameters
src:String

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

insertAfterback to summary
public void insertAfter(String src, boolean asFinally) throws CannotCompileException

Inserts bytecode at the end of the body. The bytecode is inserted just before every return instruction.

Parameters
src:String

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

asFinally:boolean

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.

insertAfterback to summary
public void insertAfter(String src, boolean asFinally, boolean redundant) throws CannotCompileException

Inserts bytecode at the end of the body. The bytecode is inserted just before every return instruction.

Parameters
src:String

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

asFinally:boolean

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.

redundant:boolean

if true, redundant bytecode will be generated. the redundancy is necessary when some compilers (Kotlin?) generate the original bytecode. The other insertAfter methods calls this method with false for this parameter. A tip is to pass this.getDeclaringClass().isKotlin() to this parameter.

Since
3.26
See Also
CtClass#isKotlin(), getDeclaringClass()
insertAfterAdviceback to summary
private int insertAfterAdvice(Bytecode code, Javac jv, String src, ConstPool cp, CtClass rtype, int varNo) throws CompileError
insertAfterHandlerback to summary
private int insertAfterHandler(boolean asFinally, Bytecode b, CtClass rtype, int returnVarNo, Javac javac, String src) throws CompileError
insertAtback to summary
public int insertAt(int lineNum, String src) throws CannotCompileException

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

Parameters
lineNum:int

the line number. The bytecode is inserted at the beginning of the code at the line specified by this line number.

src:String

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

Returns:int

the line number at which the bytecode has been inserted.

See Also
CtBehavior#insertAt(int, boolean, String)
insertAtback to summary
public int insertAt(int lineNum, boolean modify, String src) throws CannotCompileException

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:int

the line number. The bytecode is inserted at the beginning of the code at the line specified by this line number.

modify:boolean

if false, this method does not insert the bytecode. It instead only returns the line number at which the bytecode would be inserted.

src:String

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:int

the line number at which the bytecode has been inserted.

insertBeforeback to summary
public void insertBefore(String src) throws CannotCompileException

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:String

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

See Also
CtConstructor#insertBeforeBody(String)
insertBeforeback to summary
private void insertBefore(String src, boolean rebuild) throws CannotCompileException
insertGotoback to summary
private void insertGoto(CodeIterator iterator, int subr, int pos) throws BadBytecode
insertParameterback to summary
public void insertParameter(CtClass type) throws CannotCompileException

Inserts a new parameter, which becomes the first parameter.

instrumentback to summary
public void instrument(CodeConverter converter) throws CannotCompileException

Modifies the method/constructor body.

Parameters
converter:CodeConverter

specifies how to modify.

instrumentback to summary
public void instrument(ExprEditor editor) throws CannotCompileException

Modifies the method/constructor body.

While executing this method, only replace() in Expr is available for bytecode modification. Other methods such as insertBefore() may collapse the bytecode because the ExprEditor loses its current position.

Parameters
editor:ExprEditor

specifies how to modify.

See Also
javassist.expr.Expr#replace(String), insertBefore(String)
isEmptyback to summary
public abstract boolean isEmpty()

Returns true if the body is empty.

setAttributeback to summary
public void setAttribute(String name, byte[] data)

Implements abstract javassist.CtMember.setAttribute.

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 javassist.bytecode.AttributeInfo.

Parameters
name:String

attribute name

data:byte[]

attribute value

Annotations
@Override
setBodyback to summary
public void setBody(String src) throws CannotCompileException

Sets a method/constructor body.

Parameters
src:String

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.

setBodyback to summary
public void setBody(String src, String delegateObj, String delegateMethod) throws CannotCompileException

Sets a method/constructor body.

Parameters
src:String

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:String

the source text specifying the object that is called on by $proceed().

delegateMethod:String

the name of the method that is called by $proceed().

setBody0back to summary
pack-priv static void setBody0(CtClass srcClass, MethodInfo srcInfo, CtClass destClass, MethodInfo destInfo, ClassMap map) throws CannotCompileException
setExceptionTypesback to summary
public void setExceptionTypes(CtClass[] types) throws NotFoundException

Sets exceptions that this method/constructor may throw.

setGenericSignatureback to summary
public void setGenericSignature(String sig)

Implements abstract javassist.CtMember.setGenericSignature.

Set the generic signature of the method. It represents parameter types including type variables. See javassist.CtClass#setGenericSignature(String) for a code sample.

Parameters
sig:String

a new generic signature.

Annotations
@Override
Since
3.17
See Also
javassist.bytecode.SignatureAttribute.MethodSignature#encode()
setModifiersback to summary
public void setModifiers(int mod)

Implements abstract javassist.CtMember.setModifiers.

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.

Annotations
@Override
See Also
Modifier
useCflowback to summary
public void useCflow(String name) throws CannotCompileException

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:String

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

See Also
javassist.runtime.Cflow