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 CtConstructor

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

public final class CtConstructor
extends CtBehavior

An instance of CtConstructor represents a constructor. It may represent a static constructor (class initializer). To distinguish a constructor and a class initializer, call `isClassInitializer()`.

See Also:
  `CtClass.getDeclaredConstructors()`, `CtClass.getClassInitializer()`, `CtNewConstructor`

Constructor Summary
`CtConstructor(CtClass[] parameters, CtClass declaring)` Creates a constructor with no constructor body.
`CtConstructor(CtConstructor src, CtClass declaring, ClassMap map)` Creates a copy of a `CtConstructor` object.

Method Summary
`void` | `addCatch(java.lang.String src, CtClass exceptionType)` Adds a catch clause that handles an exception thrown in the constructor body.
`byte[]` | `getAttribute(java.lang.String name)` Obtains an attribute with the given name.
`CtClass` | `getDeclaringClass()` Returns the class that declares this constructor.
`CtClass[]` | `getExceptionTypes()` Obtains exceptions that this constructor may throw.
`MethodInfo` | `getMethodInfo()` Returns the MethodInfo representing the constructor in the class file.
`int` | `getModifiers()` Obtains the encoded modifiers of the constructor.
`java.lang.String` | `getName()` Obtains the name of this constructor.
`CtClass[]` | `getParameterTypes()` Obtains parameter types of this constructor.
`java.lang.String` | `getSignature()` Returns the character string representing the parameter types.
`void` | `insertAfter(java.lang.String src)` Inserts bytecode at the end of the constructor body.
`void` | `insertAfter(java.lang.String src, boolean asFinally)` Inserts bytecode at the end of the constructor body.
`void` | `insertBefore(java.lang.String src)` Inserts bytecode at the beginning of the constructor body.
`void` | `insertBeforeBody(java.lang.String src)` Inserts bytecode just after another constructor in the super class or this class is called.
`void` | `instrument(CodeConverter converter)` Modifies the constructor body.
`void` | `instrument(ExprEditor editor)` Modifies the constructor body.
`boolean` | `isClassInitializer()` Returns true if this object represents a static initializer.
`boolean` | `isConstructor()` Returns true if this object represents a constructor.
`boolean` | `isEmpty()` Returns true if the constructor is the default one.
`void` | `setAttribute(java.lang.String name, byte[] data)` Adds an attribute.
`void` | `setBody(CtConstructor src, ClassMap map)` Copies a constructor body from another constructor.
`void` | `setBody(java.lang.String src)` Sets a constructor body.
`void` | `setExceptionTypes(CtClass[] types)` Sets exceptions that this constructor may throw.
`void` | `setModifiers(int mod)` Sets the encoded modifiers of the constructor.
`void` | `useCflow(java.lang.String name)` Declares to use `$cflow` for this constructor.

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

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

Constructor Detail

### CtConstructor

    public CtConstructor(CtClass[] parameters,
                         CtClass declaring)

  Creates a constructor with no constructor body. The created constructor must be added to a class with `CtClass.addConstructor()`.

  The created constructor does not include a constructor body, must be specified with `setBody()`.

Parameters:
  `declaring` - the class to which the created method is added.
  `parameters` - a list of the parameter types
See Also:
  `CtClass.addConstructor(CtConstructor)`, `setBody(String)`, `setBody(CtConstructor,ClassMap)`

### CtConstructor

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

  Creates a copy of a `CtConstructor` object. The created constructor must be added to a class with `CtClass.addConstructor()`.

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

  By default, all the occurrences of the names of the class declaring `src` and the superclass are replaced with the name of the class and the superclass that the created constructor 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.addConstructor(CtConstructor)`, `ClassMap.fix(String)`

Method Detail

### getMethodInfo

    public MethodInfo getMethodInfo()

  Returns the MethodInfo representing the constructor in the class file.

  Overrides:
    `getMethodInfo` in class `CtBehavior`

### isConstructor

    public boolean isConstructor()

  Returns true if this object represents a constructor.

### isClassInitializer

    public boolean isClassInitializer()

  Returns true if this object represents a static initializer.

### getModifiers

    public int getModifiers()

  Obtains the encoded modifiers of the constructor.

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

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

### getName

    public java.lang.String getName()

  Obtains the name of this constructor. It is the same as the simple name of the class declaring this constructor. If this object represents a class initializer, then this method returns `"<clinit>"`.

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

### getDeclaringClass

    public CtClass getDeclaringClass()

  Returns the class that declares this constructor.

  Overrides:
    `getDeclaringClass` in class `CtMember`

### getParameterTypes

    public CtClass[] getParameterTypes()
                                throws NotFoundException

  Obtains parameter types of this constructor.

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

### getSignature

    public java.lang.String getSignature()

  Returns the character string representing the parameter types. If two constructors have the same parameter types, `getSignature()` returns the same string.

  Overrides:
    `getSignature` in class `CtBehavior`

### getExceptionTypes

    public CtClass[] getExceptionTypes()
                                throws NotFoundException

  Obtains exceptions that this constructor may throw.

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

### setExceptionTypes

    public void setExceptionTypes(CtClass[] types)
                           throws NotFoundException

  Sets exceptions that this constructor may throw.

  Overrides:
    `setExceptionTypes` in class `CtBehavior`
    `NotFoundException`

### isEmpty

    public boolean isEmpty()

  Returns true if the constructor is the default one.

  Specified by:
    `isEmpty` in class `CtBehavior`

### setBody

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

  Sets a constructor body.

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

### setBody

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

  Copies a constructor body from another constructor.

  All occurrences of the class names in the copied 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`

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

  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 constructor body.

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

### instrument

    public void instrument(ExprEditor editor)
                    throws CannotCompileException

  Modifies the constructor 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 constructor body. Since the bytecode is inserted before a constructor in the super class or this class is called, the 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 members although it can access static members.

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

### insertBeforeBody

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

  Inserts bytecode just after another constructor in the super class or this class is called. It does not work if this object represents a class initializer.

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