Top Description Inners Fields Constructors Methods
javassist

public abstract Class CtClass

extends Object
Class Inheritance
Known Direct Subclasses
javassist.CtClassType, javassist.CtPrimitiveType, javassist.CtArray
Imports
java.io.BufferedOutputStream, .ByteArrayOutputStream, .DataOutputStream, .File, .FileOutputStream, .IOException, .OutputStream, java.net.URL, java.security.ProtectionDomain, java.util.Collection, javassist.bytecode.ClassFile, .Descriptor, .Opcode, javassist.expr.ExprEditor

An instance of CtClass represents a class. It is obtained from ClassPool.
See Also
ClassPool#get(String)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
public static CtClass
booleanType

The CtClass object representing the boolean type.

public static CtClass
byteType

The CtClass object representing the byte type.

public static CtClass
charType

The CtClass object representing the char type.

public static String
debugDump

If the value of this field is not null, then all class files modified by Javassist are saved under the directory specified by this variable.

public static CtClass
doubleType

The CtClass object representing the double type.

public static CtClass
floatType

The CtClass object representing the float type.

public static CtClass
intType

The CtClass object representing the int type.

pack-priv static final String
public static CtClass
longType

The CtClass object representing the long type.

pack-priv static CtClass[]
protected String
public static CtClass
shortType

The CtClass object representing the short type.

public static final String
version

The version number of this release.

public static CtClass
voidType

The CtClass object representing the void type.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
addConstructor(CtConstructor c)

Adds a constructor.

public void
addField(CtField f)

Adds a field.

public void
addField(CtField f, String
an expression for the initial value.
init
)

Adds a field with an initial value.

public void
addField(CtField f, CtField.Initializer
specifies the initial value of the field.
init
)

Adds a field with an initial value.

public void
addInterface(CtClass
the added interface.
anInterface
)

Adds an interface.

public void
addMethod(CtMethod m)

Adds a method.

pack-priv void
pack-priv void
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.

public void
debugWriteFile(String
it must end without a directory separator.
directoryName
)

Writes a class file as writeFile() does although this method does not prune or freeze the class after writing the class file.

public void
defrost()

Defrosts the class so that the class can be modified again.

public void
detach()

Removes this CtClass object from the ClassPool.

protected void
extendToString(StringBuilder buffer)

Implemented in subclasses to add to the toString() result.

public void
freeze()

Makes the class frozen.

public AccessorMaker
getAccessorMaker()

Undocumented method.

public Object

Returns:

the annotation if found, otherwise null.
getAnnotation
(Class<?>
the annotation type.
clz
)

Returns the annotation if the class has the specified annotation type.

public Object[]

Returns:

an array of annotation-type objects.
getAnnotations
()

Returns the annotations associated with this class.

public byte[]
getAttribute(String
attribute name
name
)

Obtains an attribute with the given name.

public Object[]

Returns:

an array of annotation-type objects.
getAvailableAnnotations
()

Returns the annotations associated with this class.

public ClassFile
getClassFile()

Returns a class file for this class.

public ClassFile
getClassFile2()

Returns a class file for this class (read only).

public CtConstructor
getClassInitializer()

Gets the class initializer (static constructor) declared in the class.

public ClassPool
getClassPool()

Returns a ClassPool for this class.

public CtClass
getComponentType()

If this object represents an array, this method returns the component type of the array.

public CtConstructor
getConstructor(String
method descriptor
desc
)

Returns the constructor with the given signature, which is represented by a character string called method descriptor.

public CtConstructor[]
getConstructors()

Returns an array containing CtConstructor objects representing all the non-private constructors of the class.

public CtBehavior[]
getDeclaredBehaviors()

Gets all the constructors and methods declared in the class.

public CtClass[]
getDeclaredClasses()

Returns an array of nested classes declared in the class.

public CtConstructor
getDeclaredConstructor(CtClass[]
parameter types.
params
)

Returns a constructor receiving the specified parameters.

public CtConstructor[]
getDeclaredConstructors()

Gets all the constructors declared in the class.

public CtField
getDeclaredField(String name)

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

public CtField
getDeclaredField(String
the field name.
name
,
String
the type descriptor of the field. It is available by CtField#getSignature().
desc
)

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

public CtField[]
getDeclaredFields()

Gets all the fields declared in the class.

public CtMethod
getDeclaredMethod(String
method name
name
,
CtClass[]
parameter types
params
)

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

public CtMethod
getDeclaredMethod(String name)

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

public CtMethod[]
getDeclaredMethods()

Gets all methods declared in the class.

public CtMethod[]
getDeclaredMethods(String
method name.
name
)

Retrieves methods with the specified name among the methods declared in the class.

public CtClass

Returns:

null if this class is a top-level class or an anonymous class.
getDeclaringClass
()

If this class is a member class or interface of another class, then the class enclosing this class is returned.

public CtBehavior

Returns:

null if this class is not a local class or an anonymous class.
getEnclosingBehavior
()

Returns the immediately enclosing method of this class.

public final CtMethod

Returns:

null if this class is not a local class or an anonymous class.
getEnclosingMethod
()
Deprecated The enclosing method might be a constructor. Use getEnclosingBehavior().

Returns the immediately enclosing method of this class.

public CtField
getField(String name)

Returns the field with the specified name.

public CtField
getField(String
the field name.
name
,
String
the type descriptor of the field. It is available by CtField#getSignature().
desc
)

Returns the field with the specified name and type.

pack-priv CtField

Returns:

null if the specified field is not found.
getField2
(String name, String desc)

