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 CtClass

    java.lang.Object
      |
      +--javassist.CtClass

Direct Known Subclasses:
  CtPrimitiveType

public abstract class CtClass
extends java.lang.Object

An instance of `CtClass` represents a class. It is obtained from `ClassPool`.

See Also:
  `ClassPool.get(String)`

Field Summary
`static CtClass` | `booleanType` The `CtClass` object representing the `boolean` type.
`static CtClass` | `byteType` The `CtClass` object representing the `byte` type.
`static CtClass` | `charType` The `CtClass` object representing the `char` type.
`static CtClass` | `doubleType` The `CtClass` object representing the `double` type.
`static CtClass` | `floatType` The `CtClass` object representing the `float` type.
`static CtClass` | `intType` The `CtClass` object representing the `int` type.
`static CtClass` | `longType` The `CtClass` object representing the `long` type.
`static CtClass` | `shortType` The `CtClass` object representing the `short` type.
`static java.lang.String` | `version` The version number of this release.
`static CtClass` | `voidType` The `CtClass` object representing the `void` type.

Method Summary
`void` | `addConstructor(CtConstructor c)` Adds a constructor.
`void` | `addField(CtField f)` Adds a field.
`void` | `addField(CtField f, CtField.Initializer init)` Adds a field with an initial value.
`void` | `addField(CtField f, java.lang.String init)` Adds a field with an initial value.
`void` | `addInterface(CtClass anInterface)` Adds an interface.
`void` | `addMethod(CtMethod m)` Adds a method.
`void` | `defrost()` Defrosts the class so that the class can be modified again.
`byte[]` | `getAttribute(java.lang.String name)` Obtains an attribute with the given name.
`ClassFile` | `getClassFile()` Returns a class file for this class.
`ClassFile` | `getClassFile2()` Undocumented method.
`CtConstructor` | `getClassInitializer()` Gets the class initializer (static constructor) declared in the class.
`ClassPool` | `getClassPool()` Returns a `ClassPool` for this class.
`CtClass` | `getComponentType()` If this object represents an array, this method returns the component type of the array.
`CtConstructor` | `getConstructor(java.lang.String desc)` Returns the constructor with the given signature, which is represented by a character string called method descriptor.
`CtConstructor[]` | `getConstructors()` Returns an array containing `CtConstructor` objects representing all the public constructors of the class.
`CtBehavior[]` | `getDeclaredBehaviors()` Gets all the constructors and methods declared in the class.
`CtConstructor` | `getDeclaredConstructor(CtClass[] params)` Returns a constructor receiving the specified parameters.
`CtConstructor[]` | `getDeclaredConstructors()` Gets all the constructors declared in the class.
`CtField` | `getDeclaredField(java.lang.String name)` Retrieves the field with the specified name among the fields declared in the class.
`CtField[]` | `getDeclaredFields()` Gets all the fields declared in the class.
`CtMethod` | `getDeclaredMethod(java.lang.String name)` Retrieves the method with the specified name among the methods declared in the class.
`CtMethod` | `getDeclaredMethod(java.lang.String name, CtClass[] params)` Retrieves the method with the specified name and parameter types among the methods declared in the class.
`CtMethod[]` | `getDeclaredMethods()` Gets all methods declared in the class.
`CtField` | `getField(java.lang.String name)` Returns the field with the specified name.
`CtField[]` | `getFields()` Returns an array containing `CtField` objects representing all the public fields of the class.
`CtClass[]` | `getInterfaces()` Obtains the class objects representing the interfaces of the class.
`CtMethod` | `getMethod(java.lang.String name, java.lang.String desc)` Returns the method with the given name and signature.
`CtMethod[]` | `getMethods()` Returns an array containing `CtMethod` objects representing all the public methods of the class.
`int` | `getModifiers()` Returns the modifiers for this class, encoded in an integer.
`java.lang.String` | `getName()` Obtains the fully-qualified name of the class.
`java.lang.String` | `getPackageName()` Obtains the package name.
`java.util.Collection` | `getRefClasses()` Returns a collection of the names of all the classes referenced in this class.
`java.lang.String` | `getSimpleName()` Obtains the not-qualified class name.
`CtClass` | `getSuperclass()` Obtains the class object representing the superclass of the class.
`void` | `instrument(CodeConverter converter)` Applies the given converter to all methods and constructors declared in the class.
`void` | `instrument(ExprEditor editor)` Modifies the bodies of all methods and constructors declared in the class.
`boolean` | `isArray()` Returns `true` if this object represents an array type.
`boolean` | `isFrozen()` Returns true if the class has been loaded or written out and thus it cannot be modified any more.
`boolean` | `isInterface()` Determines whether this object represents a class or an interface.
`boolean` | `isModified()` Returns true if the definition of the class has been modified.
`boolean` | `isPrimitive()` Returns `true` if this object represents a primitive Java type: boolean, byte, char, short, int, long, float, double, or void.
`CtConstructor` | `makeClassInitializer()` Makes a class initializer (static constructor).
`void` | `replaceClassName(ClassMap map)` Changes class names appearing in the class file according to the given `map`.
`void` | `replaceClassName(java.lang.String oldname, java.lang.String newname)` Substitutes `newName` for all occurrences of a class name `oldName` in the class file.
`void` | `setAttribute(java.lang.String name, byte[] data)` Adds a named attribute.
`void` | `setInterfaces(CtClass[] list)` Sets interfaces.
`void` | `setModifiers(int mod)` Sets the modifiers.
`void` | `setName(java.lang.String name)` Sets the class name
`void` | `setSuperclass(CtClass clazz)` Changes a super class.
`boolean` | `subclassOf(CtClass superclass)` Determines whether the class directly or indirectly extends the given class.
`boolean` | `subtypeOf(CtClass clazz)` Returns `true` if this class extends or implements `clazz`.
`byte[]` | `toBytecode()` Converts this class to a class file.
`java.lang.Class` | `toClass()` Converts this class to a `java.lang.Class` object.
`void` | `writeFile()` Writes a class file represented by this `CtClass` object in the current directory.

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

