Overview | Package | Class | Use | Tree | Deprecated | Index
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javassist
Class CtMethod

    java.lang.Object
      |
      +--javassist.CtMember
            |
            +--javassist.CtBehavior
                  |
                  +--javassist.CtMethod

public final class CtMethod
extends CtBehavior

An instance of `CtMethod` represents a method.

See Also:
  `CtClass.getDeclaredMethods()`, `CtNewMethod`

Nested Class Summary
`static class` | `CtMethod.ConstParameter` Instances of this class represent a constant parameter.

Constructor Summary
`CtMethod(CtClass returnType, java.lang.String mname, CtClass[] parameters, CtClass declaring)` Creates a public abstract method.
`CtMethod(CtMethod src, CtClass declaring, ClassMap map)` Creates a copy of a `CtMethod` object.

Method Summary
`void` | `addCatch(java.lang.String src, CtClass exceptionType)` Adds a catch clause that handles an exception thrown in the method body.
`boolean` | `equals(java.lang.Object obj)` Indicates whether `obj` has the same name and the same signature as this method.
`byte[]` | `getAttribute(java.lang.String name)` Obtains an attribute with the given name.
`CtClass` | `getDeclaringClass()` Returns the class that declares this method.
`CtClass[]` | `getExceptionTypes()` Obtains exceptions that this method may throw.
`MethodInfo` | `getMethodInfo()` Returns the MethodInfo representing the method in the class file.
`int` | `getModifiers()` Obtains the modifiers of the method.
`java.lang.String` | `getName()` Obtains the name of this method.
`CtClass[]` | `getParameterTypes()` Obtains parameter types of this method.
`CtClass` | `getReturnType()` Obtains the type of the returned value.
`java.lang.String` | `getSignature()` Returns the character string representing the parameter types and the return type.
`int` | `hashCode()` Returns a hash code value for the method.
`void` | `insertAfter(java.lang.String src)` Inserts bytecode at the end of the method body.
`void` | `insertAfter(java.lang.String src, boolean asFinally)` Inserts bytecode at the end of the method body.
`void` | `insertBefore(java.lang.String src)` Inserts bytecode at the beginning of the method body.
`void` | `instrument(CodeConverter converter)` Modifies the method body.
`void` | `instrument(ExprEditor editor)` Modifies the method body.
`boolean` | `isEmpty()` Returns true if the method body is empty, that is, `{}`.
`void` | `setAttribute(java.lang.String name, byte[] data)` Adds an attribute.
`void` | `setBody(CtMethod src, ClassMap map)` Copies a method body from another method.
`void` | `setBody(java.lang.String src)` Sets a method body.
`void` | `setExceptionTypes(CtClass[] types)` Sets exceptions that this method may throw.
`void` | `setModifiers(int mod)` Sets the encoded modifiers of the method.
`void` | `setName(java.lang.String newname)` Changes the name of this method.
`void` | `setWrappedBody(CtMethod mbody, CtMethod.ConstParameter constParam)` Replace a method body with a new method body wrapping the given method.
`void` | `useCflow(java.lang.String name)` Declares to use `$cflow` for this method.

Methods inherited from class javassist.CtBehavior
`addCatch, getMethodInfo2`

