Class CtClassType
- Direct Known Subclasses:
CtNewClass
-
Nested Class Summary
Nested classes/interfaces inherited from class javassist.CtClass
CtClass.DelayedFileOutputStream
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AccessorMaker
(package private) ClassFile
(package private) ClassPool
private boolean
private FieldInitLink
(package private) boolean
private static final int
private int
private Reference
<CtMember.Cache> (package private) byte[]
private int
(package private) boolean
private boolean
(package private) boolean
Fields inherited from class javassist.CtClass
booleanType, byteType, charType, debugDump, doubleType, floatType, intType, javaLangObject, longType, primitiveTypes, qualifiedName, shortType, version, voidType
-
Constructor Summary
ConstructorsConstructorDescriptionCtClassType
(InputStream ins, ClassPool cp) CtClassType
(String name, ClassPool cp) CtClassType
(ClassFile cf, ClassPool cp) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a constructor.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.private CtField
checkGetField
(CtField f, String name, String desc) (package private) void
private void
checkPruned
(String method) (package private) void
compress()
Invoked from ClassPool#compress().void
defrost()
Defrosts the class so that the class can be modified again.private void
protected void
extendToString
(StringBuilder buffer) Implemented in subclasses to add to theCtClass.toString()
result.private void
exToString
(StringBuilder buffer, String msg, CtMember head, CtMember tail) void
freeze()
Makes the class frozen.Undocumented method.getAnnotation
(Class<?> clz) Returns the annotation if the class has the specified annotation type.Object[]
Returns the annotations associated with this class.private Object[]
getAnnotations
(boolean ignoreNotFound) (package private) static Object
getAnnotationType
(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) 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 (read only).getClassFile3
(boolean doCompress) Gets the class initializer (static constructor) declared in the class.Returns aClassPool
for this class.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.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.private CtField
getDeclaredField2
(String name, String desc) 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.Returns the field with the specified name and type.(package private) CtField
CtField[]
Returns an array containingCtField
objects representing all the non-private fields of the class.private static void
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.protected CtMember.Cache
Returns the method with the given name and signature.private static CtMethod
getMethod0
(CtClass cc, String name, String desc) CtMethod[]
Returns an array containingCtMethod
objects representing all the non-private methods of the class.private static void
getMethods0
(Map<String, CtMember> h, CtClass cc) int
Returns the modifiers for this class, encoded in an integer.CtClass[]
Returns an array of nested classes declared in the class.Obtains the class object representing the superclass of the class.(package private) int
getURL()
Returns the uniform resource locator (URL) of the class file.boolean
hasAnnotation
(String annotationName) Returns true if the class has the specified annotation type.(package private) static boolean
hasAnnotationType
(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) Deprecated.(package private) static boolean
hasAnnotationType
(String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) protected CtMember.Cache
Returns null if members are not cached.(package private) final void
private static void
insertAuxInitializer
(CodeAttribute codeAttr, Bytecode initializer, int stacksize) 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
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
Returns true if the definition of the class has been modified.private static boolean
isPubCons
(CtConstructor cons) private void
makeBehaviorCache
(CtMember.Cache cache) Makes an empty class initializer (static constructor).private void
makeFieldCache
(CtMember.Cache cache) private int
makeFieldInitializer
(Bytecode code, CtClass[] parameters) private void
makeMemberList
(Map<Object, CtClassType> table) makeNestedClass
(String name, boolean isStatic) Makes a new public nested class.makeUniqueName
(String prefix) Makes a unique member name.private void
private void
modifyClassConstructor
(ClassFile cf, Bytecode code, int stacksize, int localsize) private void
private void
private static boolean
notFindInArray
(String prefix, String[] values) 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()
.private void
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 classnames) Changes class names appearing in the class file according to the givenmap
.private void
Converts a ClassFile object into a byte array for saving memory space.void
setAttribute
(String name, byte[] data) Adds a named attribute.private ClassFile
Updatesclassfile
if it is null.(package private) void
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
.(package private) static Object[]
toAnnotationType
(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) (package private) static Object[][]
toAnnotationType
(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) private static Object
toAnnoType
(Annotation anno, ClassPool cp) void
Converts this class to a class file.private static void
updateInnerEntry
(int newMod, String name, CtClass clazz, boolean outer) Methods inherited from class javassist.CtClass
addField, debugWriteFile, debugWriteFile, detach, getClassFile, getComponentType, getDeclaredClasses, getDeclaredConstructor, getEnclosingMethod, getField, getName, getPackageName, getRefClasses, getSimpleName, hasAnnotation, isArray, isKotlin, isPrimitive, main, makeFileOutput, toBytecode, toClass, toClass, toClass, toClass, toClass, toString, writeFile, writeFile
-
Field Details
-
classPool
ClassPool classPool -
wasChanged
boolean wasChanged -
wasFrozen
private boolean wasFrozen -
wasPruned
boolean wasPruned -
gcConstPool
boolean gcConstPool -
classfile
ClassFile classfile -
rawClassfile
byte[] rawClassfile -
memberCache
-
accessors
-
fieldInitializers
-
uniqueNumberSeed
private int uniqueNumberSeed -
doPruning
private boolean doPruning -
getCount
private int getCount -
GET_THRESHOLD
private static final int GET_THRESHOLD- See Also:
-
-
Constructor Details
-
CtClassType
-
CtClassType
CtClassType(InputStream ins, ClassPool cp) throws IOException - Throws:
IOException
-
CtClassType
-
-
Method Details
-
extendToString
Description copied from class:CtClass
Implemented in subclasses to add to theCtClass.toString()
result. Subclasses should put a space before each token added to the buffer.- Overrides:
extendToString
in classCtClass
-
exToString
-
getAccessorMaker
Description copied from class:CtClass
Undocumented method. Do not use; internal-use only.- Overrides:
getAccessorMaker
in classCtClass
-
getClassFile2
Description copied from class:CtClass
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.- Overrides:
getClassFile2
in classCtClass
- See Also:
-
getClassFile3
-
incGetCounter
final void incGetCounter()- Overrides:
incGetCounter
in classCtClass
-
compress
void compress()Invoked from ClassPool#compress(). It releases the class files that have not been recently used if they are unmodified. -
saveClassFile
private void saveClassFile()Converts a ClassFile object into a byte array for saving memory space. -
removeClassFile
private void removeClassFile() -
setClassFile
Updatesclassfile
if it is null. -
getClassPool
Description copied from class:CtClass
Returns aClassPool
for this class.- Overrides:
getClassPool
in classCtClass
-
setClassPool
-
getURL
Description copied from class:CtClass
Returns the uniform resource locator (URL) of the class file.- Overrides:
getURL
in classCtClass
- Throws:
NotFoundException
-
isModified
public boolean isModified()Description copied from class:CtClass
Returns true if the definition of the class has been modified.- Overrides:
isModified
in classCtClass
-
isFrozen
public boolean isFrozen()Description copied from class:CtClass
Returns true if the class has been loaded or written out and thus it cannot be modified any more. -
freeze
public void freeze()Description copied from class:CtClass
Makes the class frozen. -
checkModify
- Overrides:
checkModify
in classCtClass
- Throws:
RuntimeException
-
defrost
public void defrost()Description copied from class:CtClass
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. -
subtypeOf
Description copied from class:CtClass
Returnstrue
if this class extends or implementsclazz
. It also returnstrue
if this class is the same asclazz
.- Overrides:
subtypeOf
in classCtClass
- Throws:
NotFoundException
-
setName
Description copied from class:CtClass
Sets the class name- Overrides:
setName
in classCtClass
- Parameters:
name
- fully-qualified name- Throws:
RuntimeException
-
getGenericSignature
Description copied from class:CtClass
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.
- Overrides:
getGenericSignature
in classCtClass
- Returns:
- null if the generic signature is not included.
- See Also:
-
setGenericSignature
Description copied from class:CtClass
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).- Overrides:
setGenericSignature
in classCtClass
- Parameters:
sig
- a generic signature.- See Also:
-
replaceClassName
Description copied from class:CtClass
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.- Overrides:
replaceClassName
in classCtClass
- Parameters:
classnames
- the hashtable associating replaced class names with substituted names.- Throws:
RuntimeException
-
replaceClassName
Description copied from class:CtClass
SubstitutesnewName
for all occurrences of a class nameoldName
in the class file.- Overrides:
replaceClassName
in classCtClass
- Parameters:
oldname
- replaced class namenewname
- substituted class name- Throws:
RuntimeException
-
isInterface
public boolean isInterface()Description copied from class:CtClass
Determines whether this object represents a class or an interface. It returnstrue
if this object represents an interface.- Overrides:
isInterface
in classCtClass
-
isAnnotation
public boolean isAnnotation()Description copied from class:CtClass
Determines whether this object represents an annotation type. It returnstrue
if this object represents an annotation type.- Overrides:
isAnnotation
in classCtClass
-
isEnum
public boolean isEnum()Description copied from class:CtClass
Determines whether this object represents an enum. It returnstrue
if this object represents an enum. -
getModifiers
public int getModifiers()Description copied from class:CtClass
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
.- Overrides:
getModifiers
in classCtClass
- See Also:
-
getNestedClasses
Description copied from class:CtClass
Returns an array of nested classes declared in the class. Nested classes are inner classes, anonymous classes, local classes, and static nested classes.- Overrides:
getNestedClasses
in classCtClass
- Throws:
NotFoundException
-
setModifiers
public void setModifiers(int mod) Description copied from class:CtClass
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).
- Overrides:
setModifiers
in classCtClass
- Parameters:
mod
- modifiers encoded byjavassist.Modifier
- See Also:
-
updateInnerEntry
-
hasAnnotation
Description copied from class:CtClass
Returns true if the class has the specified annotation type.- Overrides:
hasAnnotation
in classCtClass
- Parameters:
annotationName
- the name of annotation type.- Returns:
true
if the annotation is found, otherwisefalse
.
-
hasAnnotationType
@Deprecated static boolean hasAnnotationType(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) Deprecated. -
hasAnnotationType
static boolean hasAnnotationType(String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) -
getAnnotation
Description copied from class:CtClass
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.- Overrides:
getAnnotation
in classCtClass
- Parameters:
clz
- the annotation type.- Returns:
- the annotation if found, otherwise
null
. - Throws:
ClassNotFoundException
-
getAnnotationType
static Object getAnnotationType(Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
getAnnotations
Description copied from class:CtClass
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.- Overrides:
getAnnotations
in classCtClass
- Returns:
- an array of annotation-type objects.
- Throws:
ClassNotFoundException
- See Also:
-
getAvailableAnnotations
Description copied from class:CtClass
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.- Overrides:
getAvailableAnnotations
in classCtClass
- Returns:
- an array of annotation-type objects.
- See Also:
-
getAnnotations
- Throws:
ClassNotFoundException
-
toAnnotationType
static Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
toAnnotationType
static Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
toAnnoType
- Throws:
ClassNotFoundException
-
subclassOf
Description copied from class:CtClass
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.
- Overrides:
subclassOf
in classCtClass
-
getSuperclass
Description copied from class:CtClass
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()
.- Overrides:
getSuperclass
in classCtClass
- Throws:
NotFoundException
-
setSuperclass
Description copied from class:CtClass
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.- Overrides:
setSuperclass
in classCtClass
- Throws:
CannotCompileException
- See Also:
-
getInterfaces
Description copied from 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.- Overrides:
getInterfaces
in classCtClass
- Throws:
NotFoundException
-
setInterfaces
Description copied from class:CtClass
Sets implemented interfaces. If this object represents an interface, this method sets the interfaces extended by that interface.- Overrides:
setInterfaces
in classCtClass
- Parameters:
list
- a list of theCtClass
objects representing interfaces, ornull
if the class implements no interfaces.
-
addInterface
Description copied from class:CtClass
Adds an interface.- Overrides:
addInterface
in classCtClass
- Parameters:
anInterface
- the added interface.
-
getDeclaringClass
Description copied from class:CtClass
If this class is a member class or interface of another class, then the class enclosing this class is returned.- Overrides:
getDeclaringClass
in classCtClass
- Returns:
- null if this class is a top-level class or an anonymous class.
- Throws:
NotFoundException
-
getEnclosingBehavior
Description copied from class:CtClass
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.- Overrides:
getEnclosingBehavior
in classCtClass
- Returns:
- null if this class is not a local class or an anonymous class.
- Throws:
NotFoundException
-
makeNestedClass
Description copied from class:CtClass
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.- Overrides:
makeNestedClass
in classCtClass
- Parameters:
name
- the simple name of the nested class.isStatic
- true if the nested class is static.
-
nameReplaced
private void nameReplaced() -
hasMemberCache
Returns null if members are not cached. -
getMembers
-
makeFieldCache
-
makeBehaviorCache
-
getFields
Description copied from class:CtClass
Returns an array containingCtField
objects representing all the non-private fields of the class. That array includes non-private fields inherited from the superclasses. -
getFields
-
getField
Description copied from class:CtClass
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.- Overrides:
getField
in classCtClass
- Parameters:
name
- the field name.desc
- the type descriptor of the field. It is available byCtField.getSignature()
.- Throws:
NotFoundException
- See Also:
-
checkGetField
- Throws:
NotFoundException
-
getField2
-
getDeclaredFields
Description copied from class:CtClass
Gets all the fields declared in the class. The inherited fields are not included.Note: the result does not include inherited fields.
- Overrides:
getDeclaredFields
in classCtClass
-
getDeclaredField
Description copied from class:CtClass
Retrieves the field with the specified name among the fields declared in the class.Note: this method does not search the super classes.
- Overrides:
getDeclaredField
in classCtClass
- Throws:
NotFoundException
-
getDeclaredField
Description copied from class:CtClass
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.
- Overrides:
getDeclaredField
in classCtClass
- Parameters:
name
- the field name.desc
- the type descriptor of the field. It is available byCtField.getSignature()
.- Throws:
NotFoundException
- See Also:
-
getDeclaredField2
-
getDeclaredBehaviors
Description copied from class:CtClass
Gets all the constructors and methods declared in the class.- Overrides:
getDeclaredBehaviors
in classCtClass
-
getConstructors
Description copied from class:CtClass
Returns an array containingCtConstructor
objects representing all the non-private constructors of the class.- Overrides:
getConstructors
in classCtClass
-
isPubCons
-
getConstructor
Description copied from class:CtClass
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
.- Overrides:
getConstructor
in classCtClass
- Parameters:
desc
- method descriptor- Throws:
NotFoundException
- See Also:
-
getDeclaredConstructors
Description copied from class:CtClass
Gets all the constructors declared in the class.- Overrides:
getDeclaredConstructors
in classCtClass
- See Also:
-
getClassInitializer
Description copied from class:CtClass
Gets the class initializer (static constructor) declared in the class. This method returnsnull
if no class initializer is not declared.- Overrides:
getClassInitializer
in classCtClass
- See Also:
-
getMethods
Description copied from class:CtClass
Returns an array containingCtMethod
objects representing all the non-private methods of the class. That array includes non-private methods inherited from the superclasses.- Overrides:
getMethods
in classCtClass
-
getMethods0
-
getMethod
Description copied from class:CtClass
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.- Overrides:
getMethod
in classCtClass
- Parameters:
name
- method namedesc
- method descriptor- Throws:
NotFoundException
- See Also:
-
getMethod0
-
getDeclaredMethods
Description copied from class:CtClass
Gets all methods declared in the class. The inherited methods are not included.- Overrides:
getDeclaredMethods
in classCtClass
- See Also:
-
getDeclaredMethods
Description copied from class:CtClass
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.
- Overrides:
getDeclaredMethods
in classCtClass
- Parameters:
name
- method name.- Throws:
NotFoundException
-
getDeclaredMethod
Description copied from class:CtClass
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.
- Overrides:
getDeclaredMethod
in classCtClass
- Throws:
NotFoundException
- See Also:
-
getDeclaredMethod
Description copied from class:CtClass
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.
- Overrides:
getDeclaredMethod
in classCtClass
- Parameters:
name
- method nameparams
- parameter types- Throws:
NotFoundException
- See Also:
-
addField
Description copied from class:CtClass
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()
.- Overrides:
addField
in classCtClass
- Parameters:
init
- an expression for the initial value.- Throws:
CannotCompileException
- See Also:
-
addField
Description copied from class:CtClass
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.- Overrides:
addField
in classCtClass
- Parameters:
init
- specifies the initial value of the field.- Throws:
CannotCompileException
- See Also:
-
removeField
Description copied from class:CtClass
Removes a field declared in this class.- Overrides:
removeField
in classCtClass
- Parameters:
f
- removed field.- Throws:
NotFoundException
- if the field is not found.
-
makeClassInitializer
Description copied from class:CtClass
Makes an empty class initializer (static constructor). If the class already includes a class initializer, this method returns it.- Overrides:
makeClassInitializer
in classCtClass
- Throws:
CannotCompileException
- See Also:
-
addConstructor
Description copied from class:CtClass
Adds a constructor. To add a class initializer (static constructor), callmakeClassInitializer()
.- Overrides:
addConstructor
in classCtClass
- Throws:
CannotCompileException
- See Also:
-
removeConstructor
Description copied from class:CtClass
Removes a constructor declared in this class.- Overrides:
removeConstructor
in classCtClass
- Parameters:
m
- removed constructor.- Throws:
NotFoundException
- if the constructor is not found.
-
addMethod
Description copied from class:CtClass
Adds a method.- Overrides:
addMethod
in classCtClass
- Throws:
CannotCompileException
-
removeMethod
Description copied from class:CtClass
Removes a method declared in this class.- Overrides:
removeMethod
in classCtClass
- Parameters:
m
- removed method.- Throws:
NotFoundException
- if the method is not found.
-
getAttribute
Description copied from class:CtClass
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()
- Overrides:
getAttribute
in classCtClass
- Parameters:
name
- attribute name- See Also:
-
setAttribute
Description copied from class:CtClass
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)
- Overrides:
setAttribute
in classCtClass
- Parameters:
name
- attribute namedata
- attribute value- See Also:
-
instrument
Description copied from class:CtClass
Applies the given converter to all methods and constructors declared in the class. This method callsinstrument()
on everyCtMethod
andCtConstructor
object in the class.- Overrides:
instrument
in classCtClass
- Parameters:
converter
- specifies how to modify.- Throws:
CannotCompileException
-
instrument
Description copied from class:CtClass
Modifies the bodies of all methods and constructors declared in the class. This method callsinstrument()
on everyCtMethod
andCtConstructor
object in the class.- Overrides:
instrument
in classCtClass
- Parameters:
editor
- specifies how to modify.- Throws:
CannotCompileException
-
prune
public void prune()Description copied from class:CtClass
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()Description copied from class:CtClass
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.- Overrides:
rebuildClassFile
in classCtClass
-
toBytecode
Description copied from class:CtClass
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.
- Overrides:
toBytecode
in classCtClass
- Parameters:
out
- the output stream that a class file is written to.- Throws:
CannotCompileException
IOException
-
dumpClassFile
- Throws:
IOException
-
checkPruned
-
stopPruning
public boolean stopPruning(boolean stop) Description copied from class:CtClass
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.- Overrides:
stopPruning
in classCtClass
- Parameters:
stop
- disallow pruning if true. Otherwise, allow.- Returns:
- the previous status of pruning. true if pruning is already stopped.
- See Also:
-
modifyClassConstructor
-
modifyClassConstructor
private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException - Throws:
CannotCompileException
-
modifyConstructors
-
insertAuxInitializer
private static void insertAuxInitializer(CodeAttribute codeAttr, Bytecode initializer, int stacksize) throws BadBytecode - Throws:
BadBytecode
-
makeFieldInitializer
private int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException -
getHiddenMethods
-
getUniqueNumber
int getUniqueNumber() -
makeUniqueName
Description copied from class:CtClass
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.- Overrides:
makeUniqueName
in classCtClass
- Parameters:
prefix
- the prefix of the member name.
-
notFindInArray
-
makeMemberList
-