Field Detail

### version

    public static final java.lang.String version

  The version number of this release.

  See Also:
    Constant Field Values

### booleanType

    public static CtClass booleanType

  The `CtClass` object representing the `boolean` type.

### charType

    public static CtClass charType

  The `CtClass` object representing the `char` type.

### byteType

    public static CtClass byteType

  The `CtClass` object representing the `byte` type.

### shortType

    public static CtClass shortType

  The `CtClass` object representing the `short` type.

### intType

    public static CtClass intType

  The `CtClass` object representing the `int` type.

### longType

    public static CtClass longType

  The `CtClass` object representing the `long` type.

### floatType

    public static CtClass floatType

  The `CtClass` object representing the `float` type.

### doubleType

    public static CtClass doubleType

  The `CtClass` object representing the `double` type.

### voidType

    public static CtClass voidType

  The `CtClass` object representing the `void` type.

Method Detail

### getClassPool

    public ClassPool getClassPool()

  Returns a `ClassPool` for this class.

### getClassFile

    public ClassFile getClassFile()

  Returns a class file for this class.

  This method is not available if `isFrozen()` is true.

### getClassFile2

    public ClassFile getClassFile2()

  Undocumented method. Do not use; internal-use only.

### isModified

    public boolean isModified()

  Returns true if the definition of the class has been modified.

### isFrozen

    public boolean isFrozen()

  Returns true if the class has been loaded or written out and thus it cannot be modified any more.

  See Also:
    `defrost()`

### defrost

    public void defrost()

  Defrosts the class so that the class can be modified again. To avoid changes that will be never reflected, the class is frozen to be unmodifiable if it is loaded or written out. This method should be called only in a case that the class will be reloaded or written out later again.

  See Also:
    `isFrozen()`

### isPrimitive

    public boolean isPrimitive()

  Returns `true` if this object represents a primitive Java type: boolean, byte, char, short, int, long, float, double, or void.

