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.
getMethodInfo()
| Modifier and Type | Field and Description |
|---|---|
| protected MethodInfo |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| 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,
exceptionName)$e.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 | |
| private void | |
| pack-priv void | |
| protected void | extendToString(StringBuilder buffer)
Implements abstract javassist. Invoked by |
| public Object | Returns: the annotation if found, otherwisenull.the annotation class. clz)Implements abstract javassist. Returns the annotation if the class has the specified annotation class. |
| public Object[] | Returns: an array of annotation-type objects.Implements abstract javassist. Returns the annotations associated with this method or constructor. |
| private Object[] | |
| public byte[] | getAttribute(String
attribute name name)Implements abstract javassist. Obtains an attribute with the given name. |
| public Object[] | Returns: an array of annotation-type objects.Implements abstract javassist. 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.Returns the parameter annotations associated with this method or constructor. |
| public CtClass[] | Returns: a zero-length array if there is no throws clause.Obtains exceptions that this method/constructor may throw. |
| public String | getGenericSignature()
Implements abstract javassist. Returns the generic signature of the method. |
| public abstract String | getLongName()
Returns the method or constructor name followed by parameter types
such as |
| public MethodInfo | |
| public MethodInfo | getMethodInfo2()
Returns the |
| public int | Returns: modifiers encoded withjavassist.Modifier.Implements abstract javassist. 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.Returns the parameter annotations associated with this method or constructor. |
| pack-priv Object[][] | |
| public CtClass[] | |
| pack-priv CtClass | |
| public String | getSignature()
Implements abstract javassist. 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.the name of annotation type. typeName)Implements abstract javassist. 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 redundant)insertAfter methods calls this method
with false for this parameter.
A tip is to pass this.getDeclaringClass().isKotlin()
to this parameter.Inserts bytecode at the end of the body. |
| private int | |
| 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.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.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 | |
| private void | |
| public void | |
| public void | |
| public void | |
| public abstract boolean | |
| public void | setAttribute(String
attribute name name, byte[] attribute value data)Implements abstract javassist. Adds an attribute. |
| public void | |
| public void | setBody(String
the source code representing the body.
It must be a single statement or block.
If it is src, String null, the substituted
body does nothing except returning zero or null.the source text specifying the object
that is called on by delegateObj, String $proceed().the name of the method
that is called by delegateMethod)$proceed().Sets a method/constructor body. |
| pack-priv static void | setBody0(CtClass srcClass, MethodInfo srcInfo, CtClass destClass, MethodInfo destInfo, ClassMap map)
|
| public void | |
| public void | setGenericSignature(String
a new generic signature. sig)Implements abstract javassist. Set the generic signature of the method. |
| public void | setModifiers(int mod)
Implements abstract javassist. Sets the encoded modifiers of the method/constructor. |
| public void |
| methodInfo | back to summary |
|---|---|
| protected MethodInfo methodInfo | |
| CtBehavior | back to summary |
|---|---|
| protected CtBehavior(CtClass clazz, MethodInfo minfo) | |
| addCatch | back 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. | |
| addCatch | back 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. | |
| addLocalVariable | back 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 If the second parameter
| |
| addParameter | back to summary |
|---|---|
| public void addParameter(CtClass type) throws CannotCompileException Appends a new parameter, which becomes the last parameter. | |
| addParameter2 | back to summary |
|---|---|
| private void addParameter2(int where, CtClass type, String desc) throws BadBytecode | |
| copy | back to summary |
|---|---|
pack-priv void copy(CtBehavior src, boolean isCons, ClassMap map) throws CannotCompileException
| |
| extendToString | back to summary |
|---|---|
| protected void extendToString(StringBuilder buffer) Implements abstract javassist. Doc from javassist. Invoked by
| |
| getAnnotation | back to summary |
|---|---|
| public Object getAnnotation(Class<?> clz) throws ClassNotFoundException Implements abstract javassist. Returns the annotation if the class has the specified annotation class.
For example, if an annotation | |
| getAnnotations | back to summary |
|---|---|
| public Object[] getAnnotations() throws ClassNotFoundException Implements abstract javassist. Returns the annotations associated with this method or constructor.
| |
| getAnnotations | back to summary |
|---|---|
| private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException | |
| getAttribute | back to summary |
|---|---|
| public byte[] getAttribute(String name) Implements abstract javassist. 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 | |
| getAvailableAnnotations | back to summary |
|---|---|
| public Object[] getAvailableAnnotations() Implements abstract javassist. 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.
| |
| getAvailableParameterAnnotations | back 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.
| |
| getExceptionTypes | back to summary |
|---|---|
| public CtClass[] getExceptionTypes() throws NotFoundException Obtains exceptions that this method/constructor may throw.
| |
| getGenericSignature | back to summary |
|---|---|
| public String getGenericSignature() Implements abstract javassist. Returns the generic signature of the method. It represents parameter types including type variables.
| |
| getLongName | back to summary |
|---|---|
| public abstract String getLongName() Returns the method or constructor name followed by parameter types
such as
| |
| getMethodInfo | back to summary |
|---|---|
| public MethodInfo getMethodInfo() Returns the If you modify the bytecode through the returned
| |
| getMethodInfo2 | back to summary |
|---|---|
| public MethodInfo getMethodInfo2() Returns the The This method is available even if the
| |
| getModifiers | back to summary |
|---|---|
| public int getModifiers() Implements abstract javassist. Obtains the modifiers of the method/constructor. | |
| getParameterAnnotations | back to summary |
|---|---|
| public Object[][] getParameterAnnotations() throws ClassNotFoundException Returns the parameter annotations associated with this method or constructor.
| |
| getParameterAnnotations | back to summary |
|---|---|
| pack-priv Object[][] getParameterAnnotations(boolean ignoreNotFound) throws ClassNotFoundException | |
| getParameterTypes | back to summary |
|---|---|
| public CtClass[] getParameterTypes() throws NotFoundException Obtains parameter types of this method/constructor. | |
| getReturnType0 | back to summary |
|---|---|
| pack-priv CtClass getReturnType0() throws NotFoundException Obtains the type of the returned value. | |
| getSignature | back to summary |
|---|---|
| public String getSignature() Implements abstract javassist. 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, Note that the returned string is not the type signature
contained in the
| |
| getStartPosOfBody | back to summary |
|---|---|
| pack-priv int getStartPosOfBody(CodeAttribute ca) throws CannotCompileException | |
| hasAnnotation | back to summary |
|---|---|
| public boolean hasAnnotation(String typeName) Implements abstract javassist. Returns true if the class has the specified annotation type. | |
| insertAfter | back 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.
| |
| insertAfter | back 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.
| |
| insertAfter | back 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.
| |
| insertAfterAdvice | back to summary |
|---|---|
| private int insertAfterAdvice(Bytecode code, Javac jv, String src, ConstPool cp, CtClass rtype, int varNo) throws CompileError | |
| insertAfterHandler | back to summary |
|---|---|
| private int insertAfterHandler(boolean asFinally, Bytecode b, CtClass rtype, int returnVarNo, Javac javac, String src) throws CompileError | |
| insertAt | back 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 | back 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
| |
| insertBefore | back 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
| |
| insertBefore | back to summary |
|---|---|
| private void insertBefore(String src, boolean rebuild) throws CannotCompileException | |
| insertGoto | back to summary |
|---|---|
| private void insertGoto(CodeIterator iterator, int subr, int pos) throws BadBytecode | |
| insertParameter | back to summary |
|---|---|
| public void insertParameter(CtClass type) throws CannotCompileException Inserts a new parameter, which becomes the first parameter. | |
| instrument | back to summary |
|---|---|
| public void instrument(CodeConverter converter) throws CannotCompileException Modifies the method/constructor body.
| |
| instrument | back to summary |
|---|---|
| public void instrument(ExprEditor editor) throws CannotCompileException Modifies the method/constructor body. While executing this method, only
| |
| isEmpty | back to summary |
|---|---|
| public abstract boolean isEmpty() Returns true if the body is empty. | |
| setAttribute | back to summary |
|---|---|
| public void setAttribute(String name, byte[] data) Implements abstract javassist. 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 | |
| setBody | back to summary |
|---|---|
| public void setBody(String src) throws CannotCompileException Sets a method/constructor body.
| |
| setBody | back to summary |
|---|---|
| public void setBody(String src, String delegateObj, String delegateMethod) throws CannotCompileException Sets a method/constructor body.
| |
| setBody0 | back to summary |
|---|---|
| pack-priv static void setBody0(CtClass srcClass, MethodInfo srcInfo, CtClass destClass, MethodInfo destInfo, ClassMap map) throws CannotCompileException | |
| setExceptionTypes | back to summary |
|---|---|
| public void setExceptionTypes(CtClass[] types) throws NotFoundException Sets exceptions that this method/constructor may throw. | |
| setGenericSignature | back to summary |
|---|---|
| public void setGenericSignature(String sig) Implements abstract javassist. Set the generic signature of the method.
It represents parameter types including type variables.
See
| |
| setModifiers | back to summary |
|---|---|
| public void setModifiers(int mod) Implements abstract javassist. 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. | |
| useCflow | back to summary |
|---|---|
| public void useCflow(String name) throws CannotCompileException Declares to use Every
| |