javassist
Class CtClass

java.lang.Object
  extended by 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 debugWriteFile()
          Writes a class file as writeFile() does although this method does not prune or freeze the class after writing the class file.
 void debugWriteFile(java.lang.String directoryName)
          Writes a class file as writeFile() does although this method does not prune or freeze the class after writing the class file.
 void defrost()
          Defrosts the class so that the class can be modified again.
 void detach()
          Removes this CtClass object from the ClassPool.
 void freeze()
          Makes the class frozen.
 javassist.compiler.AccessorMaker getAccessorMaker()
          Undocumented method.
 java.lang.Object[] getAnnotations()
          Returns the annotations associated with this class.
 byte[] getAttribute(java.lang.String name)
          Obtains an attribute with the given name.
 java.lang.Object[] getAvailableAnnotations()
          Returns the annotations associated with this class.
 ClassFile getClassFile()
          Returns a class file for this class.
 ClassFile getClassFile2()
          Returns a class file for this class (read only).
 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 non-private 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.
 CtClass getDeclaringClass()
          If this class is a member class or interface of another class, then the class enclosing this class is returned.
 CtMethod getEnclosingMethod()
          Returns the immediately enclosing method of this 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 non-private fields of the class.
 CtClass[] getInterfaces()
          Obtains the class objects representing the interfaces implemented by the class or, if this object represents an interface, the interfaces extended by that interface.
 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 non-private 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.
 CtClass[] getNestedClasses()
          Returns an array of nested classes declared in 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.
 java.net.URL getURL()
          Returns the uniform resource locator (URL) of the class file.
 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 isAnnotation()
          Determines whether this object represents an annotation type.
 boolean isArray()
          Returns true if this object represents an array type.
 boolean isEnum()
          Determines whether this object represents an enum.
 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.
static void main(java.lang.String[] args)
          Prints the version number and the copyright notice.
 CtConstructor makeClassInitializer()
          Makes an empty class initializer (static constructor).
 CtClass makeNestedClass(java.lang.String name, boolean isStatic)
          Makes a new public nested class.
 java.lang.String makeUniqueName(java.lang.String prefix)
          Makes a unique member name.
 void prune()
          Discards unnecessary attributes, in particular, CodeAttributes (method bodies) of the class, to minimize the memory footprint.
 void rebuildClassFile()
          If this method is called, the class file will be rebuilt when it is finally generated by toBytecode() and writeFile().
 void removeConstructor(CtConstructor c)
          Removes a constructor declared in this class.
 void removeField(CtField f)
          Removes a field declared in this class.
 void removeMethod(CtMethod m)
          Removes a method declared in this class.
 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 implemented 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 unless this object represents an interface.
 boolean stopPruning(boolean stop)
          Disallows (or allows) automatically pruning this CtClass object.
 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.
 void toBytecode(java.io.DataOutputStream out)
          Converts this class to a class file.
 java.lang.Class toClass()
          Converts this class to a java.lang.Class object.
 java.lang.Class toClass(java.lang.ClassLoader loader)
          Deprecated. Replaced by toClass(ClassLoader,ProtectionDomain)
 java.lang.Class toClass(java.lang.ClassLoader loader, java.security.ProtectionDomain domain)
          Converts this class to a java.lang.Class object.
 java.lang.String toString()
          Converts the object to a string.
 void writeFile()
          Writes a class file represented by this CtClass object in the current directory.
 void writeFile(java.lang.String directoryName)
          Writes a class file represented by this CtClass object on a local disk.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, 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

main

public static void main(java.lang.String[] args)
Prints the version number and the copyright notice.

The following command invokes this method:


toString

public java.lang.String toString()
Converts the object to a string.

Overrides:
toString in class java.lang.Object

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()
Returns a class file for this class (read only). Normal applications do not need calling this method. Use getClassFile().

The ClassFile 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.

This method is available even if isFrozen() is true. However, if the class is frozen, it might be also pruned.

See Also:
getClassFile(), isFrozen(), prune()

getAccessorMaker

public javassist.compiler.AccessorMaker getAccessorMaker()
Undocumented method. Do not use; internal-use only.


getURL

public java.net.URL getURL()
                    throws NotFoundException
Returns the uniform resource locator (URL) of the class file.