public CtField[]
getFields()

Returns an array containing CtField objects representing all the non-private fields of the class.

public String

Returns:

null if the generic signature is not included.
getGenericSignature
()

Returns the generic signature of the class.

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

public CtMethod
getMethod(String
method name
name
,
String
method descriptor
desc
)

Returns the method with the given name and signature.

public CtMethod[]
getMethods()

Returns an array containing CtMethod objects representing all the non-private methods of the class.

public int
getModifiers()

Returns the modifiers for this class, encoded in an integer.

public String
getName()

Obtains the fully-qualified name of the class.

public CtClass[]
getNestedClasses()

Returns an array of nested classes declared in the class.

public final String
getPackageName()

Obtains the package name.

public synchronized Collection<String>

Returns:

a Collection<String> object.
getRefClasses
()

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

public final String
getSimpleName()

Obtains the not-qualified class name.

public CtClass
getSuperclass()

Obtains the class object representing the superclass of the class.

public URL
getURL()

Returns the uniform resource locator (URL) of the class file.

public boolean

Returns:

true if the annotation is found, otherwise false.
hasAnnotation
(Class<?>
the annotation type.
annotationType
)

Returns true if the class has the specified annotation type.

public boolean

Returns:

true if the annotation is found, otherwise false.
hasAnnotation
(String
the name of annotation type.
annotationTypeName
)

Returns true if the class has the specified annotation type.

pack-priv void
public void
instrument(CodeConverter
specifies how to modify.
converter
)

Applies the given converter to all methods and constructors declared in the class.

public void
instrument(ExprEditor
specifies how to modify.
editor
)

Modifies the bodies of all methods and constructors declared in the class.

public boolean
isAnnotation()

Determines whether this object represents an annotation type.

public boolean
isArray()

Returns true if this object represents an array type.

public boolean
isEnum()

Determines whether this object represents an enum.

public boolean
isFrozen()

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

public boolean
isInterface()

Determines whether this object represents a class or an interface.

public boolean
isKotlin()

Returns true if this object represents a Kotlin class.

public boolean
isModified()

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

public boolean
isPrimitive()

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

public static void
main(String[] args)

Prints the version number and the copyright notice.

public CtConstructor
makeClassInitializer()

Makes an empty class initializer (static constructor).

protected DataOutputStream
makeFileOutput(String directoryName)

public CtClass
makeNestedClass(String
the simple name of the nested class.
name
,
boolean
true if the nested class is static.
isStatic
)

Makes a new public nested class.

public String
makeUniqueName(String
the prefix of the member name.
prefix
)

Makes a unique member name.

public void
prune()

Discards unnecessary attributes, in particular, CodeAttributes (method bodies) of the class, to minimize the memory footprint.

public void
rebuildClassFile()

If this method is called, the class file will be rebuilt when it is finally generated by toBytecode() and writeFile().

public void
removeConstructor(CtConstructor
removed constructor.
c
)

Removes a constructor declared in this class.

public void
removeField(CtField
removed field.
f
)

Removes a field declared in this class.

public void
removeMethod(CtMethod
removed method.
m
)

Removes a method declared in this class.

public void
replaceClassName(String
replaced class name
oldName
,
String
substituted class name
newName
)

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

public void
replaceClassName(ClassMap
the hashtable associating replaced class names with substituted names.
map
)

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

public void
setAttribute(String
attribute name
name
,
byte[]
attribute value
data
)

Adds a named attribute.

public void
setGenericSignature(String
a generic signature.
sig
)

Sets the generic signature of the class.

public void
setInterfaces(CtClass[]
a list of the CtClass objects representing interfaces, or null if the class implements no interfaces.
list
)

Sets implemented interfaces.

public void
setModifiers(int
modifiers encoded by javassist.Modifier
mod
)

Sets the modifiers.

public void
setName(String
fully-qualified name
name
)

Sets the class name

public void
setSuperclass(CtClass clazz)

Changes a super class unless this object represents an interface.

public boolean

Returns:

the previous status of pruning. true if pruning is already stopped.
stopPruning
(boolean
disallow pruning if true. Otherwise, allow.
stop
)

Disallows (or allows) automatically pruning this CtClass object.

public boolean
subclassOf(CtClass superclass)

Determines whether the class directly or indirectly extends the given class.

public boolean
subtypeOf(CtClass clazz)

Returns true if this class extends or implements clazz.

public byte[]

Returns:

the contents of the class file.
toBytecode
()

Converts this class to a class file.

public void
toBytecode(DataOutputStream
the output stream that a class file is written to.
out
)

Converts this class to a class file.

public Class<?>
toClass()

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

public Class<?>
toClass(Class<?>
A class belonging to the same package that this class belongs to. It is used to load the class.
neighbor
)

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

public Class<?>
toClass(MethodHandles.Lookup
used when loading the class. It has to have an access right to define a new class.
lookup
)

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

public Class<?>
toClass(ClassLoader
the class loader used to load this class. If it is null, the class loader returned by ClassPool#getClassLoader() is used.
loader
,
ProtectionDomain
the protection domain that the class belongs to. If it is null, the default domain created by java.lang.ClassLoader is used.
domain
)

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

public final Class<?>
toClass(ClassLoader loader)

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

public String
toString()

Overrides java.lang.Object.toString.

Converts the object to a string.

public void
writeFile()

Writes a class file represented by this CtClass object in the current directory.

public void
writeFile(String
it must end without a directory separator.
directoryName
)

Writes a class file represented by this CtClass object on a local disk.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait