class CtClassType extends CtClass
CtClass.DelayedFileOutputStream
Modifier and Type | Field and Description |
---|---|
private AccessorMaker |
accessors |
(package private) ClassFile |
classfile |
(package private) ClassPool |
classPool |
private boolean |
doPruning |
private FieldInitLink |
fieldInitializers |
(package private) boolean |
gcConstPool |
private static int |
GET_THRESHOLD |
private int |
getCount |
private java.util.Hashtable |
hiddenMethods |
private java.lang.ref.WeakReference |
memberCache |
(package private) byte[] |
rawClassfile |
private int |
uniqueNumberSeed |
(package private) boolean |
wasChanged |
private boolean |
wasFrozen |
(package private) boolean |
wasPruned |
booleanType, byteType, charType, debugDump, doubleType, floatType, intType, javaLangObject, longType, primitiveTypes, qualifiedName, shortType, version, voidType
Constructor and Description |
---|
CtClassType(java.io.InputStream ins,
ClassPool cp) |
CtClassType(java.lang.String name,
ClassPool cp) |
Modifier and Type | Method and Description |
---|---|
void |
addConstructor(CtConstructor c)
Adds a constructor.
|
void |
addField(CtField f,
CtField.Initializer init)
Adds a field with an initial value.
|
void |
addField(CtField f,
java.lang.String init)
Adds a field with an initial value.
|
void |
addInterface(CtClass anInterface)
Adds an interface.
|
void |
addMethod(CtMethod m)
Adds a method.
|
private CtField |
checkGetField(CtField f,
java.lang.String name,
java.lang.String desc) |
(package private) void |
checkModify() |
private void |
checkPruned(java.lang.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 |
dumpClassFile(ClassFile cf) |
protected void |
extendToString(java.lang.StringBuffer buffer)
Implemented in subclasses to add to the
CtClass.toString() result. |
private void |
exToString(java.lang.StringBuffer buffer,
java.lang.String msg,
CtMember head,
CtMember tail) |
void |
freeze()
Makes the class frozen.
|
AccessorMaker |
getAccessorMaker()
Undocumented method.
|
java.lang.Object |
getAnnotation(java.lang.Class clz)
Returns the annotation if the class has the specified annotation class.
|
java.lang.Object[] |
getAnnotations()
Returns the annotations associated with this class.
|
private java.lang.Object[] |
getAnnotations(boolean ignoreNotFound) |
(package private) static java.lang.Object |
getAnnotationType(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.
|
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(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 containing
CtConstructor objects
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.
|
CtField |
getDeclaredField(java.lang.String name)
Retrieves the field with the specified name among the fields
declared in the class.
|
CtField |
getDeclaredField(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 CtField |
getDeclaredField2(java.lang.String name,
java.lang.String desc) |
CtField[] |
getDeclaredFields()
Gets all the fields declared in the class.
|
CtMethod |
getDeclaredMethod(java.lang.String name)
Retrieves the method with the specified name among the methods
declared in the class.
|
CtMethod |
getDeclaredMethod(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.
|
CtClass |
getDeclaringClass()
If this class is a member class or interface of another class,
then the class enclosing this class is returned.
|
CtMethod |
getEnclosingMethod()
Returns the immediately enclosing method of this class.
|
CtField |
getField(java.lang.String name,
java.lang.String desc)
Returns the field with the specified name and type.
|
(package private) CtField |
getField2(java.lang.String name,
java.lang.String desc) |
CtField[] |
getFields()
Returns an array containing
CtField objects
representing all the non-private fields of the class. |
private static void |
getFields(java.util.ArrayList alist,
CtClass cc) |
java.lang.String |
getGenericSignature()
Returns the generic signature of the class.
|
(package private) java.util.Hashtable |
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.Cache |
getMembers() |
CtMethod |
getMethod(java.lang.String name,
java.lang.String desc)
Returns the method with the given name and signature.
|
private static CtMethod |
getMethod0(CtClass cc,
java.lang.String name,
java.lang.String desc) |
CtMethod[] |
getMethods()
Returns an array containing
CtMethod objects
representing all the non-private methods of the class. |
private static void |
getMethods0(java.util.HashMap h,
CtClass cc) |
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.
|
(package private) int |
getUniqueNumber() |
java.net.URL |
getURL()
Returns the uniform resource locator (URL) of the class file.
|
boolean |
hasAnnotation(java.lang.Class clz)
Returns true if the class has the specified annotation class.
|
(package private) static boolean |
hasAnnotationType(java.lang.Class clz,
ClassPool cp,
AnnotationsAttribute a1,
AnnotationsAttribute a2) |
protected CtMember.Cache |
hasMemberCache()
Returns null if members are not cached.
|
(package private) void |
incGetCounter() |
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 |
isAnnotation()
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 |
isInterface()
Determines whether this object represents a class or an interface.
|
boolean |
isModified()
Returns true if the definition of the class has been modified.
|
private static boolean |
isPubCons(CtConstructor cons) |
private void |
makeBehaviorCache(CtMember.Cache cache) |
CtConstructor |
makeClassInitializer()
Makes an empty class initializer (static constructor).
|
private void |
makeFieldCache(CtMember.Cache cache) |
private int |
makeFieldInitializer(Bytecode code,
CtClass[] parameters) |
private void |
makeMemberList(java.util.HashMap table) |
CtClass |
makeNestedClass(java.lang.String name,
boolean isStatic)
Makes a new public nested class.
|
java.lang.String |
makeUniqueName(java.lang.String prefix)
Makes a unique member name.
|
private void |
modifyClassConstructor(ClassFile cf) |
private void |
modifyClassConstructor(ClassFile cf,
Bytecode code,
int stacksize,
int localsize) |
private void |
modifyConstructors(ClassFile cf) |
private void |
nameReplaced() |
private static boolean |
notFindInArray(java.lang.String prefix,
java.lang.String[] values) |
void |
prune()
Discards unnecessary attributes, in particular,
CodeAttribute s (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() . |
private void |
removeClassFile() |
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 |
replaceClassName(ClassMap classnames)
Changes class names appearing in the class file according to the
given
map . |
void |
replaceClassName(java.lang.String oldname,
java.lang.String newname)
Substitutes
newName for all occurrences of a class
name oldName in the class file. |
private void |
saveClassFile()
Converts a ClassFile object into a byte array
for saving memory space.
|
void |
setAttribute(java.lang.String name,
byte[] data)
Adds a named attribute.
|
(package private) void |
setClassPool(ClassPool cp) |
void |
setGenericSignature(java.lang.String sig)
Sets the generic signature of the class.
|
void |
setInterfaces(CtClass[] list)
Sets implemented interfaces.
|
void |
setModifiers(int mod)
Sets the modifiers.
|
void |
setName(java.lang.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 . |
(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.Object |
toAnnoType(Annotation anno,
ClassPool cp) |
void |
toBytecode(java.io.DataOutputStream out)
Converts this class to a class file.
|
addField, debugWriteFile, debugWriteFile, detach, getClassFile, getComponentType, getDeclaredClasses, getDeclaredConstructor, getField, getName, getPackageName, getRefClasses, getSimpleName, isArray, isPrimitive, main, makeFileOutput, toBytecode, toClass, toClass, toClass, toString, writeFile, writeFile
ClassPool classPool
boolean wasChanged
private boolean wasFrozen
boolean wasPruned
boolean gcConstPool
ClassFile classfile
byte[] rawClassfile
private java.lang.ref.WeakReference memberCache
private AccessorMaker accessors
private FieldInitLink fieldInitializers
private java.util.Hashtable hiddenMethods
private int uniqueNumberSeed
private boolean doPruning
private int getCount
private static final int GET_THRESHOLD
CtClassType(java.lang.String name, ClassPool cp)
CtClassType(java.io.InputStream ins, ClassPool cp) throws java.io.IOException
java.io.IOException
protected void extendToString(java.lang.StringBuffer buffer)
CtClass
CtClass.toString()
result.
Subclasses should put a space before each token added to the buffer.extendToString
in class CtClass
private void exToString(java.lang.StringBuffer buffer, java.lang.String msg, CtMember head, CtMember tail)
public AccessorMaker getAccessorMaker()
CtClass
getAccessorMaker
in class CtClass
public ClassFile getClassFile2()
CtClass
getClassFile()
.
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 CtClass
CtClass.getClassFile()
,
CtClass.isFrozen()
,
CtClass.prune()
final void incGetCounter()
incGetCounter
in class CtClass
void compress()
private void saveClassFile()
private void removeClassFile()
public ClassPool getClassPool()
CtClass
ClassPool
for this class.getClassPool
in class CtClass
void setClassPool(ClassPool cp)
public java.net.URL getURL() throws NotFoundException
CtClass
getURL
in class CtClass
NotFoundException
public boolean isModified()
CtClass
isModified
in class CtClass
public boolean isFrozen()
CtClass
isFrozen
in class CtClass
CtClass.defrost()
,
CtClass.detach()
public void freeze()
CtClass
freeze
in class CtClass
CtClass.isFrozen()
,
CtClass.defrost()
void checkModify() throws java.lang.RuntimeException
checkModify
in class CtClass
java.lang.RuntimeException
public void defrost()
CtClass
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.
defrost
in class CtClass
CtClass.isFrozen()
,
CtClass.stopPruning(boolean)
,
CtClass.detach()
public boolean subtypeOf(CtClass clazz) throws NotFoundException
CtClass
true
if this class extends or implements
clazz
. It also returns true
if
this class is the same as clazz
.subtypeOf
in class CtClass
NotFoundException
public void setName(java.lang.String name) throws java.lang.RuntimeException
CtClass
public java.lang.String getGenericSignature()
CtClass
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.
getGenericSignature
in class CtClass
SignatureAttribute.toClassSignature(String)
,
CtMember.getGenericSignature()
public void setGenericSignature(java.lang.String sig)
CtClass
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 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.
setGenericSignature
in class CtClass
sig
- a generic signature.SignatureAttribute.ClassSignature.encode()
,
SignatureAttribute.MethodSignature.encode()
,
CtMember.setGenericSignature(String)
public void replaceClassName(ClassMap classnames) throws java.lang.RuntimeException
CtClass
map
.
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 of ClassMap
so that get()
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.
replaceClassName
in class CtClass
classnames
- the hashtable associating replaced class names
with substituted names.java.lang.RuntimeException
public void replaceClassName(java.lang.String oldname, java.lang.String newname) throws java.lang.RuntimeException
CtClass
newName
for all occurrences of a class
name oldName
in the class file.replaceClassName
in class CtClass
oldname
- replaced class namenewname
- substituted class namejava.lang.RuntimeException
public boolean isInterface()
CtClass
true
if this object represents an interface.isInterface
in class CtClass
public boolean isAnnotation()
CtClass
true
if this object represents an annotation type.isAnnotation
in class CtClass
public boolean isEnum()
CtClass
true
if this object represents an enum.public int getModifiers()
CtClass
javassist.Modifier
.
If the class is a static nested class (a.k.a. static inner class),
the returned modifiers include Modifier.STATIC
.
getModifiers
in class CtClass
Modifier
public CtClass[] getNestedClasses() throws NotFoundException
CtClass
getNestedClasses
in class CtClass
NotFoundException
public void setModifiers(int mod)
CtClass
If 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 CtClass
mod
- modifiers encoded by
javassist.Modifier
Modifier
public boolean hasAnnotation(java.lang.Class clz)
CtClass
hasAnnotation
in class CtClass
clz
- the annotation class.true
if the annotation is found, otherwise false
.static boolean hasAnnotationType(java.lang.Class clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2)
public java.lang.Object getAnnotation(java.lang.Class clz) throws java.lang.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 CtClass
clz
- the annotation class.null
.java.lang.ClassNotFoundException
static java.lang.Object getAnnotationType(java.lang.Class clz, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public java.lang.Object[] getAnnotations() throws java.lang.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 CtClass
java.lang.ClassNotFoundException
CtMember.getAnnotations()
public java.lang.Object[] getAvailableAnnotations()
CtClass
getAnnotations()
except that, if any annotations are not on the classpath,
they are not included in the returned array.getAvailableAnnotations
in class CtClass
CtClass.getAnnotations()
,
CtMember.getAvailableAnnotations()
private java.lang.Object[] getAnnotations(boolean ignoreNotFound) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
static java.lang.Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
static java.lang.Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
private static java.lang.Object toAnnoType(Annotation anno, ClassPool cp) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public boolean subclassOf(CtClass superclass)
CtClass
This method returns true if the given class is identical to the class represented by this object.
subclassOf
in class CtClass
public CtClass getSuperclass() throws NotFoundException
CtClass
java.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 CtClass
NotFoundException
public void setSuperclass(CtClass clazz) throws CannotCompileException
CtClass
If 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 CtClass
CannotCompileException
CtClass.replaceClassName(String, String)
,
CtClass.replaceClassName(ClassMap)
public CtClass[] getInterfaces() throws NotFoundException
CtClass
getInterfaces
in class CtClass
NotFoundException
public void setInterfaces(CtClass[] list)
CtClass
setInterfaces
in class CtClass
list
- a list of the CtClass
objects
representing interfaces, or
null
if the class implements
no interfaces.public void addInterface(CtClass anInterface)
CtClass
addInterface
in class CtClass
anInterface
- the added interface.public CtClass getDeclaringClass() throws NotFoundException
CtClass
getDeclaringClass
in class CtClass
NotFoundException
public CtMethod getEnclosingMethod() throws NotFoundException
CtClass
getEnclosingMethod
in class CtClass
NotFoundException
public CtClass makeNestedClass(java.lang.String name, boolean isStatic)
CtClass
CtClass
, 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 CtClass
name
- the simple name of the nested class.isStatic
- true if the nested class is static.private void nameReplaced()
protected CtMember.Cache hasMemberCache()
protected CtMember.Cache getMembers()
private void makeFieldCache(CtMember.Cache cache)
private void makeBehaviorCache(CtMember.Cache cache)
public CtField[] getFields()
CtClass
CtField
objects
representing all the non-private fields of the class.
That array includes non-private fields inherited from the
superclasses.private static void getFields(java.util.ArrayList alist, CtClass cc)
public CtField getField(java.lang.String name, java.lang.String desc) throws NotFoundException
CtClass
getField
in class CtClass
name
- the field name.desc
- the type descriptor of the field. It is available by
CtField.getSignature()
.NotFoundException
CtField.getSignature()
private CtField checkGetField(CtField f, java.lang.String name, java.lang.String desc) throws NotFoundException
NotFoundException
CtField getField2(java.lang.String name, java.lang.String desc)
public CtField[] getDeclaredFields()
CtClass
Note: the result does not include inherited fields.
getDeclaredFields
in class CtClass
public CtField getDeclaredField(java.lang.String name) throws NotFoundException
CtClass
Note: this method does not search the super classes.
getDeclaredField
in class CtClass
NotFoundException
public CtField getDeclaredField(java.lang.String name, java.lang.String desc) throws NotFoundException
CtClass
Note: this method does not search the super classes.
getDeclaredField
in class CtClass
name
- the field name.desc
- the type descriptor of the field. It is available by
CtField.getSignature()
.NotFoundException
CtField.getSignature()
private CtField getDeclaredField2(java.lang.String name, java.lang.String desc)
public CtBehavior[] getDeclaredBehaviors()
CtClass
getDeclaredBehaviors
in class CtClass
public CtConstructor[] getConstructors()
CtClass
CtConstructor
objects
representing all the non-private constructors of the class.getConstructors
in class CtClass
private static boolean isPubCons(CtConstructor cons)
public CtConstructor getConstructor(java.lang.String desc) throws NotFoundException
CtClass
javassist.bytecode.Descriptor
.getConstructor
in class CtClass
desc
- method descriptorNotFoundException
Descriptor
public CtConstructor[] getDeclaredConstructors()
CtClass
getDeclaredConstructors
in class CtClass
CtConstructor
public CtConstructor getClassInitializer()
CtClass
null
if
no class initializer is not declared.getClassInitializer
in class CtClass
CtClass.makeClassInitializer()
,
CtConstructor
public CtMethod[] getMethods()
CtClass
CtMethod
objects
representing all the non-private methods of the class.
That array includes non-private methods inherited from the
superclasses.getMethods
in class CtClass
private static void getMethods0(java.util.HashMap h, CtClass cc)
public CtMethod getMethod(java.lang.String name, java.lang.String desc) throws NotFoundException
CtClass
getMethod
in class CtClass
name
- method namedesc
- method descriptorNotFoundException
CtBehavior.getSignature()
,
Descriptor
private static CtMethod getMethod0(CtClass cc, java.lang.String name, java.lang.String desc)
public CtMethod[] getDeclaredMethods()
CtClass
getDeclaredMethods
in class CtClass
CtMethod
public CtMethod getDeclaredMethod(java.lang.String name) throws NotFoundException
CtClass
Note: this method does not search the superclasses.
getDeclaredMethod
in class CtClass
NotFoundException
CtMethod
public CtMethod getDeclaredMethod(java.lang.String name, CtClass[] params) throws NotFoundException
CtClass
Note: this method does not search the superclasses.
getDeclaredMethod
in class CtClass
name
- method nameparams
- parameter typesNotFoundException
CtMethod
public void addField(CtField f, java.lang.String init) throws CannotCompileException
CtClass
The CtField
belonging to another
CtClass
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
is CtClass
.
The type of f
is CtField
.
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 by addField()
.
addField
in class CtClass
init
- an expression for the initial value.CannotCompileException
CtField.Initializer.byExpr(String)
,
CtField(CtField,CtClass)
public void addField(CtField f, CtField.Initializer init) throws CannotCompileException
CtClass
The CtField
belonging to another
CtClass
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.
addField
in class CtClass
init
- specifies the initial value of the field.CannotCompileException
CtField(CtField,CtClass)
public void removeField(CtField f) throws NotFoundException
CtClass
removeField
in class CtClass
f
- removed field.NotFoundException
- if the field is not found.public CtConstructor makeClassInitializer() throws CannotCompileException
CtClass
makeClassInitializer
in class CtClass
CannotCompileException
CtClass.getClassInitializer()
public void addConstructor(CtConstructor c) throws CannotCompileException
CtClass
makeClassInitializer()
.addConstructor
in class CtClass
CannotCompileException
CtClass.makeClassInitializer()
public void removeConstructor(CtConstructor m) throws NotFoundException
CtClass
removeConstructor
in class CtClass
m
- removed constructor.NotFoundException
- if the constructor is not found.public void addMethod(CtMethod m) throws CannotCompileException
CtClass
addMethod
in class CtClass
CannotCompileException
public void removeMethod(CtMethod m) throws NotFoundException
CtClass
removeMethod
in class CtClass
m
- removed method.NotFoundException
- if the method is not found.public byte[] getAttribute(java.lang.String name)
CtClass
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()
getAttribute
in class CtClass
name
- attribute nameAttributeInfo
public void setAttribute(java.lang.String name, byte[] data)
CtClass
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 attribute info
to a class file.
getClassFile().addAttribute(info)
setAttribute
in class CtClass
name
- attribute namedata
- attribute valueAttributeInfo
public void instrument(CodeConverter converter) throws CannotCompileException
CtClass
instrument()
on every CtMethod
and CtConstructor
object
in the class.instrument
in class CtClass
converter
- specifies how to modify.CannotCompileException
public void instrument(ExprEditor editor) throws CannotCompileException
CtClass
instrument()
on every CtMethod
and CtConstructor
object
in the class.instrument
in class CtClass
editor
- specifies how to modify.CannotCompileException
public void prune()
CtClass
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()
,
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.
prune
in class CtClass
CtClass.prune()
,
CtClass.stopPruning(boolean)
public void rebuildClassFile()
CtClass
toBytecode()
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 CtClass
public void toBytecode(java.io.DataOutputStream out) throws CannotCompileException, java.io.IOException
CtClass
This method dose not close the output stream in the end.
toBytecode
in class CtClass
out
- the output stream that a class file is written to.CannotCompileException
java.io.IOException
private void dumpClassFile(ClassFile cf) throws java.io.IOException
java.io.IOException
private void checkPruned(java.lang.String method)
public boolean stopPruning(boolean stop)
CtClass
CtClass
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 CtClass
stop
- disallow pruning if true. Otherwise, allow.CtClass.detach()
,
CtClass.prune()
,
ClassPool.doPruning
private void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException
private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException
CannotCompileException
private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException
private static void insertAuxInitializer(CodeAttribute codeAttr, Bytecode initializer, int stacksize) throws BadBytecode
BadBytecode
private int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException
java.util.Hashtable getHiddenMethods()
int getUniqueNumber()
public java.lang.String makeUniqueName(java.lang.String prefix)
CtClass
makeUniqueName
in class CtClass
prefix
- the prefix of the member name.private static boolean notFindInArray(java.lang.String prefix, java.lang.String[] values)
private void makeMemberList(java.util.HashMap table)