Class CtBehavior
- java.lang.Object
-
- javassist.CtMember
-
- javassist.CtBehavior
-
- Direct Known Subclasses:
CtConstructor
,CtMethod
public abstract class CtBehavior extends CtMember
CtBehavior
represents a method, a constructor, or a static constructor (class initializer). It is the abstract super class ofCtMethod
andCtConstructor
.To directly read or modify bytecode, obtain
MethodInfo
objects.- See Also:
getMethodInfo()
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addCatch(java.lang.String src, CtClass exceptionType)
Adds a catch clause that handles an exception thrown in the body.void
addCatch(java.lang.String src, CtClass exceptionType, java.lang.String exceptionName)
Adds a catch clause that handles an exception thrown in the body.void
addLocalVariable(java.lang.String name, CtClass type)
Declares a new local variable.void
addParameter(CtClass type)
Appends a new parameter, which becomes the last parameter.java.lang.Object
getAnnotation(java.lang.Class<?> clz)
Returns the annotation if the class has the specified annotation class.java.lang.Object[]
getAnnotations()
Returns the annotations associated with this method or constructor.byte[]
getAttribute(java.lang.String name)
Obtains an attribute with the given name.java.lang.Object[]
getAvailableAnnotations()
Returns the annotations associated with this method or constructor.java.lang.Object[][]
getAvailableParameterAnnotations()
Returns the parameter annotations associated with this method or constructor.CtClass[]
getExceptionTypes()
Obtains exceptions that this method/constructor may throw.java.lang.String
getGenericSignature()
Returns the generic signature of the method.abstract java.lang.String
getLongName()
Returns the method or constructor name followed by parameter types such asjavassist.CtBehavior.stBody(String)
.MethodInfo
getMethodInfo()
Returns theMethodInfo
representing this method/constructor in the class file.MethodInfo
getMethodInfo2()
Returns theMethodInfo
representing the method/constructor in the class file (read only).int
getModifiers()
Obtains the modifiers of the method/constructor.java.lang.Object[][]
getParameterAnnotations()
Returns the parameter annotations associated with this method or constructor.CtClass[]
getParameterTypes()
Obtains parameter types of this method/constructor.java.lang.String
getSignature()
Returns the method signature (the parameter types and the return type).boolean
hasAnnotation(java.lang.String typeName)
Returns true if the class has the specified annotation type.void
insertAfter(java.lang.String src)
Inserts bytecode at the end of the body.void
insertAfter(java.lang.String src, boolean asFinally)
Inserts bytecode at the end of the body.void
insertAfter(java.lang.String src, boolean asFinally, boolean redundant)
Inserts bytecode at the end of the body.int
insertAt(int lineNum, boolean modify, java.lang.String src)
Inserts bytecode at the specified line in the body.int
insertAt(int lineNum, java.lang.String src)
Inserts bytecode at the specified line in the body.void
insertBefore(java.lang.String src)
Inserts bytecode at the beginning of the body.void
insertParameter(CtClass type)
Inserts a new parameter, which becomes the first parameter.void
instrument(CodeConverter converter)
Modifies the method/constructor body.void
instrument(ExprEditor editor)
Modifies the method/constructor body.abstract boolean
isEmpty()
Returns true if the body is empty.void
setAttribute(java.lang.String name, byte[] data)
Adds an attribute.void
setBody(java.lang.String src)
Sets a method/constructor body.void
setBody(java.lang.String src, java.lang.String delegateObj, java.lang.String delegateMethod)
Sets a method/constructor body.void
setExceptionTypes(CtClass[] types)
Sets exceptions that this method/constructor may throw.void
setGenericSignature(java.lang.String sig)
Set the generic signature of the method.void
setModifiers(int mod)
Sets the encoded modifiers of the method/constructor.void
useCflow(java.lang.String name)
Declares to use$cflow
for this method/constructor.-
Methods inherited from class javassist.CtMember
getDeclaringClass, getName, hasAnnotation, toString, visibleFrom
-
-
-
-
Method Detail
-
getLongName
public abstract java.lang.String getLongName()
Returns the method or constructor name followed by parameter types such asjavassist.CtBehavior.stBody(String)
.- Since:
- 3.5
-
getMethodInfo
public MethodInfo getMethodInfo()
Returns theMethodInfo
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:
MethodInfo.rebuildStackMap(ClassPool)
-
getMethodInfo2
public MethodInfo getMethodInfo2()
Returns theMethodInfo
representing the method/constructor in the class file (read only). Normal applications do not need calling this method. UsegetMethodInfo()
.The
MethodInfo
object obtained by this method is read only. Changes to this object might not be reflected on a class file generated bytoBytecode()
,toClass()
, etc inCtClass
.This method is available even if the
CtClass
containing this method is frozen. However, if the class is frozen, theMethodInfo
might be also pruned.- See Also:
getMethodInfo()
,CtClass.isFrozen()
,CtClass.prune()
-
getModifiers
public int getModifiers()
Obtains the modifiers of the method/constructor.- Specified by:
getModifiers
in classCtMember
- Returns:
- modifiers encoded with
javassist.Modifier
. - See Also:
Modifier
-
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.
- Specified by:
setModifiers
in classCtMember
- See Also:
Modifier
-
hasAnnotation
public boolean hasAnnotation(java.lang.String typeName)
Returns true if the class has the specified annotation type.- Specified by:
hasAnnotation
in classCtMember
- Parameters:
typeName
- the name of annotation type.- Returns:
true
if the annotation is found, otherwisefalse
.- Since:
- 3.21
-
getAnnotation
public java.lang.Object getAnnotation(java.lang.Class<?> clz) throws java.lang.ClassNotFoundException
Returns the annotation if the class has the specified annotation class. For example, if an annotation@Author
is associated with this method/constructor, anAuthor
object is returned. The member values can be obtained by calling methods on theAuthor
object.- Specified by:
getAnnotation
in classCtMember
- Parameters:
clz
- the annotation class.- Returns:
- the annotation if found, otherwise
null
. - Throws:
java.lang.ClassNotFoundException
- Since:
- 3.11
-
getAnnotations
public java.lang.Object[] getAnnotations() throws java.lang.ClassNotFoundException
Returns the annotations associated with this method or constructor.- Specified by:
getAnnotations
in classCtMember
- Returns:
- an array of annotation-type objects.
- Throws:
java.lang.ClassNotFoundException
- Since:
- 3.1
- See Also:
getAvailableAnnotations()
-
getAvailableAnnotations
public java.lang.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.- Specified by:
getAvailableAnnotations
in classCtMember
- Returns:
- an array of annotation-type objects.
- Since:
- 3.3
- See Also:
getAnnotations()
-
getParameterAnnotations
public java.lang.Object[][] getParameterAnnotations() throws java.lang.ClassNotFoundException
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.
- Throws:
java.lang.ClassNotFoundException
- Since:
- 3.1
- See Also:
getAvailableParameterAnnotations()
,getAnnotations()
-
getAvailableParameterAnnotations
public java.lang.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()
-
getParameterTypes
public CtClass[] getParameterTypes() throws NotFoundException
Obtains parameter types of this method/constructor.- Throws:
NotFoundException
-
getSignature
public java.lang.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 isvoid
).Note that the returned string is not the type signature contained in the
SignatureAttirbute
. It is a descriptor.- Specified by:
getSignature
in classCtMember
- See Also:
Descriptor
,getGenericSignature()
-
getGenericSignature
public java.lang.String getGenericSignature()
Returns the generic signature of the method. It represents parameter types including type variables.- Specified by:
getGenericSignature
in classCtMember
- Since:
- 3.17
- See Also:
SignatureAttribute.toMethodSignature(String)
-
setGenericSignature
public void setGenericSignature(java.lang.String sig)
Set the generic signature of the method. It represents parameter types including type variables. SeeCtClass.setGenericSignature(String)
for a code sample.- Specified by:
setGenericSignature
in classCtMember
- Parameters:
sig
- a new generic signature.- Since:
- 3.17
- See Also:
SignatureAttribute.MethodSignature.encode()
-
getExceptionTypes
public CtClass[] getExceptionTypes() throws NotFoundException
Obtains exceptions that this method/constructor may throw.- Returns:
- a zero-length array if there is no throws clause.
- Throws:
NotFoundException
-
setExceptionTypes
public void setExceptionTypes(CtClass[] types) throws NotFoundException
Sets exceptions that this method/constructor may throw.- Throws:
NotFoundException
-
isEmpty
public abstract boolean isEmpty()
Returns true if the body is empty.
-
setBody
public void setBody(java.lang.String src) throws CannotCompileException
Sets a method/constructor body.- Parameters:
src
- the source code representing the body. It must be a single statement or block. If it isnull
, the substituted body does nothing except returning zero or null.- Throws:
CannotCompileException
-
setBody
public void setBody(java.lang.String src, java.lang.String delegateObj, java.lang.String delegateMethod) throws CannotCompileException
Sets a method/constructor body.- Parameters:
src
- the source code representing the body. It must be a single statement or block. If it isnull
, 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()
.- Throws:
CannotCompileException
-
getAttribute
public byte[] getAttribute(java.lang.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
.- Specified by:
getAttribute
in classCtMember
- Parameters:
name
- attribute name
-
setAttribute
public void setAttribute(java.lang.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
.- Specified by:
setAttribute
in classCtMember
- Parameters:
name
- attribute namedata
- attribute value
-
useCflow
public void useCflow(java.lang.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 classjavassist.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).- Throws:
CannotCompileException
- See Also:
Cflow
-
addLocalVariable
public void addLocalVariable(java.lang.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 byinsertBefore()
,insertAfter()
, etc.If the second parameter
asFinally
toinsertAfter()
is true, the declared local variable is not visible from the code inserted byinsertAfter()
.- Parameters:
name
- the name of the variabletype
- the type of the variable- Throws:
CannotCompileException
- See Also:
insertBefore(String)
,insertAfter(String)
-
insertParameter
public void insertParameter(CtClass type) throws CannotCompileException
Inserts a new parameter, which becomes the first parameter.- Throws:
CannotCompileException
-
addParameter
public void addParameter(CtClass type) throws CannotCompileException
Appends a new parameter, which becomes the last parameter.- Throws:
CannotCompileException
-
instrument
public void instrument(CodeConverter converter) throws CannotCompileException
Modifies the method/constructor body.- Parameters:
converter
- specifies how to modify.- Throws:
CannotCompileException
-
instrument
public void instrument(ExprEditor editor) throws CannotCompileException
Modifies the method/constructor body.While executing this method, only
replace()
inExpr
is available for bytecode modification. Other methods such asinsertBefore()
may collapse the bytecode because theExprEditor
loses its current position.- Parameters:
editor
- specifies how to modify.- Throws:
CannotCompileException
- See Also:
Expr.replace(String)
,insertBefore(String)
-
insertBefore
public void insertBefore(java.lang.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()
inCtConstructor
.- Parameters:
src
- the source code representing the inserted bytecode. It must be a single statement or block.- Throws:
CannotCompileException
- See Also:
CtConstructor.insertBeforeBody(String)
-
insertAfter
public void insertAfter(java.lang.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
- the source code representing the inserted bytecode. It must be a single statement or block.- Throws:
CannotCompileException
-
insertAfter
public void insertAfter(java.lang.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
- 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.- Throws:
CannotCompileException
-
insertAfter
public void insertAfter(java.lang.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
- 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.redundant
- if true, redundant bytecode will be generated. the redundancy is necessary when some compilers (Kotlin?) generate the original bytecode. The otherinsertAfter
methods calls this method withfalse
for this parameter. A tip is to passthis.getDeclaringClass().isKotlin()
to this parameter.- Throws:
CannotCompileException
- Since:
- 3.26
- See Also:
CtClass.isKotlin()
,CtMember.getDeclaringClass()
-
addCatch
public void addCatch(java.lang.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
- 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.- Throws:
CannotCompileException
-
addCatch
public void addCatch(java.lang.String src, CtClass exceptionType, java.lang.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
- 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
.- Throws:
CannotCompileException
-
insertAt
public int insertAt(int lineNum, java.lang.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
- 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.
- Throws:
CannotCompileException
- See Also:
insertAt(int,boolean,String)
-
insertAt
public int insertAt(int lineNum, boolean modify, java.lang.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 tofalse
.- 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.
- Throws:
CannotCompileException
-
-