public class CtClassType extends CtClass
booleanType, byteType, charType, doubleType, floatType, intType, longType, qualifiedName, shortType, version, voidType| Constructor and Description |
|---|
CtClassType(InputStream ins,
ClassPool cp) |
| Modifier and Type | Method and Description |
|---|---|
void |
addConstructor(CtConstructor c)
Adds a constructor.
|
void |
addInterface(CtClass anInterface)
Adds an interface.
|
void |
addMethod(CtMethod m)
Adds a method.
|
Object |
getAnnotation(Class<?> clz)
Returns the annotation if the class has the specified annotation class.
|
Object[] |
getAnnotations()
Returns the annotations associated with this class.
|
byte[] |
getAttribute(String name)
Obtains an attribute with the given name.
|
Object[] |
getAvailableAnnotations()
Returns the annotations associated with this class.
|
ClassFile |
getClassFile2()
Returns a class file for this class (read only).
|
CtConstructor |
getClassInitializer()
Gets the class initializer (static constructor)
declared in the class.
|
ClassPool |
getClassPool()
Returns a
ClassPool for this class. |
CtConstructor |
getConstructor(String desc)
Returns the constructor with the given signature,
which is represented by a character string
called method descriptor.
|
CtConstructor[] |
getDeclaredConstructors()
Gets all the constructors declared in the class.
|
CtField |
getDeclaredField(String name)
Retrieves the field with the specified name among the fields
declared in the class.
|
CtField |
getDeclaredField(String name,
String desc) |
CtField[] |
getDeclaredFields()
Gets all the fields declared in the class.
|
CtMethod |
getDeclaredMethod(String name)
Retrieves the method with the specified name among the methods
declared in the class.
|
CtMethod |
getDeclaredMethod(String name,
CtClass[] params)
Retrieves the method with the specified name and parameter types
among the methods declared in the class.
|
CtMethod[] |
getDeclaredMethods()
Gets all methods declared in the class.
|
CtClass |
getDeclaringClass()
If this class is a member class or interface of another class,
then the class enclosing this class is returned.
|
CtField |
getField(String name)
Returns the field with the specified name.
|
CtField |
getField(String name,
String desc) |
CtField[] |
getFields()
Returns an array containing
CtField objects
representing all the non-private fields of the class. |
CtClass[] |
getInterfaces()
Obtains the class objects representing the interfaces implemented
by the class or, if this object represents an interface, the interfaces
extended by that interface.
|
protected xapi.bytecode.CtMember.Cache |
getMembers() |
CtMethod |
getMethod(String name,
String desc)
Returns the method with the given name and signature.
|
CtMethod[] |
getMethods()
Returns an array containing
CtMethod objects
representing all the non-private methods of the class. |
int |
getModifiers()
Returns the modifiers for this class, encoded in an integer.
|
CtClass[] |
getNestedClasses()
Returns an array of nested classes declared in the class.
|
CtClass |
getSuperclass()
Obtains the class object representing the superclass of the
class.
|
URL |
getURL()
Returns the uniform resource locator (URL) of the class file.
|
boolean |
hasAnnotation(Class<?> clz)
Returns true if the class has the specified annotation class.
|
protected xapi.bytecode.CtMember.Cache |
hasMemberCache()
Returns null if members are not cached.
|
boolean |
isAnnotation()
Determines whether this object represents an annotation type.
|
boolean |
isEnum()
Determines whether this object represents an enum.
|
boolean |
isInterface()
Determines whether this object represents a class or an interface.
|
boolean |
isModified()
Returns true if the definition of the class has been modified.
|
CtClass |
makeNestedClass(String name,
boolean isStatic)
Makes a new public nested class.
|
String |
makeUniqueName(String prefix)
Makes a unique member name.
|
void |
prune()
Discards unnecessary attributes, in particular,
CodeAttributes (method bodies) of the class,
to minimize the memory footprint. |
void |
rebuildClassFile()
If this method is called, the class file will be
rebuilt when it is finally generated by
toBytecode() and writeFile(). |
void |
removeConstructor(CtConstructor m)
Removes a constructor declared in this class.
|
void |
removeField(CtField f)
Removes a field declared in this class.
|
void |
removeMethod(CtMethod m)
Removes a method declared in this class.
|
void |
setAttribute(String name,
byte[] data)
Adds a named attribute.
|
void |
setInterfaces(CtClass[] list)
Sets implemented interfaces.
|
void |
setModifiers(int mod)
Sets the modifiers.
|
void |
setName(String name)
Sets the class name
|
void |
setSuperclass(CtClass clazz)
Changes a super class unless this object represents an interface.
|
boolean |
stopPruning(boolean stop)
Disallows (or allows) automatically pruning this
CtClass
object. |
boolean |
subclassOf(CtClass superclass)
Determines whether the class directly or indirectly extends
the given class.
|
boolean |
subtypeOf(CtClass clazz)
Returns
true if this class extends or implements
clazz. |
debugWriteFile, debugWriteFile, defrost, detach, extendToString, freeze, getClassFile, getComponentType, getConstructors, getDeclaredBehaviors, getDeclaredConstructor, getEnclosedName, getEnclosingMethod, getName, getPackageName, getRefClasses, getSimpleName, isArray, isFrozen, isPrimitive, main, makeClassInitializer, replaceClassName, replaceClassName, toBytecode, toBytecode, toString, writeFile, writeFilepublic CtClassType(InputStream ins, ClassPool cp) throws IOException
IOExceptionpublic ClassFile getClassFile2()
CtClassgetClassFile().
The ClassFile object obtained by this method
is read only. Changes to this object might not be reflected
on a class file generated by toBytecode(),
toClass(), etc.
This method is available even if isFrozen()
is true. However, if the class is frozen, it might be also
pruned.
getClassFile2 in class CtClassCtClass.getClassFile(),
CtClass.isFrozen(),
CtClass.prune()public ClassPool getClassPool()
CtClassClassPool for this class.getClassPool in class CtClasspublic URL getURL() throws NotFoundException
CtClassgetURL in class CtClassNotFoundExceptionpublic boolean isModified()
CtClassisModified in class CtClasspublic boolean subtypeOf(CtClass clazz) throws NotFoundException
CtClasstrue if this class extends or implements
clazz. It also returns true if
this class is the same as clazz.subtypeOf in class CtClassNotFoundExceptionpublic void setName(String name) throws RuntimeException
CtClasssetName in class CtClassname - fully-qualified nameRuntimeExceptionprotected xapi.bytecode.CtMember.Cache hasMemberCache()
public boolean isInterface()
CtClasstrue if this object represents an interface.isInterface in class CtClasspublic boolean isAnnotation()
CtClasstrue if this object represents an annotation type.isAnnotation in class CtClasspublic boolean isEnum()
CtClasstrue if this object represents an enum.public int getModifiers()
CtClassjavassist.Modifier.
If the class is a static nested class (a.k.a. static inner class),
the returned modifiers include Modifier.STATIC.
getModifiers in class CtClassModifierpublic CtClass[] getNestedClasses() throws NotFoundException
CtClassgetNestedClasses in class CtClassNotFoundExceptionpublic void setModifiers(int mod)
CtClassIf the class is a nested class, this method also modifies the class declaring that nested class (i.e. the enclosing class is modified).
setModifiers in class CtClassmod - modifiers encoded by
javassist.ModifierModifierpublic boolean hasAnnotation(Class<?> clz)
CtClasshasAnnotation in class CtClassclz - the annotation class.true if the annotation is found, otherwise false.public Object getAnnotation(Class<?> clz) throws ClassNotFoundException
CtClass@Author is associated
with this class, an Author object is returned.
The member values can be obtained by calling methods on
the Author object.getAnnotation in class CtClassclz - the annotation class.null.ClassNotFoundExceptionpublic Object[] getAnnotations() throws ClassNotFoundException
CtClass@Author is associated
with this class, the returned array contains an Author
object. The member values can be obtained by calling methods on
the Author object.getAnnotations in class CtClassClassNotFoundExceptionCtMember.getAnnotations()public Object[] getAvailableAnnotations()
CtClassgetAnnotations()
except that, if any annotations are not on the classpath,
they are not included in the returned array.getAvailableAnnotations in class CtClassCtClass.getAnnotations(),
CtMember.getAvailableAnnotations()public boolean subclassOf(CtClass superclass)
CtClassThis method returns true if the given class is identical to the class represented by this object.
subclassOf in class CtClasspublic CtClass getSuperclass() throws NotFoundException
CtClassjava.lang.Object class and thus it does not have
the super class.
If this object represents an interface, this method
always returns the java.lang.Object class.
To obtain the super interfaces
extended by that interface, call getInterfaces().
getSuperclass in class CtClassNotFoundExceptionpublic void setSuperclass(CtClass clazz)
CtClassIf this object represents an interface, this method is equivalent
to addInterface(); it appends clazz to
the list of the super interfaces extended by that interface.
Note that an interface can extend multiple super interfaces.
setSuperclass in class CtClassCtClass.replaceClassName(String, String),
CtClass.replaceClassName(ClassMap)public CtClass[] getInterfaces() throws NotFoundException
CtClassgetInterfaces in class CtClassNotFoundExceptionpublic void setInterfaces(CtClass[] list)
CtClasssetInterfaces in class CtClasslist - a list of the CtClass objects
representing interfaces, or
null if the class implements
no interfaces.public void addInterface(CtClass anInterface)
CtClassaddInterface in class CtClassanInterface - the added interface.public CtClass getDeclaringClass() throws NotFoundException
CtClassgetDeclaringClass in class CtClassNotFoundExceptionpublic CtClass makeNestedClass(String name, boolean isStatic)
CtClassCtClass, 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.
makeNestedClass in class CtClassname - the simple name of the nested class.isStatic - true if the nested class is static.public CtConstructor getConstructor(String desc) throws NotFoundException
CtClassjavassist.bytecode.Descriptor.getConstructor in class CtClassdesc - method descriptorNotFoundExceptionjavassist.bytecode.Descriptorpublic CtConstructor[] getDeclaredConstructors()
CtClassgetDeclaredConstructors in class CtClassjavassist.CtConstructorpublic CtConstructor getClassInitializer()
CtClassnull if
no class initializer is not declared.getClassInitializer in class CtClassCtClass.makeClassInitializer(),
javassist.CtConstructorpublic CtMethod[] getMethods()
CtClassCtMethod objects
representing all the non-private methods of the class.
That array includes non-private methods inherited from the
superclasses.getMethods in class CtClassprotected xapi.bytecode.CtMember.Cache getMembers()
public CtMethod getMethod(String name, String desc) throws NotFoundException
CtClassgetMethod in class CtClassname - method namedesc - method descriptorNotFoundExceptionCtBehavior.getSignature(),
javassist.bytecode.Descriptorpublic CtMethod[] getDeclaredMethods()
CtClassgetDeclaredMethods in class CtClassjavassist.CtMethodpublic CtMethod getDeclaredMethod(String name) throws NotFoundException
CtClassNote: this method does not search the superclasses.
getDeclaredMethod in class CtClassNotFoundExceptionjavassist.CtMethodpublic CtMethod getDeclaredMethod(String name, CtClass[] params) throws NotFoundException
CtClassNote: this method does not search the superclasses.
getDeclaredMethod in class CtClassname - method nameparams - parameter typesNotFoundExceptionjavassist.CtMethodpublic CtField[] getFields()
CtClassCtField objects
representing all the non-private fields of the class.
That array includes non-private fields inherited from the
superclasses.public CtField getField(String name) throws NotFoundException
CtClassgetField in class CtClassNotFoundExceptionpublic CtField getField(String name, String desc) throws NotFoundException
NotFoundExceptionpublic CtField[] getDeclaredFields()
CtClassNote: the result does not include inherited fields.
getDeclaredFields in class CtClasspublic CtField getDeclaredField(String name) throws NotFoundException
CtClassNote: this method does not search the superclasses.
getDeclaredField in class CtClassNotFoundExceptionpublic CtField getDeclaredField(String name, String desc) throws NotFoundException
NotFoundExceptionpublic void removeField(CtField f) throws NotFoundException
CtClassremoveField in class CtClassf - removed field.NotFoundException - if the field is not found.public void addConstructor(CtConstructor c) throws CannotCompileException
CtClassmakeClassInitializer().addConstructor in class CtClassCannotCompileExceptionCtClass.makeClassInitializer()public void removeConstructor(CtConstructor m) throws NotFoundException
CtClassremoveConstructor in class CtClassm - removed constructor.NotFoundException - if the constructor is not found.public void addMethod(CtMethod m) throws CannotCompileException
CtClassaddMethod in class CtClassCannotCompileExceptionpublic void removeMethod(CtMethod m) throws NotFoundException
CtClassremoveMethod in class CtClassm - removed method.NotFoundException - if the method is not found.public byte[] getAttribute(String name)
CtClassThis 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()
getAttribute in class CtClassname - attribute namejavassist.bytecode.AttributeInfopublic void setAttribute(String name, byte[] data)
CtClassThis is a convenient method mainly for adding
a user-defined attribute. For dealing with attributes, see the
javassist.bytecode package. For example, the following
expression adds an attribute info to a class file.
getClassFile().addAttribute(info)
setAttribute in class CtClassname - attribute namedata - attribute valuejavassist.bytecode.AttributeInfopublic void prune()
CtClassCodeAttributes (method bodies) of the class,
to minimize the memory footprint.
After calling this method, the class is read only.
It cannot be modified any more.
Furthermore, toBytecode(),
writeFile(), toClass(),
or instrument() cannot be called.
However, the method names and signatures in the class etc.
are still accessible.
toBytecode(), writeFile(), and
toClass() internally call this method if
automatic pruning is on.
According to some experiments, pruning does not really reduce memory consumption. Only about 20%. Since pruning takes time, it might not pay off. So the automatic pruning is off by default.
public void rebuildClassFile()
CtClasstoBytecode() and writeFile().
For a performance reason, the symbol table of the
class file may contain unused entries, for example,
after a method or a filed is deleted.
This method
removes those unused entries. This removal will
minimize the size of the class file.rebuildClassFile in class CtClasspublic boolean stopPruning(boolean stop)
CtClassCtClass
object.
Javassist can automatically prune a CtClass object
when toBytecode() (or toClass(),
writeFile()) is called.
Since a ClassPool holds all instances of CtClass
even after toBytecode() (or toClass(),
writeFile()) is called, pruning may significantly
save memory consumption.
If ClassPool.doPruning is true, the automatic pruning
is on by default. Otherwise, it is off. The default value of
ClassPool.doPruning is false.
stopPruning in class CtClassstop - disallow pruning if true. Otherwise, allow.CtClass.detach(),
CtClass.prune(),
ClassPool.doPruningpublic String makeUniqueName(String prefix)
CtClassmakeUniqueName in class CtClassprefix - the prefix of the member name.Copyright © December 07, 2012–2015 The Internet Party. All rights reserved.