Methods inherited from class java.lang.Object
`getClass, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### CtMethod

    public CtMethod(CtClass returnType,
                    java.lang.String mname,
                    CtClass[] parameters,
                    CtClass declaring)

  Creates a public abstract method. The created method must be added to a class with `CtClass.addMethod()`.

Parameters:
  `declaring` - the class to which the created method is added.
  `returnType` - the type of the returned value
  `mname` - the method name
  `parameters` - a list of the parameter types
See Also:
  `CtClass.addMethod(CtMethod)`

### CtMethod

    public CtMethod(CtMethod src,
                    CtClass declaring,
                    ClassMap map)
             throws CannotCompileException

  Creates a copy of a `CtMethod` object. The created method must be added to a class with `CtClass.addMethod()`.

  All occurrences of class names in the created method are replaced with names specified by `map` if `map` is not `null`.

  For example, suppose that a method `at()` is as follows:

      public X at(int i) {
           return (X)super.elementAt(i);
       }

  (`X` is a class name.) If `map` substitutes `String` for `X`, then the created method is:

      public String at(int i) {
           return (String)super.elementAt(i);
       }

  By default, all the occurrences of the names of the class declaring `at()` and the superclass are replaced with the name of the class and the superclass that the created method is added to. This is done whichever `map` is null or not. To prevent this replacement, call `ClassMap.fix()`.

  Note: if the `.class` notation (for example, `String.class`) is included in an expression, the Javac compiler may produce a helper method. Since this constructor never copies this helper method, the programmers have the responsiblity of copying it. Otherwise, use `Class.forName()` in the expression.

Parameters:
  `src` - the source method.
  `declaring` - the class to which the created method is added.
  `map` - the hashtable associating original class names with substituted names. It can be `null`.
See Also:
  `CtClass.addMethod(CtMethod)`, `ClassMap.fix(String)`

Method Detail

### hashCode

    public int hashCode()

  Returns a hash code value for the method. If two methods have the same name and signature, then the hash codes for the two methods are equal.

  Overrides:
    `hashCode` in class `java.lang.Object`

### equals

    public boolean equals(java.lang.Object obj)

  Indicates whether `obj` has the same name and the same signature as this method.

  Overrides:
    `equals` in class `java.lang.Object`

### getMethodInfo

    public MethodInfo getMethodInfo()

  Returns the MethodInfo representing the method in the class file.

  Overrides:
    `getMethodInfo` in class `CtBehavior`

### getModifiers

    public int getModifiers()

  Obtains the modifiers of the method.

  Overrides:
    `getModifiers` in class `CtBehavior`
  Returns:
    modifiers encoded with `javassist.Modifier`.
  See Also:
    `Modifier`

### setModifiers

    public void setModifiers(int mod)

  Sets the encoded modifiers of the method.

  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.

  Overrides:
    `setModifiers` in class `CtBehavior`
  See Also:
    `Modifier`

### getName

    public java.lang.String getName()

  Obtains the name of this method.

  Specified by:
    `getName` in class `CtBehavior`
  See Also:
    `CtConstructor.getName()`

### setName

    public void setName(java.lang.String newname)

  Changes the name of this method.

### getDeclaringClass

    public CtClass getDeclaringClass()

  Returns the class that declares this method.

  Overrides:
    `getDeclaringClass` in class `CtMember`

### getParameterTypes

    public CtClass[] getParameterTypes()
                                throws NotFoundException

  Obtains parameter types of this method.

  Overrides:
    `getParameterTypes` in class `CtBehavior`
    `NotFoundException`

### getReturnType

    public CtClass getReturnType()
                          throws NotFoundException

  Obtains the type of the returned value.

    `NotFoundException`

### getSignature

    public java.lang.String getSignature()

  Returns the character string representing the parameter types and the return type. If two methods have the same parameter types and the return type, `getSignature()` returns the same string.

  Overrides:
    `getSignature` in class `CtBehavior`

### getExceptionTypes

    public CtClass[] getExceptionTypes()
                                throws NotFoundException

  Obtains exceptions that this method may throw.

  Overrides:
    `getExceptionTypes` in class `CtBehavior`
    `NotFoundException`

### setExceptionTypes

    public void setExceptionTypes(CtClass[] types)
                           throws NotFoundException

  Sets exceptions that this method may throw.

  Overrides:
    `setExceptionTypes` in class `CtBehavior`
  Parameters:
    `types` - exception types (or null)
    `NotFoundException`

### isEmpty

    public boolean isEmpty()

  Returns true if the method body is empty, that is, `{}`. It also returns true if the method is an abstract method.

  Specified by:
    `isEmpty` in class `CtBehavior`

### setBody

    public void setBody(java.lang.String src)
                 throws CannotCompileException

  Sets a method body.

  Overrides:
    `setBody` in class `CtBehavior`
  Parameters:
    `src` - the source code representing the method body. It must be a single statement or block. If it is `null`, the substituted method body does nothing except returning zero or null.
    `CannotCompileException`

### setBody

    public void setBody(CtMethod src,
                        ClassMap map)
                 throws CannotCompileException

  Copies a method body from another method. If this method is abstract, the abstract modifier is removed after the method body is copied.

  All occurrences of the class names in the copied method body are replaced with the names specified by `map` if `map` is not `null`.

  Parameters:
    `src` - the method that the body is copied from.
    `map` - the hashtable associating original class names with substituted names. It can be `null`.
    `CannotCompileException`

### setWrappedBody

    public void setWrappedBody(CtMethod mbody,
                               CtMethod.ConstParameter constParam)
                        throws CannotCompileException

  Replace a method body with a new method body wrapping the given method.

  Parameters:
    `mbody` - the wrapped method
    `constParam` - the constant parameter given to the wrapped method (maybe `null`).
    `CannotCompileException`
  See Also:
    `CtNewMethod.wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)`

### 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.

  Overrides:
    `getAttribute` in class `CtBehavior`
  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.

  Overrides:
    `setAttribute` in class `CtBehavior`
  Parameters:
    `name` - attribute name
    `data` - attribute value

### useCflow

    public void useCflow(java.lang.String name)
                  throws CannotCompileException

  Declares to use `$cflow` for this method. 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)`.

  Overrides:
    `useCflow` in class `CtBehavior`
  Parameters:
    `name` - `$cflow` name. It can include alphabets, numbers, `_`, `$`, and `.` (dot).
    `CannotCompileException`
  See Also:
    `Cflow`

### instrument

    public void instrument(CodeConverter converter)
                    throws CannotCompileException

  Modifies the method body.

  Overrides:
    `instrument` in class `CtBehavior`
  Parameters:
    `converter` - specifies how to modify.
    `CannotCompileException`

### instrument

    public void instrument(ExprEditor editor)
                    throws CannotCompileException

  Modifies the method body.

  Overrides:
    `instrument` in class `CtBehavior`
  Parameters:
    `editor` - specifies how to modify.
    `CannotCompileException`

### insertBefore

    public void insertBefore(java.lang.String src)
                      throws CannotCompileException

  Inserts bytecode at the beginning of the method body.

  Overrides:
    `insertBefore` in class `CtBehavior`
  Parameters:
    `src` - the source code representing the inserted bytecode. It must be a single statement or block.
    `CannotCompileException`

### insertAfter

    public void insertAfter(java.lang.String src)
                     throws CannotCompileException

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

  Overrides:
    `insertAfter` in class `CtBehavior`
  Parameters:
    `src` - the source code representing the inserted bytecode. It must be a single statement or block.
    `CannotCompileException`

### insertAfter

    public void insertAfter(java.lang.String src,
                            boolean asFinally)
                     throws CannotCompileException

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

  Overrides:
    `insertAfter` in class `CtBehavior`
  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 transfer normally returns but also when an exception is thrown.
    `CannotCompileException`

### addCatch

    public void addCatch(java.lang.String src,
                         CtClass exceptionType)
                  throws CannotCompileException

  Adds a catch clause that handles an exception thrown in the method body. The catch clause must end with a return or throw statement.

  Overrides:
    `addCatch` in class `CtBehavior`
  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.
    `CannotCompileException`

Overview | Package | Class | Use | Tree | Deprecated | Index
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

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