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.
 To directly read or modify bytecode, obtain MethodInfo
 objects.
getMethodInfo()| Modifier and Type | Field and Description | 
|---|---|
| protected MethodInfo | methodInfo | 
declaringClass| Modifier | Constructor and Description | 
|---|---|
| protected  | CtBehavior(CtClass clazz,
          MethodInfo minfo) | 
| Modifier and Type | Method and Description | 
|---|---|
| void | addCatch(String src,
        CtClass exceptionType)Adds a catch clause that handles an exception thrown in the
 body. | 
| void | addCatch(String src,
        CtClass exceptionType,
        String exceptionName)Adds a catch clause that handles an exception thrown in the
 body. | 
| void | addLocalVariable(String name,
                CtClass type)Declares a new local variable. | 
| void | addParameter(CtClass type)Appends a new parameter, which becomes the last parameter. | 
| protected void | extendToString(StringBuffer buffer)Invoked by  CtMember.toString()to add to the buffer and provide the
 complete value. | 
| Object | getAnnotation(Class clz)Returns the annotation if the class has the specified annotation class. | 
| 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. | 
| String | getGenericSignature()Returns the generic signature of the method. | 
| abstract String | getLongName()Returns the method or constructor name followed by parameter types
 such as  javassist.CtBehavior.stBody(String). | 
| MethodInfo | getMethodInfo()Returns the  MethodInforepresenting this method/constructor in the
 class file. | 
| MethodInfo | getMethodInfo2()Returns the  MethodInforepresenting the method/constructor in the
 class file (read only). | 
| int | getModifiers()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. | 
| String | getSignature()Returns the method signature (the parameter types
 and the return type). | 
| boolean | hasAnnotation(String typeName)Returns true if the class has the specified annotation type. | 
| void | insertAfter(String src)Inserts bytecode at the end of the body. | 
| void | insertAfter(String src,
           boolean asFinally)Inserts bytecode at the end of the body. | 
| int | insertAt(int lineNum,
        boolean modify,
        String src)Inserts bytecode at the specified line in the body. | 
| int | insertAt(int lineNum,
        String src)Inserts bytecode at the specified line in the body. | 
| void | insertBefore(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(String name,
            byte[] data)Adds an attribute. | 
| void | setBody(String src)Sets a method/constructor body. | 
| void | setBody(String src,
       String delegateObj,
       String delegateMethod)Sets a method/constructor body. | 
| void | setExceptionTypes(CtClass[] types)Sets exceptions that this method/constructor may throw. | 
| void | setGenericSignature(String sig)Set the generic signature of the method. | 
| void | setModifiers(int mod)Sets the encoded modifiers of the method/constructor. | 
| void | useCflow(String name)Declares to use  $cflowfor this method/constructor. | 
getDeclaringClass, getName, hasAnnotation, toString, visibleFromprotected MethodInfo methodInfo
protected CtBehavior(CtClass clazz, MethodInfo minfo)
protected void extendToString(StringBuffer buffer)
CtMemberCtMember.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.extendToString in class CtMemberpublic abstract String getLongName()
javassist.CtBehavior.stBody(String).public MethodInfo getMethodInfo()
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.
MethodInfo.rebuildStackMap(ClassPool)public MethodInfo getMethodInfo2()
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.
getMethodInfo(), 
CtClass.isFrozen(), 
CtClass.prune()public int getModifiers()
getModifiers in class CtMemberjavassist.Modifier.Modifierpublic void setModifiers(int mod)
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.
setModifiers in class CtMemberModifierpublic boolean hasAnnotation(String typeName)
hasAnnotation in class CtMembertypeName - the name of annotation type.true if the annotation is found,
         otherwise false.public Object getAnnotation(Class clz) throws ClassNotFoundException
@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.getAnnotation in class CtMemberclz - the annotation class.null.ClassNotFoundExceptionpublic Object[] getAnnotations() throws ClassNotFoundException
getAnnotations in class CtMemberClassNotFoundExceptiongetAvailableAnnotations()public Object[] getAvailableAnnotations()
getAvailableAnnotations in class CtMembergetAnnotations()public Object[][] getParameterAnnotations() throws ClassNotFoundException
ClassNotFoundExceptiongetAvailableParameterAnnotations(), 
getAnnotations()public Object[][] getAvailableParameterAnnotations()
getParameterAnnotations(), 
getAvailableAnnotations()public CtClass[] getParameterTypes() throws NotFoundException
NotFoundExceptionpublic String getSignature()
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.
getSignature in class CtMemberDescriptor, 
getGenericSignature()public String getGenericSignature()
getGenericSignature in class CtMemberSignatureAttribute.toMethodSignature(String)public void setGenericSignature(String sig)
CtClass.setGenericSignature(String)
 for a code sample.setGenericSignature in class CtMembersig - a new generic signature.SignatureAttribute.MethodSignature.encode()public CtClass[] getExceptionTypes() throws NotFoundException
NotFoundExceptionpublic void setExceptionTypes(CtClass[] types) throws NotFoundException
NotFoundExceptionpublic abstract boolean isEmpty()
public void setBody(String src) throws CannotCompileException
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.CannotCompileExceptionpublic void setBody(String src, String delegateObj, String delegateMethod) throws CannotCompileException
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().CannotCompileExceptionpublic byte[] getAttribute(String name)
Note that an attribute is a data block specified by
 the class file format.  It is not an annotation.
 See AttributeInfo.
getAttribute in class CtMembername - attribute namepublic void setAttribute(String name, byte[] data)
Note that an attribute is a data block specified by
 the class file format.  It is not an annotation.
 See AttributeInfo.
setAttribute in class CtMembername - attribute namedata - attribute valuepublic void useCflow(String name) throws CannotCompileException
$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).
name - $cflow name.  It can include
                  alphabets, numbers, _,
                  $, and . (dot).CannotCompileExceptionCflowpublic void addLocalVariable(String name, CtClass type) throws CannotCompileException
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().
name - the name of the variabletype - the type of the variableCannotCompileExceptioninsertBefore(String), 
insertAfter(String)public void insertParameter(CtClass type) throws CannotCompileException
CannotCompileExceptionpublic void addParameter(CtClass type) throws CannotCompileException
CannotCompileExceptionpublic void instrument(CodeConverter converter) throws CannotCompileException
converter - specifies how to modify.CannotCompileExceptionpublic void instrument(ExprEditor editor) throws CannotCompileException
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.
editor - specifies how to modify.CannotCompileExceptionExpr.replace(String), 
insertBefore(String)public void insertBefore(String src) throws CannotCompileException
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.
src - the source code representing the inserted bytecode.
                  It must be a single statement or block.CannotCompileExceptionCtConstructor.insertBeforeBody(String)public void insertAfter(String src) throws CannotCompileException
src - the source code representing the inserted bytecode.
                  It must be a single statement or block.CannotCompileExceptionpublic void insertAfter(String src, boolean asFinally) throws CannotCompileException
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.CannotCompileExceptionpublic void addCatch(String src, CtClass exceptionType) throws CannotCompileException
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.CannotCompileExceptionpublic void addCatch(String src, CtClass exceptionType, String exceptionName) throws CannotCompileException
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.CannotCompileExceptionpublic int insertAt(int lineNum,
                    String src)
             throws CannotCompileException
insertAt(lineNum, true, src)
 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.CannotCompileExceptioninsertAt(int,boolean,String)public int insertAt(int lineNum,
                    boolean modify,
                    String src)
             throws CannotCompileException
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.
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.CannotCompileExceptionCopyright © 2016 Shigeru Chiba, www.javassist.org. All Rights Reserved.