### isArray

    public boolean isArray()

  Returns `true` if this object represents an array type.

### getComponentType

    public CtClass getComponentType()
                             throws NotFoundException

  If this object represents an array, this method returns the component type of the array. Otherwise, it returns `null`.

    `NotFoundException`

### subtypeOf

    public boolean subtypeOf(CtClass clazz)
                      throws NotFoundException

  Returns `true` if this class extends or implements `clazz`. It also returns `true` if this class is the same as `clazz`.

    `NotFoundException`

### getName

    public java.lang.String getName()

  Obtains the fully-qualified name of the class.

### getSimpleName

    public final java.lang.String getSimpleName()

  Obtains the not-qualified class name.

### getPackageName

    public final java.lang.String getPackageName()

  Obtains the package name. It may be `null`.

### setName

    public void setName(java.lang.String name)

  Sets the class name

  Parameters:
    `name` - fully-qualified name

### replaceClassName

    public void replaceClassName(java.lang.String oldname,
                                 java.lang.String newname)

  Substitutes `newName` for all occurrences of a class name `oldName` in the class file.

### replaceClassName

    public void replaceClassName(ClassMap map)

  Changes class names appearing in the class file according to the given `map`.

  All the class names appearing in the class file are tested with `map` to determine whether each class name is replaced or not. Thus this method can be used for collecting all the class names in the class file. To do that, first define a subclass of `ClassMap` so that `get()` records all the given parameters. Then, make an instance of that subclass as an empty hash-table. Finally, pass that instance to this method. After this method finishes, that instance would contain all the class names appearing in the class file.

  Parameters:
    `map` - the hashtable associating replaced class names with substituted names.

### getRefClasses

    public java.util.Collection getRefClasses()

  Returns a collection of the names of all the classes referenced in this class. That collection includes the name of this class.

  This method may return `null`.

### isInterface

    public boolean isInterface()

  Determines whether this object represents a class or an interface. It returns `true` if this object represents an interface.

### getModifiers

    public int getModifiers()

  Returns the modifiers for this class, encoded in an integer. For decoding, use `javassist.Modifier`.

  See Also:
    `Modifier`

### setModifiers

    public void setModifiers(int mod)

  Sets the modifiers.

  Parameters:
    `mod` - modifiers encoded by `javassist.Modifier`
  See Also:
    `Modifier`

### subclassOf

    public boolean subclassOf(CtClass superclass)

  Determines whether the class directly or indirectly extends the given class. If this class extends a class A and the class A extends a class B, then subclassof(B) returns true.

  This method returns true if the given class is identical to the class represented by this object.

### getSuperclass

    public CtClass getSuperclass()
                          throws NotFoundException

  Obtains the class object representing the superclass of the class. It returns null if this object represents the `java.lang.Object` class and thus it does not have the super class.

    `NotFoundException`

### setSuperclass

    public void setSuperclass(CtClass clazz)
                       throws CannotCompileException

  Changes a super class. The new super class must be compatible with the old one.

    `CannotCompileException`

### getInterfaces

    public CtClass[] getInterfaces()
                            throws NotFoundException

  Obtains the class objects representing the interfaces of the class.

    `NotFoundException`

### setInterfaces

    public void setInterfaces(CtClass[] list)

  Sets interfaces.

  Parameters:
    `list` - a list of the `CtClass` objects representing interfaces, or `null` if the class implements no interfaces.

### addInterface

    public void addInterface(CtClass anInterface)

  Adds an interface.

  Parameters:
    `anInterface` - the added interface.

### getFields

    public CtField[] getFields()

  Returns an array containing `CtField` objects representing all the public fields of the class. That array includes public fields inherited from the superclasses.

### getField

    public CtField getField(java.lang.String name)
                     throws NotFoundException

  Returns the field with the specified name. The returned field may be a private field declared in a super class or interface.

    `NotFoundException`

