Class CtClassType
- java.lang.Object
-
- javassist.CtClass
-
- javassist.CtClassType
-
- Direct Known Subclasses:
CtNewClass
class CtClassType extends CtClass
Class> types.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javassist.CtClass
CtClass.DelayedFileOutputStream
-
-
Field Summary
Fields Modifier and Type Field Description private AccessorMakeraccessors(package private) ClassFileclassfile(package private) ClassPoolclassPoolprivate booleandoPruningprivate FieldInitLinkfieldInitializers(package private) booleangcConstPoolprivate static intGET_THRESHOLDprivate intgetCountprivate java.util.Map<CtMethod,java.lang.String>hiddenMethodsprivate java.lang.ref.Reference<CtMember.Cache>memberCache(package private) byte[]rawClassfileprivate intuniqueNumberSeed(package private) booleanwasChangedprivate booleanwasFrozen(package private) booleanwasPruned-
Fields inherited from class javassist.CtClass
booleanType, byteType, charType, debugDump, doubleType, floatType, intType, javaLangObject, longType, primitiveTypes, qualifiedName, shortType, version, voidType
-
-
Constructor Summary
Constructors Constructor Description CtClassType(java.io.InputStream ins, ClassPool cp)CtClassType(java.lang.String name, ClassPool cp)CtClassType(ClassFile cf, ClassPool cp)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddConstructor(CtConstructor c)Adds a constructor.voidaddField(CtField f, java.lang.String init)Adds a field with an initial value.voidaddField(CtField f, CtField.Initializer init)Adds a field with an initial value.voidaddInterface(CtClass anInterface)Adds an interface.voidaddMethod(CtMethod m)Adds a method.private CtFieldcheckGetField(CtField f, java.lang.String name, java.lang.String desc)(package private) voidcheckModify()private voidcheckPruned(java.lang.String method)(package private) voidcompress()Invoked from ClassPool#compress().voiddefrost()Defrosts the class so that the class can be modified again.private voiddumpClassFile(ClassFile cf)protected voidextendToString(java.lang.StringBuffer buffer)Implemented in subclasses to add to theCtClass.toString()result.private voidexToString(java.lang.StringBuffer buffer, java.lang.String msg, CtMember head, CtMember tail)voidfreeze()Makes the class frozen.AccessorMakergetAccessorMaker()Undocumented method.java.lang.ObjectgetAnnotation(java.lang.Class<?> clz)Returns the annotation if the class has the specified annotation type.java.lang.Object[]getAnnotations()Returns the annotations associated with this class.private java.lang.Object[]getAnnotations(boolean ignoreNotFound)(package private) static java.lang.ObjectgetAnnotationType(java.lang.Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)byte[]getAttribute(java.lang.String name)Obtains an attribute with the given name.java.lang.Object[]getAvailableAnnotations()Returns the annotations associated with this class.ClassFilegetClassFile2()Returns a class file for this class (read only).ClassFilegetClassFile3(boolean doCompress)CtConstructorgetClassInitializer()Gets the class initializer (static constructor) declared in the class.ClassPoolgetClassPool()Returns aClassPoolfor this class.CtConstructorgetConstructor(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 containingCtConstructorobjects representing all the non-private constructors of the class.CtBehavior[]getDeclaredBehaviors()Gets all the constructors and methods declared in the class.CtConstructor[]getDeclaredConstructors()Gets all the constructors declared in the class.CtFieldgetDeclaredField(java.lang.String name)Retrieves the field with the specified name among the fields declared in the class.CtFieldgetDeclaredField(java.lang.String name, java.lang.String desc)Retrieves the field with the specified name and type among the fields declared in the class.private CtFieldgetDeclaredField2(java.lang.String name, java.lang.String desc)CtField[]getDeclaredFields()Gets all the fields declared in the class.CtMethodgetDeclaredMethod(java.lang.String name)Retrieves the method with the specified name among the methods declared in the class.CtMethodgetDeclaredMethod(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.CtMethod[]getDeclaredMethods(java.lang.String name)Retrieves methods with the specified name among the methods declared in the class.CtClassgetDeclaringClass()If this class is a member class or interface of another class, then the class enclosing this class is returned.CtBehaviorgetEnclosingBehavior()Returns the immediately enclosing method of this class.CtFieldgetField(java.lang.String name, java.lang.String desc)Returns the field with the specified name and type.(package private) CtFieldgetField2(java.lang.String name, java.lang.String desc)CtField[]getFields()Returns an array containingCtFieldobjects representing all the non-private fields of the class.private static voidgetFields(java.util.List<CtMember> alist, CtClass cc)java.lang.StringgetGenericSignature()Returns the generic signature of the class.(package private) java.util.Map<CtMethod,java.lang.String>getHiddenMethods()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.protected CtMember.CachegetMembers()CtMethodgetMethod(java.lang.String name, java.lang.String desc)Returns the method with the given name and signature.private static CtMethodgetMethod0(CtClass cc, java.lang.String name, java.lang.String desc)CtMethod[]getMethods()Returns an array containingCtMethodobjects representing all the non-private methods of the class.private static voidgetMethods0(java.util.Map<java.lang.String,CtMember> h, CtClass cc)intgetModifiers()Returns the modifiers for this class, encoded in an integer.CtClass[]getNestedClasses()Returns an array of nested classes declared in the class.CtClassgetSuperclass()Obtains the class object representing the superclass of the class.(package private) intgetUniqueNumber()java.net.URLgetURL()Returns the uniform resource locator (URL) of the class file.booleanhasAnnotation(java.lang.String annotationName)Returns true if the class has the specified annotation type.(package private) static booleanhasAnnotationType(java.lang.Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)Deprecated.(package private) static booleanhasAnnotationType(java.lang.String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)protected CtMember.CachehasMemberCache()Returns null if members are not cached.(package private) voidincGetCounter()private static voidinsertAuxInitializer(CodeAttribute codeAttr, Bytecode initializer, int stacksize)voidinstrument(CodeConverter converter)Applies the given converter to all methods and constructors declared in the class.voidinstrument(ExprEditor editor)Modifies the bodies of all methods and constructors declared in the class.booleanisAnnotation()Determines whether this object represents an annotation type.booleanisEnum()Determines whether this object represents an enum.booleanisFrozen()Returns true if the class has been loaded or written out and thus it cannot be modified any more.booleanisInterface()Determines whether this object represents a class or an interface.booleanisModified()Returns true if the definition of the class has been modified.private static booleanisPubCons(CtConstructor cons)private voidmakeBehaviorCache(CtMember.Cache cache)CtConstructormakeClassInitializer()Makes an empty class initializer (static constructor).private voidmakeFieldCache(CtMember.Cache cache)private intmakeFieldInitializer(Bytecode code, CtClass[] parameters)private voidmakeMemberList(java.util.Map<java.lang.Object,CtClassType> table)CtClassmakeNestedClass(java.lang.String name, boolean isStatic)Makes a new public nested class.java.lang.StringmakeUniqueName(java.lang.String prefix)Makes a unique member name.private voidmodifyClassConstructor(ClassFile cf)private voidmodifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize)private voidmodifyConstructors(ClassFile cf)private voidnameReplaced()private static booleannotFindInArray(java.lang.String prefix, java.lang.String[] values)voidprune()Discards unnecessary attributes, in particular,CodeAttributes (method bodies) of the class, to minimize the memory footprint.voidrebuildClassFile()If this method is called, the class file will be rebuilt when it is finally generated bytoBytecode()andwriteFile().private voidremoveClassFile()voidremoveConstructor(CtConstructor m)Removes a constructor declared in this class.voidremoveField(CtField f)Removes a field declared in this class.voidremoveMethod(CtMethod m)Removes a method declared in this class.voidreplaceClassName(java.lang.String oldname, java.lang.String newname)SubstitutesnewNamefor all occurrences of a class nameoldNamein the class file.voidreplaceClassName(ClassMap classnames)Changes class names appearing in the class file according to the givenmap.private voidsaveClassFile()Converts a ClassFile object into a byte array for saving memory space.voidsetAttribute(java.lang.String name, byte[] data)Adds a named attribute.private ClassFilesetClassFile(ClassFile cf)Updatesclassfileif it is null.(package private) voidsetClassPool(ClassPool cp)voidsetGenericSignature(java.lang.String sig)Sets the generic signature of the class.voidsetInterfaces(CtClass[] list)Sets implemented interfaces.voidsetModifiers(int mod)Sets the modifiers.voidsetName(java.lang.String name)Sets the class namevoidsetSuperclass(CtClass clazz)Changes a super class unless this object represents an interface.booleanstopPruning(boolean stop)Disallows (or allows) automatically pruning thisCtClassobject.booleansubclassOf(CtClass superclass)Determines whether the class directly or indirectly extends the given class.booleansubtypeOf(CtClass clazz)Returnstrueif this class extends or implementsclazz.(package private) static java.lang.Object[]toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)(package private) static java.lang.Object[][]toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo)private static java.lang.ObjecttoAnnoType(Annotation anno, ClassPool cp)voidtoBytecode(java.io.DataOutputStream out)Converts this class to a class file.private static voidupdateInnerEntry(int newMod, java.lang.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 Detail
-
classPool
ClassPool classPool
-
wasChanged
boolean wasChanged
-
wasFrozen
private boolean wasFrozen
-
wasPruned
boolean wasPruned
-
gcConstPool
boolean gcConstPool
-
classfile
ClassFile classfile
-
rawClassfile
byte[] rawClassfile
-
memberCache
private java.lang.ref.Reference<CtMember.Cache> memberCache
-
accessors
private AccessorMaker accessors
-
fieldInitializers
private FieldInitLink fieldInitializers
-
hiddenMethods
private java.util.Map<CtMethod,java.lang.String> hiddenMethods
-
uniqueNumberSeed
private int uniqueNumberSeed
-
doPruning
private boolean doPruning
-
getCount
private int getCount
-
GET_THRESHOLD
private static final int GET_THRESHOLD
- See Also:
- Constant Field Values
-
-
Method Detail
-
extendToString
protected void extendToString(java.lang.StringBuffer buffer)
Description copied from class:CtClassImplemented in subclasses to add to theCtClass.toString()result. Subclasses should put a space before each token added to the buffer.- Overrides:
extendToStringin classCtClass
-
exToString
private void exToString(java.lang.StringBuffer buffer, java.lang.String msg, CtMember head, CtMember tail)
-
getAccessorMaker
public AccessorMaker getAccessorMaker()
Description copied from class:CtClassUndocumented method. Do not use; internal-use only.- Overrides:
getAccessorMakerin classCtClass
-
getClassFile2
public ClassFile getClassFile2()
Description copied from class:CtClassReturns a class file for this class (read only). Normal applications do not need calling this method. UsegetClassFile().The
ClassFileobject 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:
getClassFile2in classCtClass- See Also:
CtClass.getClassFile(),CtClass.isFrozen(),CtClass.prune()
-
getClassFile3
public ClassFile getClassFile3(boolean doCompress)
-
incGetCounter
final void incGetCounter()
- Overrides:
incGetCounterin 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()
-
getClassPool
public ClassPool getClassPool()
Description copied from class:CtClassReturns aClassPoolfor this class.- Overrides:
getClassPoolin classCtClass
-
setClassPool
void setClassPool(ClassPool cp)
-
getURL
public java.net.URL getURL() throws NotFoundExceptionDescription copied from class:CtClassReturns the uniform resource locator (URL) of the class file.- Overrides:
getURLin classCtClass- Throws:
NotFoundException
-
isModified
public boolean isModified()
Description copied from class:CtClassReturns true if the definition of the class has been modified.- Overrides:
isModifiedin classCtClass
-
isFrozen
public boolean isFrozen()
Description copied from class:CtClassReturns true if the class has been loaded or written out and thus it cannot be modified any more.- Overrides:
isFrozenin classCtClass- See Also:
CtClass.defrost(),CtClass.detach()
-
freeze
public void freeze()
Description copied from class:CtClassMakes the class frozen.- Overrides:
freezein classCtClass- See Also:
CtClass.isFrozen(),CtClass.defrost()
-
checkModify
void checkModify() throws java.lang.RuntimeException- Overrides:
checkModifyin classCtClass- Throws:
java.lang.RuntimeException
-
defrost
public void defrost()
Description copied from class:CtClassDefrosts 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.- Overrides:
defrostin classCtClass- See Also:
CtClass.isFrozen(),CtClass.stopPruning(boolean),CtClass.detach()
-
subtypeOf
public boolean subtypeOf(CtClass clazz) throws NotFoundException
Description copied from class:CtClassReturnstrueif this class extends or implementsclazz. It also returnstrueif this class is the same asclazz.- Overrides:
subtypeOfin classCtClass- Throws:
NotFoundException
-
setName
public void setName(java.lang.String name) throws java.lang.RuntimeExceptionDescription copied from class:CtClassSets the class name
-
getGenericSignature
public java.lang.String getGenericSignature()
Description copied from class:CtClassReturns 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:
getGenericSignaturein classCtClass- Returns:
- null if the generic signature is not included.
- See Also:
SignatureAttribute.toClassSignature(String),CtMember.getGenericSignature()
-
setGenericSignature
public void setGenericSignature(java.lang.String sig)
Description copied from class:CtClassSets 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
Tcan be accessible through reflection.MethodSignatureis a utility class. You can directly pass the signature string to thesetGenericSignaturemethod. For the specification of the signatures, see Section 4.7.9.1 Signatures of The Java Virtual Machine Specification (Java SE 8).- Overrides:
setGenericSignaturein classCtClass- Parameters:
sig- a generic signature.- See Also:
SignatureAttribute.ClassSignature.encode(),SignatureAttribute.MethodSignature.encode(),CtMember.setGenericSignature(String)
-
replaceClassName
public void replaceClassName(ClassMap classnames) throws java.lang.RuntimeException
Description copied from class:CtClassChanges class names appearing in the class file according to the givenmap.All the class names appearing in the class file are tested with
mapto 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 ofClassMapso 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:
replaceClassNamein classCtClass- Parameters:
classnames- the hashtable associating replaced class names with substituted names.- Throws:
java.lang.RuntimeException
-
replaceClassName
public void replaceClassName(java.lang.String oldname, java.lang.String newname) throws java.lang.RuntimeExceptionDescription copied from class:CtClassSubstitutesnewNamefor all occurrences of a class nameoldNamein the class file.- Overrides:
replaceClassNamein classCtClass- Parameters:
oldname- replaced class namenewname- substituted class name- Throws:
java.lang.RuntimeException
-
isInterface
public boolean isInterface()
Description copied from class:CtClassDetermines whether this object represents a class or an interface. It returnstrueif this object represents an interface.- Overrides:
isInterfacein classCtClass
-
isAnnotation
public boolean isAnnotation()
Description copied from class:CtClassDetermines whether this object represents an annotation type. It returnstrueif this object represents an annotation type.- Overrides:
isAnnotationin classCtClass
-
isEnum
public boolean isEnum()
Description copied from class:CtClassDetermines whether this object represents an enum. It returnstrueif this object represents an enum.
-
getModifiers
public int getModifiers()
Description copied from class:CtClassReturns 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:
getModifiersin classCtClass- See Also:
Modifier
-
getNestedClasses
public CtClass[] getNestedClasses() throws NotFoundException
Description copied from class:CtClassReturns an array of nested classes declared in the class. Nested classes are inner classes, anonymous classes, local classes, and static nested classes.- Overrides:
getNestedClassesin classCtClass- Throws:
NotFoundException
-
setModifiers
public void setModifiers(int mod)
Description copied from class:CtClassSets 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:
setModifiersin classCtClass- Parameters:
mod- modifiers encoded byjavassist.Modifier- See Also:
Modifier
-
updateInnerEntry
private static void updateInnerEntry(int newMod, java.lang.String name, CtClass clazz, boolean outer)
-
hasAnnotation
public boolean hasAnnotation(java.lang.String annotationName)
Description copied from class:CtClassReturns true if the class has the specified annotation type.- Overrides:
hasAnnotationin classCtClass- Parameters:
annotationName- the name of annotation type.- Returns:
trueif the annotation is found, otherwisefalse.
-
hasAnnotationType
@Deprecated static boolean hasAnnotationType(java.lang.Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)Deprecated.
-
hasAnnotationType
static boolean hasAnnotationType(java.lang.String annotationTypeName, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)
-
getAnnotation
public java.lang.Object getAnnotation(java.lang.Class<?> clz) throws java.lang.ClassNotFoundExceptionDescription copied from class:CtClassReturns the annotation if the class has the specified annotation type. For example, if an annotation@Authoris associated with this class, anAuthorobject is returned. The member values can be obtained by calling methods on theAuthorobject.- Overrides:
getAnnotationin classCtClass- Parameters:
clz- the annotation type.- Returns:
- the annotation if found, otherwise
null. - Throws:
java.lang.ClassNotFoundException
-
getAnnotationType
static java.lang.Object getAnnotationType(java.lang.Class<?> clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
getAnnotations
public java.lang.Object[] getAnnotations() throws java.lang.ClassNotFoundExceptionDescription copied from class:CtClassReturns the annotations associated with this class. For example, if an annotation@Authoris associated with this class, the returned array contains anAuthorobject. The member values can be obtained by calling methods on theAuthorobject.- Overrides:
getAnnotationsin classCtClass- Returns:
- an array of annotation-type objects.
- Throws:
java.lang.ClassNotFoundException- See Also:
CtMember.getAnnotations()
-
getAvailableAnnotations
public java.lang.Object[] getAvailableAnnotations()
Description copied from class:CtClassReturns 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:
getAvailableAnnotationsin classCtClass- Returns:
- an array of annotation-type objects.
- See Also:
CtClass.getAnnotations(),CtMember.getAvailableAnnotations()
-
getAnnotations
private java.lang.Object[] getAnnotations(boolean ignoreNotFound) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
toAnnotationType
static java.lang.Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
toAnnotationType
static java.lang.Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
toAnnoType
private static java.lang.Object toAnnoType(Annotation anno, ClassPool cp) throws java.lang.ClassNotFoundException
- Throws:
java.lang.ClassNotFoundException
-
subclassOf
public boolean subclassOf(CtClass superclass)
Description copied from class:CtClassDetermines 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:
subclassOfin classCtClass
-
getSuperclass
public CtClass getSuperclass() throws NotFoundException
Description copied from class:CtClassObtains the class object representing the superclass of the class. It returns null if this object represents thejava.lang.Objectclass and thus it does not have the super class.If this object represents an interface, this method always returns the
java.lang.Objectclass. To obtain the super interfaces extended by that interface, callgetInterfaces().- Overrides:
getSuperclassin classCtClass- Throws:
NotFoundException
-
setSuperclass
public void setSuperclass(CtClass clazz) throws CannotCompileException
Description copied from class:CtClassChanges 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 appendsclazzto the list of the super interfaces extended by that interface. Note that an interface can extend multiple super interfaces.- Overrides:
setSuperclassin classCtClass- Throws:
CannotCompileException- See Also:
CtClass.replaceClassName(String, String),CtClass.replaceClassName(ClassMap)
-
getInterfaces
public CtClass[] getInterfaces() throws NotFoundException
Description copied from class:CtClassObtains the class objects representing the interfaces implemented by the class or, if this object represents an interface, the interfaces extended by that interface.- Overrides:
getInterfacesin classCtClass- Throws:
NotFoundException
-
setInterfaces
public void setInterfaces(CtClass[] list)
Description copied from class:CtClassSets implemented interfaces. If this object represents an interface, this method sets the interfaces extended by that interface.- Overrides:
setInterfacesin classCtClass- Parameters:
list- a list of theCtClassobjects representing interfaces, ornullif the class implements no interfaces.
-
addInterface
public void addInterface(CtClass anInterface)
Description copied from class:CtClassAdds an interface.- Overrides:
addInterfacein classCtClass- Parameters:
anInterface- the added interface.
-
getDeclaringClass
public CtClass getDeclaringClass() throws NotFoundException
Description copied from class:CtClassIf this class is a member class or interface of another class, then the class enclosing this class is returned.- Overrides:
getDeclaringClassin classCtClass- Returns:
- null if this class is a top-level class or an anonymous class.
- Throws:
NotFoundException
-
getEnclosingBehavior
public CtBehavior getEnclosingBehavior() throws NotFoundException
Description copied from class:CtClassReturns 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:
getEnclosingBehaviorin classCtClass- 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)
Description copied from class:CtClassMakes 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.
isStaticmust be true.- Overrides:
makeNestedClassin classCtClass- Parameters:
name- the simple name of the nested class.isStatic- true if the nested class is static.
-
nameReplaced
private void nameReplaced()
-
hasMemberCache
protected CtMember.Cache hasMemberCache()
Returns null if members are not cached.
-
getMembers
protected CtMember.Cache getMembers()
-
makeFieldCache
private void makeFieldCache(CtMember.Cache cache)
-
makeBehaviorCache
private void makeBehaviorCache(CtMember.Cache cache)
-
getFields
public CtField[] getFields()
Description copied from class:CtClassReturns an array containingCtFieldobjects 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, java.lang.String desc) throws NotFoundException
Description copied from class:CtClassReturns 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:
getFieldin classCtClass- Parameters:
name- the field name.desc- the type descriptor of the field. It is available byCtField.getSignature().- Throws:
NotFoundException- See Also:
CtField.getSignature()
-
checkGetField
private CtField checkGetField(CtField f, java.lang.String name, java.lang.String desc) throws NotFoundException
- Throws:
NotFoundException
-
getField2
CtField getField2(java.lang.String name, java.lang.String desc)
-
getDeclaredFields
public CtField[] getDeclaredFields()
Description copied from class:CtClassGets all the fields declared in the class. The inherited fields are not included.Note: the result does not include inherited fields.
- Overrides:
getDeclaredFieldsin classCtClass
-
getDeclaredField
public CtField getDeclaredField(java.lang.String name) throws NotFoundException
Description copied from class:CtClassRetrieves the field with the specified name among the fields declared in the class.Note: this method does not search the super classes.
- Overrides:
getDeclaredFieldin classCtClass- Throws:
NotFoundException
-
getDeclaredField
public CtField getDeclaredField(java.lang.String name, java.lang.String desc) throws NotFoundException
Description copied from class:CtClassRetrieves 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:
getDeclaredFieldin classCtClass- Parameters:
name- the field name.desc- the type descriptor of the field. It is available byCtField.getSignature().- Throws:
NotFoundException- See Also:
CtField.getSignature()
-
getDeclaredField2
private CtField getDeclaredField2(java.lang.String name, java.lang.String desc)
-
getDeclaredBehaviors
public CtBehavior[] getDeclaredBehaviors()
Description copied from class:CtClassGets all the constructors and methods declared in the class.- Overrides:
getDeclaredBehaviorsin classCtClass
-
getConstructors
public CtConstructor[] getConstructors()
Description copied from class:CtClassReturns an array containingCtConstructorobjects representing all the non-private constructors of the class.- Overrides:
getConstructorsin classCtClass
-
isPubCons
private static boolean isPubCons(CtConstructor cons)
-
getConstructor
public CtConstructor getConstructor(java.lang.String desc) throws NotFoundException
Description copied from class:CtClassReturns 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:
getConstructorin classCtClass- Parameters:
desc- method descriptor- Throws:
NotFoundException- See Also:
Descriptor
-
getDeclaredConstructors
public CtConstructor[] getDeclaredConstructors()
Description copied from class:CtClassGets all the constructors declared in the class.- Overrides:
getDeclaredConstructorsin classCtClass- See Also:
CtConstructor
-
getClassInitializer
public CtConstructor getClassInitializer()
Description copied from class:CtClassGets the class initializer (static constructor) declared in the class. This method returnsnullif no class initializer is not declared.- Overrides:
getClassInitializerin classCtClass- See Also:
CtClass.makeClassInitializer(),CtConstructor
-
getMethods
public CtMethod[] getMethods()
Description copied from class:CtClassReturns an array containingCtMethodobjects representing all the non-private methods of the class. That array includes non-private methods inherited from the superclasses.- Overrides:
getMethodsin classCtClass
-
getMethods0
private static void getMethods0(java.util.Map<java.lang.String,CtMember> h, CtClass cc)
-
getMethod
public CtMethod getMethod(java.lang.String name, java.lang.String desc) throws NotFoundException
Description copied from class:CtClassReturns 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:
getMethodin classCtClass- Parameters:
name- method namedesc- method descriptor- Throws:
NotFoundException- See Also:
CtBehavior.getSignature(),Descriptor
-
getMethod0
private static CtMethod getMethod0(CtClass cc, java.lang.String name, java.lang.String desc)
-
getDeclaredMethods
public CtMethod[] getDeclaredMethods()
Description copied from class:CtClassGets all methods declared in the class. The inherited methods are not included.- Overrides:
getDeclaredMethodsin classCtClass- See Also:
CtMethod
-
getDeclaredMethods
public CtMethod[] getDeclaredMethods(java.lang.String name) throws NotFoundException
Description copied from class:CtClassRetrieves 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:
getDeclaredMethodsin classCtClass- Parameters:
name- method name.- Throws:
NotFoundException
-
getDeclaredMethod
public CtMethod getDeclaredMethod(java.lang.String name) throws NotFoundException
Description copied from class:CtClassRetrieves 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:
getDeclaredMethodin classCtClass- Throws:
NotFoundException- See Also:
CtMethod
-
getDeclaredMethod
public CtMethod getDeclaredMethod(java.lang.String name, CtClass[] params) throws NotFoundException
Description copied from class:CtClassRetrieves 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:
getDeclaredMethodin classCtClass- Parameters:
name- method nameparams- parameter types- Throws:
NotFoundException- See Also:
CtMethod
-
addField
public void addField(CtField f, java.lang.String init) throws CannotCompileException
Description copied from class:CtClassAdds a field with an initial value.The
CtFieldbelonging to anotherCtClasscannot 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
ccisCtClass. The type offisCtField.Note: do not change the modifier of the field (in particular, do not add or remove
staticto/from the modifier) after it is added to the class byaddField().- Overrides:
addFieldin classCtClassinit- an expression for the initial value.- Throws:
CannotCompileException- See Also:
CtField.Initializer.byExpr(String),CtField(CtField,CtClass)
-
addField
public void addField(CtField f, CtField.Initializer init) throws CannotCompileException
Description copied from class:CtClassAdds a field with an initial value.The
CtFieldbelonging to anotherCtClasscannot 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
intfield named "i". The initial value of this field is 1.- Overrides:
addFieldin classCtClassinit- specifies the initial value of the field.- Throws:
CannotCompileException- See Also:
CtField(CtField,CtClass)
-
removeField
public void removeField(CtField f) throws NotFoundException
Description copied from class:CtClassRemoves a field declared in this class.- Overrides:
removeFieldin classCtClass- Parameters:
f- removed field.- Throws:
NotFoundException- if the field is not found.
-
makeClassInitializer
public CtConstructor makeClassInitializer() throws CannotCompileException
Description copied from class:CtClassMakes an empty class initializer (static constructor). If the class already includes a class initializer, this method returns it.- Overrides:
makeClassInitializerin classCtClass- Throws:
CannotCompileException- See Also:
CtClass.getClassInitializer()
-
addConstructor
public void addConstructor(CtConstructor c) throws CannotCompileException
Description copied from class:CtClassAdds a constructor. To add a class initializer (static constructor), callmakeClassInitializer().- Overrides:
addConstructorin classCtClass- Throws:
CannotCompileException- See Also:
CtClass.makeClassInitializer()
-
removeConstructor
public void removeConstructor(CtConstructor m) throws NotFoundException
Description copied from class:CtClassRemoves a constructor declared in this class.- Overrides:
removeConstructorin classCtClass- Parameters:
m- removed constructor.- Throws:
NotFoundException- if the constructor is not found.
-
addMethod
public void addMethod(CtMethod m) throws CannotCompileException
Description copied from class:CtClassAdds a method.- Overrides:
addMethodin classCtClass- Throws:
CannotCompileException
-
removeMethod
public void removeMethod(CtMethod m) throws NotFoundException
Description copied from class:CtClassRemoves a method declared in this class.- Overrides:
removeMethodin classCtClass- Parameters:
m- removed method.- Throws:
NotFoundException- if the method is not found.
-
getAttribute
public byte[] getAttribute(java.lang.String name)
Description copied from class:CtClassObtains 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.bytecodepackage. For example, the following expression returns all the attributes of a class file.getClassFile().getAttributes()
- Overrides:
getAttributein classCtClass- Parameters:
name- attribute name- See Also:
AttributeInfo
-
setAttribute
public void setAttribute(java.lang.String name, byte[] data)Description copied from class:CtClassAdds 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.bytecodepackage. For example, the following expression adds an attributeinfoto a class file.getClassFile().addAttribute(info)
- Overrides:
setAttributein classCtClass- Parameters:
name- attribute namedata- attribute value- See Also:
AttributeInfo
-
instrument
public void instrument(CodeConverter converter) throws CannotCompileException
Description copied from class:CtClassApplies the given converter to all methods and constructors declared in the class. This method callsinstrument()on everyCtMethodandCtConstructorobject in the class.- Overrides:
instrumentin classCtClass- Parameters:
converter- specifies how to modify.- Throws:
CannotCompileException
-
instrument
public void instrument(ExprEditor editor) throws CannotCompileException
Description copied from class:CtClassModifies the bodies of all methods and constructors declared in the class. This method callsinstrument()on everyCtMethodandCtConstructorobject in the class.- Overrides:
instrumentin classCtClass- Parameters:
editor- specifies how to modify.- Throws:
CannotCompileException
-
prune
public void prune()
Description copied from class:CtClassDiscards 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(), 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.
- Overrides:
prunein classCtClass- See Also:
CtClass.prune(),CtClass.stopPruning(boolean)
-
rebuildClassFile
public void rebuildClassFile()
Description copied from class:CtClassIf 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:
rebuildClassFilein classCtClass
-
toBytecode
public void toBytecode(java.io.DataOutputStream out) throws CannotCompileException, java.io.IOExceptionDescription copied from class:CtClassConverts 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:
toBytecodein classCtClass- Parameters:
out- the output stream that a class file is written to.- Throws:
CannotCompileExceptionjava.io.IOException
-
dumpClassFile
private void dumpClassFile(ClassFile cf) throws java.io.IOException
- Throws:
java.io.IOException
-
checkPruned
private void checkPruned(java.lang.String method)
-
stopPruning
public boolean stopPruning(boolean stop)
Description copied from class:CtClassDisallows (or allows) automatically pruning thisCtClassobject.Javassist can automatically prune a
CtClassobject whentoBytecode()(ortoClass(),writeFile()) is called. Since aClassPoolholds all instances ofCtClasseven aftertoBytecode()(ortoClass(),writeFile()) is called, pruning may significantly save memory consumption.If
ClassPool.doPruningis true, the automatic pruning is on by default. Otherwise, it is off. The default value ofClassPool.doPruningis false.- Overrides:
stopPruningin classCtClass- Parameters:
stop- disallow pruning if true. Otherwise, allow.- Returns:
- the previous status of pruning. true if pruning is already stopped.
- See Also:
CtClass.detach(),CtClass.prune(),ClassPool.doPruning
-
modifyClassConstructor
private void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException
-
modifyClassConstructor
private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException
- Throws:
CannotCompileException
-
modifyConstructors
private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException
-
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
java.util.Map<CtMethod,java.lang.String> getHiddenMethods()
-
getUniqueNumber
int getUniqueNumber()
-
makeUniqueName
public java.lang.String makeUniqueName(java.lang.String prefix)
Description copied from class:CtClassMakes 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:
makeUniqueNamein classCtClass- Parameters:
prefix- the prefix of the member name.
-
notFindInArray
private static boolean notFindInArray(java.lang.String prefix, java.lang.String[] values)
-
makeMemberList
private void makeMemberList(java.util.Map<java.lang.Object,CtClassType> table)
-
-