Throws:
NotFoundException

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(), detach()

freeze

public void freeze()
Makes the class frozen.

Since:
3.6
See Also:
isFrozen(), 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.

If defrost() will be called later, pruning must be disallowed in advance.

See Also:
isFrozen(), stopPruning(boolean), detach()

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.

Throws:
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.

Throws:
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.

Parameters:
oldName - replaced class name
newName - substituted class name

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.


isAnnotation

public boolean isAnnotation()
Determines whether this object represents an annotation type. It returns true if this object represents an annotation type.

Since:
3.2

isEnum

public boolean isEnum()
Determines whether this object represents an enum. It returns true if this object represents an enum.

Since:
3.2

getModifiers

public int getModifiers()
Returns the modifiers for this class, encoded in an integer. For decoding, use javassist.Modifier.

If the class is a static nested class (a.k.a. static inner class), the returned modifiers include Modifier.STATIC.

See Also:
Modifier

getAnnotations

public java.lang.Object[] getAnnotations()
                                  throws java.lang.ClassNotFoundException
Returns the annotations associated with this class. For example, if an annotation @Author is associated with this class, the returned array contains an Author object. The member values can be obtained by calling methods on the Author object.

Returns:
an array of annotation-type objects.
Throws:
java.lang.ClassNotFoundException
Since:
3.1
See Also:
CtMember.getAnnotations()

getAvailableAnnotations

public java.lang.Object[] getAvailableAnnotations()
Returns the annotations associated with this class. This method is equivalent to getAnnotations() except that, if any annotations are not on the classpath, they are not included in the returned array.

Returns:
an array of annotation-type objects.
Since:
3.3
See Also:
getAnnotations(), CtMember.getAvailableAnnotations()

getNestedClasses

public CtClass[] getNestedClasses()
                           throws NotFoundException
Returns an array of nested classes declared in the class. Nested classes are inner classes, anonymous classes, local classes, and static nested classes.

Throws:
NotFoundException
Since:
3.2

setModifiers

public void setModifiers(int mod)
Sets the modifiers.

If the class is a nested class, this method also modifies the class declaring that nested class (i.e. the enclosing class is modified).

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.

If this object represents an interface, this method always returns the java.lang.Object class. To obtain the super interfaces extended by that interface, call getInterfaces().

Throws:
NotFoundException

setSuperclass

public void setSuperclass(CtClass clazz)
                   throws CannotCompileException
Changes a super class unless this object represents an interface. The new super class must be compatible with the old one; for example, it should inherit from the old super class.

If this object represents an interface, this method is equivalent to addInterface(); it appends clazz to the list of the super interfaces extended by that interface. Note that an interface can extend multiple super interfaces.

Throws:
CannotCompileException
See Also:
replaceClassName(String, String), replaceClassName(ClassMap)

getInterfaces

public CtClass[] getInterfaces()
                        throws NotFoundException
Obtains the class objects representing the interfaces implemented by the class or, if this object represents an interface, the interfaces extended by that interface.

Throws:
NotFoundException

setInterfaces

public void setInterfaces(CtClass[] list)
Sets implemented interfaces. If this object represents an interface, this method sets the interfaces extended by that interface.

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.

getDeclaringClass

public CtClass getDeclaringClass()
                          throws NotFoundException
If this class is a member class or interface of another class, then the class enclosing this class is returned.

Returns:
null if this class is a top-level class or an anonymous class.
Throws:
NotFoundException

getEnclosingMethod

public CtMethod getEnclosingMethod()
                            throws NotFoundException
Returns the immediately enclosing method of this class. This method works only with JDK 1.5 or later.

Returns:
null if this class is not a local class or an anonymous class.
Throws:
NotFoundException

makeNestedClass

public CtClass makeNestedClass(java.lang.String name,
                               boolean isStatic)
Makes a new public nested class. If this method is called, the CtClass, which encloses the nested class, is modified since a class file includes a list of nested classes.

The current implementation only supports a static nested class. isStatic must be true.

Parameters:
name - the simple name of the nested class.
isStatic - true if the nested class is static.

getFields

public CtField[] getFields()
Returns an array containing CtField objects representing all the non-private fields of the class. That array includes non-private 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.