### getDeclaredFields

    public CtField[] getDeclaredFields()

  Gets all the fields declared in the class. The inherited fields are not included.

  Note: the result does not include inherited fields.

### getDeclaredField

    public CtField getDeclaredField(java.lang.String name)
                             throws NotFoundException

  Retrieves the field with the specified name among the fields declared in the class.

  Note: this method does not search the superclasses.

    `NotFoundException`

### getDeclaredBehaviors

    public CtBehavior[] getDeclaredBehaviors()

  Gets all the constructors and methods declared in the class.

### getConstructors

    public CtConstructor[] getConstructors()

  Returns an array containing `CtConstructor` objects representing all the public constructors of the class.

### getConstructor

    public CtConstructor getConstructor(java.lang.String desc)
                                 throws NotFoundException

  Returns the constructor with the given signature, which is represented by a character string called method descriptor. For details of the method descriptor, see the JVM specification or `javassist.bytecode.Descriptor`.

  Parameters:
    `desc` - method descriptor
    `NotFoundException`
  See Also:
    `Descriptor`

### getDeclaredConstructors

    public CtConstructor[] getDeclaredConstructors()

  Gets all the constructors declared in the class.

  See Also:
    `CtConstructor`

### getDeclaredConstructor

    public CtConstructor getDeclaredConstructor(CtClass[] params)
                                         throws NotFoundException

  Returns a constructor receiving the specified parameters.

  Parameters:
    `params` - parameter types.
    `NotFoundException`

### getClassInitializer

    public CtConstructor getClassInitializer()

  Gets the class initializer (static constructor) declared in the class. This method returns `null` if no class initializer is not declared.

  See Also:
    `makeClassInitializer()`, `CtConstructor`

### getMethods

    public CtMethod[] getMethods()

  Returns an array containing `CtMethod` objects representing all the public methods of the class. That array includes public methods inherited from the superclasses.

### getMethod

    public CtMethod getMethod(java.lang.String name,
                              java.lang.String desc)
                       throws NotFoundException

  Returns the method with the given name and signature. The returned method may be declared in a super class. The method signature is represented by a character string called method descriptor, which is defined in the JVM specification.

  Parameters:
    `name` - method name
    `desc` - method descriptor
    `NotFoundException`
  See Also:
    `Descriptor`

### getDeclaredMethods

    public CtMethod[] getDeclaredMethods()

  Gets all methods declared in the class. The inherited methods are not included.

  See Also:
    `CtMethod`

### getDeclaredMethod

    public CtMethod getDeclaredMethod(java.lang.String name,
                                      CtClass[] params)
                               throws NotFoundException

  Retrieves the method with the specified name and parameter types among the methods declared in the class.

  Note: this method does not search the superclasses.

  Parameters:
    `name` - method name
    `params` - parameter types
    `NotFoundException`
  See Also:
    `CtMethod`

### getDeclaredMethod

    public CtMethod getDeclaredMethod(java.lang.String name)
                               throws NotFoundException

  Retrieves the method with the specified name among the methods declared in the class. If there are multiple methods with the specified name, then this method returns one of them.

  Note: this method does not search the superclasses.

    `NotFoundException`
  See Also:
    `CtMethod`

### makeClassInitializer

    public CtConstructor makeClassInitializer()
                                       throws CannotCompileException

  Makes a class initializer (static constructor). If the class already includes a class initializer, this method returns it.

    `CannotCompileException`
  See Also:
    `getClassInitializer()`

### addConstructor

    public void addConstructor(CtConstructor c)
                        throws CannotCompileException

  Adds a constructor.

    `CannotCompileException`

### addMethod

    public void addMethod(CtMethod m)
                   throws CannotCompileException

  Adds a method.

    `CannotCompileException`

### addField

    public void addField(CtField f)
                  throws CannotCompileException

  Adds a field.

  The `CtField` belonging to another `CtClass` cannot be directly added to this class. Only a field created for this class can be added.

    `CannotCompileException`
  See Also:
    `CtField.CtField(CtField,CtClass)`

