Class CtClass
- Direct Known Subclasses:
CtPrimitiveType
CtClass
represents a class.
It is obtained from ClassPool
.- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic CtClass
TheCtClass
object representing theboolean
type.static CtClass
TheCtClass
object representing thebyte
type.static CtClass
TheCtClass
object representing thechar
type.static String
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.static CtClass
TheCtClass
object representing thedouble
type.static CtClass
TheCtClass
object representing thefloat
type.static CtClass
TheCtClass
object representing theint
type.static CtClass
TheCtClass
object representing thelong
type.static CtClass
TheCtClass
object representing theshort
type.static final String
The version number of this release.static CtClass
TheCtClass
object representing thevoid
type. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a constructor.void
Adds a field.void
Adds a field with an initial value.void
addField
(CtField f, CtField.Initializer init) Adds a field with an initial value.void
addInterface
(CtClass anInterface) Adds an interface.void
Adds a method.void
Writes a class file aswriteFile()
does although this method does not prune or freeze the class after writing the class file.void
debugWriteFile
(String directoryName) Writes a class file aswriteFile()
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 thisCtClass
object from theClassPool
.void
freeze()
Makes the class frozen.javassist.compiler.AccessorMaker
Undocumented method.getAnnotation
(Class<?> clz) Returns the annotation if the class has the specified annotation type.Object[]
Returns the annotations associated with this class.byte[]
getAttribute
(String name) Obtains an attribute with the given name.Object[]
Returns the annotations associated with this class.Returns a class file for this class.Returns a class file for this class (read only).Gets the class initializer (static constructor) declared in the class.Returns aClassPool
for this class.If this object represents an array, this method returns the component type of the array.getConstructor
(String desc) Returns the constructor with the given signature, which is represented by a character string called method descriptor.Returns an array containingCtConstructor
objects representing all the non-private constructors of the class.Gets all the constructors and methods declared in the class.CtClass[]
Returns an array of nested classes declared in the class.getDeclaredConstructor
(CtClass[] params) Returns a constructor receiving the specified parameters.Gets all the constructors declared in the class.getDeclaredField
(String name) Retrieves the field with the specified name among the fields declared in the class.getDeclaredField
(String name, String desc) Retrieves the field with the specified name and type among the fields declared in the class.CtField[]
Gets all the fields declared in the class.getDeclaredMethod
(String name) Retrieves the method with the specified name among the methods declared in the class.getDeclaredMethod
(String name, CtClass[] params) Retrieves the method with the specified name and parameter types among the methods declared in the class.CtMethod[]
Gets all methods declared in the class.CtMethod[]
getDeclaredMethods
(String name) Retrieves methods with the specified name among the methods declared in the class.If this class is a member class or interface of another class, then the class enclosing this class is returned.Returns the immediately enclosing method of this class.final CtMethod
Deprecated.The enclosing method might be a constructor.Returns the field with the specified name.Returns the field with the specified name and type.CtField[]
Returns an array containingCtField
objects representing all the non-private fields of the class.Returns the generic signature of the class.CtClass[]
Obtains the class objects representing the interfaces implemented by the class or, if this object represents an interface, the interfaces extended by that interface.Returns the method with the given name and signature.CtMethod[]
Returns an array containingCtMethod
objects representing all the non-private methods of the class.int
Returns the modifiers for this class, encoded in an integer.getName()
Obtains the fully-qualified name of the class.CtClass[]
Returns an array of nested classes declared in the class.final String
Obtains the package name.Returns a collection of the names of all the classes referenced in this class.final String
Obtains the not-qualified class name.Obtains the class object representing the superclass of the class.getURL()
Returns the uniform resource locator (URL) of the class file.boolean
hasAnnotation
(Class<?> annotationType) Returns true if the class has the specified annotation type.boolean
hasAnnotation
(String annotationTypeName) Returns true if the class has the specified annotation type.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
Determines whether this object represents an annotation type.boolean
isArray()
Returnstrue
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
Determines whether this object represents a class or an interface.boolean
isKotlin()
Returnstrue
if this object represents a Kotlin class.boolean
Returns true if the definition of the class has been modified.boolean
Returnstrue
if this object represents a primitive Java type: boolean, byte, char, short, int, long, float, double, or void.static void
Prints the version number and the copyright notice.Makes an empty class initializer (static constructor).makeNestedClass
(String name, boolean isStatic) Makes a new public nested class.makeUniqueName
(String prefix) Makes a unique member name.void
prune()
Discards unnecessary attributes, in particular,CodeAttribute
s (method bodies) of the class, to minimize the memory footprint.void
If this method is called, the class file will be rebuilt when it is finally generated bytoBytecode()
andwriteFile()
.void
Removes a constructor declared in this class.void
Removes a field declared in this class.void
Removes a method declared in this class.void
replaceClassName
(String oldName, String newName) SubstitutesnewName
for all occurrences of a class nameoldName
in the class file.void
replaceClassName
(ClassMap map) Changes class names appearing in the class file according to the givenmap
.void
setAttribute
(String name, byte[] data) Adds a named attribute.void
Sets the generic signature of the class.void
setInterfaces
(CtClass[] list) Sets implemented interfaces.void
setModifiers
(int mod) Sets the modifiers.void
Sets the class namevoid
setSuperclass
(CtClass clazz) Changes a super class unless this object represents an interface.boolean
stopPruning
(boolean stop) Disallows (or allows) automatically pruning thisCtClass
object.boolean
subclassOf
(CtClass superclass) Determines whether the class directly or indirectly extends the given class.boolean
Returnstrue
if this class extends or implementsclazz
.byte[]
Converts this class to a class file.void
Converts this class to a class file.Class<?>
toClass()
Converts this class to ajava.lang.Class
object.Class<?>
Converts this class to ajava.lang.Class
object.final Class<?>
toClass
(ClassLoader loader) Deprecated.Replaced bytoClass(ClassLoader,ProtectionDomain)
Class<?>
toClass
(ClassLoader loader, ProtectionDomain domain) Converts this class to ajava.lang.Class
object.Class<?>
toClass
(MethodHandles.Lookup lookup) Converts this class to ajava.lang.Class
object.toString()
Converts the object to a string.void
Writes a class file represented by thisCtClass
object in the current directory.void
Writes a class file represented by thisCtClass
object on a local disk.
-
Field Details
-
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. For example, if the value is"./debug"
, then all class files are saved there. The directory name must not end with a directory separator such as/
.The default value is null.
- Since:
- 3.16
- See Also:
-
version
The version number of this release.- See Also:
-
booleanType
TheCtClass
object representing theboolean
type. -
charType
TheCtClass
object representing thechar
type. -
byteType
TheCtClass
object representing thebyte
type. -
shortType
TheCtClass
object representing theshort
type. -
intType
TheCtClass
object representing theint
type. -
longType
TheCtClass
object representing thelong
type. -
floatType
TheCtClass
object representing thefloat
type. -
doubleType
TheCtClass
object representing thedouble
type. -
voidType
TheCtClass
object representing thevoid
type.
-
-
Method Details
-
main
Prints the version number and the copyright notice.The following command invokes this method:
java -jar javassist.jar
-
toString
Converts the object to a string. -
getClassPool
Returns aClassPool
for this class. -
getClassFile
Returns a class file for this class.This method is not available if
isFrozen()
is true. -
getClassFile2
Returns a class file for this class (read only). Normal applications do not need calling this method. UsegetClassFile()
.The
ClassFile
object obtained by this method is read only. Changes to this object might not be reflected on a class file generated bytoBytecode()
,toClass()
, etc.This method is available even if
isFrozen()
is true. However, if the class is frozen, it might be also pruned.- See Also:
-
getAccessorMaker
public javassist.compiler.AccessorMaker getAccessorMaker()Undocumented method. Do not use; internal-use only. -
getURL
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. -
freeze
public void freeze()Makes the class frozen.- Since:
- 3.6
- See Also:
-
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:
-
isPrimitive
public boolean isPrimitive()Returnstrue
if this object represents a primitive Java type: boolean, byte, char, short, int, long, float, double, or void. -
isArray
public boolean isArray()Returnstrue
if this object represents an array type. -
isKotlin
public boolean isKotlin()Returnstrue
if this object represents a Kotlin class.- Since:
- 3.26
-
getComponentType
If this object represents an array, this method returns the component type of the array. Otherwise, it returnsnull
.- Throws:
NotFoundException
-
subtypeOf
Returnstrue
if this class extends or implementsclazz
. It also returnstrue
if this class is the same asclazz
.- Throws:
NotFoundException
-
getName
Obtains the fully-qualified name of the class. -
getSimpleName
Obtains the not-qualified class name. -
getPackageName
Obtains the package name. It may benull
. -
setName
Sets the class name- Parameters:
name
- fully-qualified name
-
getGenericSignature
Returns the generic signature of the class.The generics of Java is implemented by the erasure technique. After compilation, all type parameters are dropped off from the main part of a class file. However, for reflection, the type parameters are encoded into generic signatures and attached to a class file.
- Returns:
- null if the generic signature is not included.
- Since:
- 3.17
- See Also:
-
setGenericSignature
Sets the generic signature of the class.The generics of Java is implemented by the erasure technique. After compilation, all type parameters are dropped off from the main part of a class file. However, for reflection, the type parameters must be encoded into generic signatures and attached to a class file.
For example,
class List<T> { T value; T get() { return value; } void set(T v) { value = v; } }
this class is generated by the following code:
ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.makeClass("List"); CtClass objectClass = pool.get(CtClass.javaLangObject); ClassSignature cs = new ClassSignature( new TypeParameter[] { new TypeParameter("T") }); cc.setGenericSignature(cs.encode()); // <T:Ljava/lang/Object;>Ljava/lang/Object; CtField f = new CtField(objClass, "value", cc); TypeVariable tvar = new TypeVariable("T"); f.setGenericSignature(tvar.encode()); // TT; cc.addField(f); CtMethod m = CtNewMethod.make("public Object get(){return value;}", cc); MethodSignature ms = new MethodSignature(null, null, tvar, null); m.setGenericSignature(ms.encode()); // ()TT; cc.addMethod(m); CtMethod m2 = CtNewMethod.make("public void set(Object v){value = v;}", cc); MethodSignature ms2 = new MethodSignature(null, new Type[] { tvar }, new BaseType("void"), null); m2.setGenericSignature(ms2.encode()); // (TT;)V; cc.addMethod(m2); cc.writeFile();
The generated class file is equivalent to the following:
class List { Object value; Object get() { return value; } void set(Object v) { value = v; } }
but it includes generic signatures for the class, the field, and the methods so that the type variable
T
can be accessible through reflection.MethodSignature
is a utility class. You can directly pass the signature string to thesetGenericSignature
method. For the specification of the signatures, see Section 4.7.9.1 Signatures of The Java Virtual Machine Specification (Java SE 8).- Parameters:
sig
- a generic signature.- Since:
- 3.17
- See Also:
-
replaceClassName
SubstitutesnewName
for all occurrences of a class nameoldName
in the class file.- Parameters:
oldName
- replaced class namenewName
- substituted class name
-
replaceClassName
Changes class names appearing in the class file according to the givenmap
.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 ofClassMap
so thatget()
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
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
.- Returns:
- a
Collection<String>
object.
-
isInterface
public boolean isInterface()Determines whether this object represents a class or an interface. It returnstrue
if this object represents an interface. -
isAnnotation
public boolean isAnnotation()Determines whether this object represents an annotation type. It returnstrue
if this object represents an annotation type.- Since:
- 3.2
-
isEnum
public boolean isEnum()Determines whether this object represents an enum. It returnstrue
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, usejavassist.Modifier
.If the class is a static nested class (a.k.a. static inner class), the returned modifiers include
Modifier.STATIC
.- See Also:
-
hasAnnotation
Returns true if the class has the specified annotation type.- Parameters:
annotationType
- the annotation type.- Returns:
true
if the annotation is found, otherwisefalse
.- Since:
- 3.11
-
hasAnnotation
Returns true if the class has the specified annotation type.- Parameters:
annotationTypeName
- the name of annotation type.- Returns:
true
if the annotation is found, otherwisefalse
.- Since:
- 3.21
-
getAnnotation
Returns the annotation if the class has the specified annotation type. For example, if an annotation@Author
is associated with this class, anAuthor
object is returned. The member values can be obtained by calling methods on theAuthor
object.- Parameters:
clz
- the annotation type.- Returns:
- the annotation if found, otherwise
null
. - Throws:
ClassNotFoundException
- Since:
- 3.11
-
getAnnotations
Returns the annotations associated with this class. For example, if an annotation@Author
is associated with this class, the returned array contains anAuthor
object. The member values can be obtained by calling methods on theAuthor
object.- Returns:
- an array of annotation-type objects.
- Throws:
ClassNotFoundException
- Since:
- 3.1
- See Also:
-
getAvailableAnnotations
Returns the annotations associated with this class. This method is equivalent togetAnnotations()
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:
-
getDeclaredClasses
Returns an array of nested classes declared in the class. Nested classes are inner classes, anonymous classes, local classes, and static nested classes. This simply callsgetNestedClasses()
.- Throws:
NotFoundException
- Since:
- 3.15
- See Also:
-
getNestedClasses
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 byjavassist.Modifier
- See Also:
-
subclassOf
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
Obtains the class object representing the superclass of the class. It returns null if this object represents thejava.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, callgetInterfaces()
.- Throws:
NotFoundException
-
setSuperclass
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 appendsclazz
to the list of the super interfaces extended by that interface. Note that an interface can extend multiple super interfaces.- Throws:
CannotCompileException
- See Also:
-
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.- Throws:
NotFoundException
-
setInterfaces
Sets implemented interfaces. If this object represents an interface, this method sets the interfaces extended by that interface.- Parameters:
list
- a list of theCtClass
objects representing interfaces, ornull
if the class implements no interfaces.
-
addInterface
Adds an interface.- Parameters:
anInterface
- the added interface.
-
getDeclaringClass
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
Deprecated.The enclosing method might be a constructor. UsegetEnclosingBehavior()
.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
- See Also:
-
getEnclosingBehavior
Returns the immediately enclosing method of this class. It might be not a method but a constructor. 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
Makes a new public nested class. If this method is called, theCtClass
, 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
Returns an array containingCtField
objects representing all the non-private fields of the class. That array includes non-private fields inherited from the superclasses. -
getField
Returns the field with the specified name. The returned field may be a private field declared in a super class or interface.- Throws:
NotFoundException
-
getField
Returns the field with the specified name and type. The returned field may be a private field declared in a super class or interface. Unlike Java, the JVM allows a class to have multiple fields with the same name but different types.- Parameters:
name
- the field name.desc
- the type descriptor of the field. It is available byCtField.getSignature()
.- Throws:
NotFoundException
- See Also:
-
getDeclaredFields
Gets all the fields declared in the class. The inherited fields are not included.Note: the result does not include inherited fields.
-
getDeclaredField
Retrieves the field with the specified name among the fields declared in the class.Note: this method does not search the super classes.
- Throws:
NotFoundException
-
getDeclaredField
Retrieves the field with the specified name and type among the fields declared in the class. Unlike Java, the JVM allows a class to have multiple fields with the same name but different types.Note: this method does not search the super classes.
- Parameters:
name
- the field name.desc
- the type descriptor of the field. It is available byCtField.getSignature()
.- Throws:
NotFoundException
- See Also:
-
getDeclaredBehaviors
Gets all the constructors and methods declared in the class. -
getConstructors
Returns an array containingCtConstructor
objects representing all the non-private constructors of the class. -
getConstructor
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 orjavassist.bytecode.Descriptor
.- Parameters:
desc
- method descriptor- Throws:
NotFoundException
- See Also:
-
getDeclaredConstructors
Gets all the constructors declared in the class.- See Also:
-
getDeclaredConstructor
Returns a constructor receiving the specified parameters.- Parameters:
params
- parameter types.- Throws:
NotFoundException
-
getClassInitializer
Gets the class initializer (static constructor) declared in the class. This method returnsnull
if no class initializer is not declared.- See Also:
-
getMethods
Returns an array containingCtMethod
objects representing all the non-private methods of the class. That array includes non-private methods inherited from the superclasses. -
getMethod
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 namedesc
- method descriptor- Throws:
NotFoundException
- See Also:
-
getDeclaredMethods
Gets all methods declared in the class. The inherited methods are not included.- See Also:
-
getDeclaredMethod
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 nameparams
- parameter types- Throws:
NotFoundException
- See Also:
-
getDeclaredMethods
Retrieves methods with the specified name among the methods declared in the class. Multiple methods with different parameters may be returned.Note: this method does not search the superclasses.
- Parameters:
name
- method name.- Throws:
NotFoundException
- Since:
- 3.19
-
getDeclaredMethod
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:
-
makeClassInitializer
Makes an empty class initializer (static constructor). If the class already includes a class initializer, this method returns it.- Throws:
CannotCompileException
- See Also:
-
addConstructor
Adds a constructor. To add a class initializer (static constructor), callmakeClassInitializer()
.- Throws:
CannotCompileException
- See Also:
-
removeConstructor
Removes a constructor declared in this class.- Parameters:
c
- removed constructor.- Throws:
NotFoundException
- if the constructor is not found.
-
addMethod
Adds a method.- Throws:
CannotCompileException
-
removeMethod
Removes a method declared in this class.- Parameters:
m
- removed method.- Throws:
NotFoundException
- if the method is not found.
-
addField
Adds a field.The
CtField
belonging to anotherCtClass
cannot be directly added to this class. Only a field created for this class can be added.- Throws:
CannotCompileException
- See Also:
-
addField
Adds a field with an initial value.The
CtField
belonging to anotherCtClass
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
isCtClass
. The type off
isCtField
.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 byaddField()
.- Parameters:
init
- an expression for the initial value.- Throws:
CannotCompileException
- See Also:
-
addField
Adds a field with an initial value.The
CtField
belonging to anotherCtClass
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.- Throws:
CannotCompileException
- See Also:
-
removeField
Removes a field declared in this class.- Parameters:
f
- removed field.- Throws:
NotFoundException
- if the field is not found.
-
getAttribute
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.getClassFile().getAttributes()
- Parameters:
name
- attribute name- See Also:
-
setAttribute
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 attributeinfo
to a class file.getClassFile().addAttribute(info)
- Parameters:
name
- attribute namedata
- attribute value- See Also:
-
instrument
Applies the given converter to all methods and constructors declared in the class. This method callsinstrument()
on everyCtMethod
andCtConstructor
object in the class.- Parameters:
converter
- specifies how to modify.- Throws:
CannotCompileException
-
instrument
Modifies the bodies of all methods and constructors declared in the class. This method callsinstrument()
on everyCtMethod
andCtConstructor
object in the class.- Parameters:
editor
- specifies how to modify.- Throws:
CannotCompileException
-
toClass
Converts this class to ajava.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.Warning: In Java 11 or later, the call to this method will print a warning message:
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by javassist.util.proxy.SecurityActions$3 ... WARNING: Please consider reporting this to the maintainers of javassist.util.proxy.SecurityActions$3 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
To avoid this message, use
toClass(Class)
ortoClass(java.lang.invoke.MethodHandles.Lookup)
.toClass()
will be unavailable in a future release.Warning: 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.
Note: this method calls
toClass()
inClassPool
. -
toClass
Converts this class to ajava.lang.Class
object. Once this method is called, further modifications are not allowed any more.This method is provided for convenience. You should use
toClass(Lookup)
for better compatibility with the module system.Note: this method calls
toClass()
inClassPool
.Warning: 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.
- Parameters:
neighbor
- A class belonging to the same package that this class belongs to. It is used to load the class.- Throws:
CannotCompileException
- Since:
- 3.24
- See Also:
-
toClass
Converts this class to ajava.lang.Class
object. Once this method is called, further modifications are not allowed any more.This method is provided for convenience. If you need more complex functionality, you should write your own class loader.
Note: this method calls
toClass()
inClassPool
.Warning: 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.
- Parameters:
lookup
- used when loading the class. It has to have an access right to define a new class.- Throws:
CannotCompileException
- Since:
- 3.24
- See Also:
-
toClass
Converts this class to ajava.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 ajava.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 callgetProtectionDomain()
injava.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()
inClassPool
.- Parameters:
loader
- the class loader used to load this class. If it is null, the class loader returned byClassPool.getClassLoader()
is used.domain
- the protection domain that the class belongs to. If it is null, the default domain created byjava.lang.ClassLoader
is used.- Throws:
CannotCompileException
- Since:
- 3.3
- See Also:
-
toClass
Deprecated.Replaced bytoClass(ClassLoader,ProtectionDomain)
Converts this class to ajava.lang.Class
object.Warning: 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 thisCtClass
object from theClassPool
. After this method is called, any method cannot be called on the removedCtClass
object.If
get()
inClassPool
is called with the name of the removed method, theClassPool
will read the class file again and constructs anotherCtClass
object representing the same class. -
stopPruning
public boolean stopPruning(boolean stop) Disallows (or allows) automatically pruning thisCtClass
object.Javassist can automatically prune a
CtClass
object whentoBytecode()
(ortoClass()
,writeFile()
) is called. Since aClassPool
holds all instances ofCtClass
even aftertoBytecode()
(ortoClass()
,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 ofClassPool.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:
-
prune
public void prune()Discards unnecessary attributes, in particular,CodeAttribute
s (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()
, orinstrument()
cannot be called. However, the method names and signatures in the class etc. are still accessible.toBytecode()
,writeFile()
, andtoClass()
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.
-
rebuildClassFile
public void rebuildClassFile()If this method is called, the class file will be rebuilt when it is finally generated bytoBytecode()
andwriteFile()
. 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
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:
IOException
CannotCompileException
-
writeFile
Writes a class file represented by thisCtClass
object in the current directory. Once this method is called, further modifications are not possible any more.- Throws:
NotFoundException
IOException
CannotCompileException
- See Also:
-
writeFile
Writes a class file represented by thisCtClass
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
IOException
- See Also:
-
debugWriteFile
public void debugWriteFile()Writes a class file aswriteFile()
does although this method does not prune or freeze the class after writing the class file. Note that, oncewriteFile()
ortoBytecode()
is called, it cannot be called again since the class is pruned and frozen. This method would be useful for debugging. -
debugWriteFile
Writes a class file aswriteFile()
does although this method does not prune or freeze the class after writing the class file. Note that, oncewriteFile()
ortoBytecode()
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
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
IOException
-
makeUniqueName
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.
-