Throws:
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.

Throws:
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 non-private 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
Throws:
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.
Throws:
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 non-private methods of the class. That array includes non-private 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
Throws:
NotFoundException
See Also:
CtBehavior.getSignature(), 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
Throws:
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.

Throws:
NotFoundException
See Also:
CtMethod

makeClassInitializer

public CtConstructor makeClassInitializer()
                                   throws CannotCompileException
Makes an empty class initializer (static constructor). If the class already includes a class initializer, this method returns it.

Throws:
CannotCompileException
See Also:
getClassInitializer()

addConstructor

public void addConstructor(CtConstructor c)
                    throws CannotCompileException
Adds a constructor. To add a class initializer (static constructor), call makeClassInitializer().

Throws:
CannotCompileException
See Also:
makeClassInitializer()

removeConstructor

public void removeConstructor(CtConstructor c)
                       throws NotFoundException
Removes a constructor declared in this class.

Parameters:
c - removed constructor.
Throws:
NotFoundException - if the constructor is not found.

addMethod

public void addMethod(CtMethod m)
               throws CannotCompileException
Adds a method.

Throws:
CannotCompileException

removeMethod

public void removeMethod(CtMethod m)
                  throws NotFoundException
Removes a method declared in this class.

Parameters:
m - removed method.
Throws:
NotFoundException - if the method is not found.

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.

Throws:
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.

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

Note: do not change the modifier of the field (in particular, do not add or remove static to/from the modifier) after it is added to the class by addField().

Parameters:
init - an expression for the initial value.
Throws:
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,

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.
Throws:
CannotCompileException
See Also:
CtField.CtField(CtField,CtClass)

removeField

public void removeField(CtField f)
                 throws NotFoundException
Removes a field declared in this class.

Parameters:
f - removed field.
Throws:
NotFoundException - if the field is not found.

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.

This is a convenient method mainly for obtaining a user-defined attribute. For dealing with attributes, see the javassist.bytecode package. For example, the following expression returns all the attributes of a class file.

Parameters:
name - attribute name
See Also:
AttributeInfo

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.

This is a convenient method mainly for adding a user-defined attribute. For dealing with attributes, see the javassist.bytecode package. For example, the following expression adds an attribute info to a class file.

Parameters:
name - attribute name
data - attribute value
See Also:
AttributeInfo

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.
Throws:
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.
Throws:
CannotCompileException

toClass

public java.lang.Class toClass()
                        throws CannotCompileException
Converts this class to a java.lang.Class object. Once this method is called, further modifications are not allowed any more. To load the class, this method uses the context class loader of the current thread. If the program is running on some application server, the context class loader might be inappropriate to load the class.

This method is provided for convenience. If you need more complex functionality, you should write your own class loader.

Note: this method calls toClass() in ClassPool.

Warining: A Class object returned by this method may not work with a security manager or a signed jar file because a protection domain is not specified.

Throws:
CannotCompileException
See Also:
toClass(java.lang.ClassLoader,ProtectionDomain), ClassPool.toClass(CtClass)

toClass

public java.lang.Class toClass(java.lang.ClassLoader loader,
                               java.security.ProtectionDomain domain)
                        throws CannotCompileException
Converts this class to a java.lang.Class object. Once this method is called, further modifications are not allowed any more.

The class file represented by this CtClass is loaded by the given class loader to construct a java.lang.Class object. Since a private method on the class loader is invoked through the reflection API, the caller must have permissions to do that.

An easy way to obtain ProtectionDomain object is to call getProtectionDomain() in java.lang.Class. It returns the domain that the class belongs to.

This method is provided for convenience. If you need more complex functionality, you should write your own class loader.

Note: this method calls toClass() in ClassPool.

Parameters:
loader - the class loader used to load this class. If it is null, the class loader returned by ClassPool.getClassLoader() is used.
domain - the protection domain that the class belongs to. If it is null, the default domain created by java.lang.ClassLoader is used.
Throws:
CannotCompileException
Since:
3.3
See Also:
ClassPool.toClass(CtClass,java.lang.ClassLoader)

toClass

public final java.lang.Class toClass(java.lang.ClassLoader loader)
                              throws CannotCompileException
Deprecated. Replaced by toClass(ClassLoader,ProtectionDomain)