### addField

    public void addField(CtField f,
                         java.lang.String init)
                  throws CannotCompileException

  Adds a field with an initial value.

  The `CtField` belonging to another `CtClass` cannot be directly added to this class. Only a field created for this class can be added.

  The initial value is given as an expression written in Java. Any regular Java expression can be used for specifying the initial value. The followings are examples.

       cc.addField(f, "0")               // the initial value is 0.
       cc.addField(f, "i + 1")           // i + 1.
       cc.addField(f, "new Point()");    // a Point object.

  Here, the type of variable `cc` is `CtClass`. The type of `f` is `CtField`.

  Parameters:
    `init` - an expression for the initial value.
    `CannotCompileException`
  See Also:
    `CtField.Initializer.byExpr(String)`, `CtField.CtField(CtField,CtClass)`

### addField

    public void addField(CtField f,
                         CtField.Initializer init)
                  throws CannotCompileException

  Adds a field with an initial value.

  The `CtField` belonging to another `CtClass` cannot be directly added to this class. Only a field created for this class can be added.

  For example,

       CtClass cc = ...;
       addField(new CtField(CtClass.intType, "i", cc),
                CtField.Initializer.constant(1));

  This code adds an `int` field named "i". The initial value of this field is 1.

  Parameters:
    `init` - specifies the initial value of the field.
    `CannotCompileException`
  See Also:
    `CtField.CtField(CtField,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.

  Parameters:
    `name` - attribute name

### setAttribute

    public void setAttribute(java.lang.String name,
                             byte[] data)

  Adds a named attribute. An arbitrary data (smaller than 64Kb) can be saved in the class file. Some attribute name are reserved by the JVM. The attributes with the non-reserved names are ignored when a class file is loaded into the JVM. If there is already an attribute with the same name, this method substitutes the new one for it.

  Parameters:
    `name` - attribute name
    `data` - attribute value

### instrument

    public void instrument(CodeConverter converter)
                    throws CannotCompileException

  Applies the given converter to all methods and constructors declared in the class. This method calls `instrument()` on every `CtMethod` and `CtConstructor` object in the class.

  Parameters:
    `converter` - specifies how to modify.
    `CannotCompileException`

### instrument

    public void instrument(ExprEditor editor)
                    throws CannotCompileException

  Modifies the bodies of all methods and constructors declared in the class. This method calls `instrument()` on every `CtMethod` and `CtConstructor` object in the class.

  Parameters:
    `editor` - specifies how to modify.
    `CannotCompileException`

### toClass

    public java.lang.Class toClass()
                            throws NotFoundException,
                                   java.io.IOException,
                                   CannotCompileException

  Converts this class to a `java.lang.Class` object. Once this method is called, further modifications are not possible any more.

  This method is equivalent to:

      this.getClassPool().writeAsClass(this.getName())

  See the description of `ClassPool.writeAsClass()` before you use this method. This method is provided for convenience. If you need more complex functionality, you should write your own class loader.

    `NotFoundException`
    `java.io.IOException`
    `CannotCompileException`
  See Also:
    `ClassPool.writeAsClass(String)`, `ClassPool.forName(String)`

### toBytecode

    public byte[] toBytecode()
                      throws NotFoundException,
                             java.io.IOException,
                             CannotCompileException

  Converts this class to a class file. Once this method is called, further modifications are not possible any more.

  This method is equivalent to:

      this.getClassPool().write(this.getName())

    `NotFoundException`
    `java.io.IOException`
    `CannotCompileException`
  See Also:
    `ClassPool.write(String)`

### writeFile

    public void writeFile()
                   throws NotFoundException,
                          java.io.IOException,
                          CannotCompileException

  Writes a class file represented by this `CtClass` object in the current directory. Once this method is called, further modifications are not possible any more.

  This method is equivalent to:

      this.getClassPool().writeFile(this.getName())

    `NotFoundException`
    `java.io.IOException`
    `CannotCompileException`
  See Also:
    `ClassPool.writeFile(String)`

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.