Converts this class to a java.lang.Class object.

Warining: A Class object returned by this method may not work with a security manager or a signed jar file because a protection domain is not specified.

Throws:
CannotCompileException

detach

public void detach()
Removes this CtClass object from the ClassPool. After this method is called, any method cannot be called on the removed CtClass object.

If get() in ClassPool is called with the name of the removed method, the ClassPool will read the class file again and constructs another CtClass object representing the same class.


stopPruning

public boolean stopPruning(boolean stop)
Disallows (or allows) automatically pruning this CtClass object.

Javassist can automatically prune a CtClass object when toBytecode() (or toClass(), writeFile()) is called. Since a ClassPool holds all instances of CtClass even after toBytecode() (or toClass(), writeFile()) is called, pruning may significantly save memory consumption.

If ClassPool.doPruning is true, the automatic pruning is on by default. Otherwise, it is off. The default value of ClassPool.doPruning is false.

Parameters:
stop - disallow pruning if true. Otherwise, allow.
Returns:
the previous status of pruning. true if pruning is already stopped.
See Also:
detach(), prune(), ClassPool.doPruning

prune

public void prune()
Discards unnecessary attributes, in particular, CodeAttributes (method bodies) of the class, to minimize the memory footprint. After calling this method, the class is read only. It cannot be modified any more. Furthermore, toBytecode(), writeFile(), toClass(), or instrument() cannot be called. However, the method names and signatures in the class etc. are still accessible.

toBytecode(), writeFile(), and toClass() internally call this method if automatic pruning is on.

According to some experiments, pruning does not really reduce memory consumption. Only about 20%. Since pruning takes time, it might not pay off. So the automatic pruning is off by default.

See Also:
stopPruning(boolean), detach(), ClassPool.doPruning, toBytecode(), toClass(), writeFile(), instrument(CodeConverter), instrument(ExprEditor)

rebuildClassFile

public void rebuildClassFile()
If this method is called, the class file will be rebuilt when it is finally generated by toBytecode() and writeFile(). For a performance reason, the symbol table of the class file may contain unused entries, for example, after a method or a filed is deleted. This method removes those unused entries. This removal will minimize the size of the class file.

Since:
3.8.1

toBytecode

public byte[] toBytecode()
                  throws java.io.IOException,
                         CannotCompileException
Converts this class to a class file. Once this method is called, further modifications are not possible any more.

Returns:
the contents of the class file.
Throws:
java.io.IOException
CannotCompileException

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.

Throws:
NotFoundException
java.io.IOException
CannotCompileException
See Also:
debugWriteFile()

writeFile

public void writeFile(java.lang.String directoryName)
               throws CannotCompileException,
                      java.io.IOException
Writes a class file represented by this CtClass object on a local disk. Once this method is called, further modifications are not possible any more.

Parameters:
directoryName - it must end without a directory separator.
Throws:
CannotCompileException
java.io.IOException
See Also:
debugWriteFile(String)

debugWriteFile

public void debugWriteFile()
Writes a class file as writeFile() does although this method does not prune or freeze the class after writing the class file. Note that, once writeFile() or toBytecode() is called, it cannot be called again since the class is pruned and frozen. This method would be useful for debugging.


debugWriteFile

public void debugWriteFile(java.lang.String directoryName)
Writes a class file as writeFile() does although this method does not prune or freeze the class after writing the class file. Note that, once writeFile() or toBytecode() is called, it cannot be called again since the class is pruned and frozen. This method would be useful for debugging.

Parameters:
directoryName - it must end without a directory separator.

toBytecode

public void toBytecode(java.io.DataOutputStream out)
                throws CannotCompileException,
                       java.io.IOException
Converts this class to a class file. Once this method is called, further modifications are not possible any more.

This method dose not close the output stream in the end.

Parameters:
out - the output stream that a class file is written to.
Throws:
CannotCompileException
java.io.IOException

makeUniqueName

public java.lang.String makeUniqueName(java.lang.String prefix)
Makes a unique member name. This method guarantees that the returned name is not used as a prefix of any methods or fields visible in this class. If the returned name is XYZ, then any method or field names in this class do not start with XYZ.

Parameters:
prefix - the prefix of the member name.


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