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,
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(). |
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, writeFileClassPool 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.IOExceptionprotected void extendToString(java.lang.StringBuffer buffer)
CtClassCtClass.toString() result.
Subclasses should put a space before each token added to the buffer.extendToString in class CtClassprivate void exToString(java.lang.StringBuffer buffer,
java.lang.String msg,
CtMember head,
CtMember tail)
public AccessorMaker getAccessorMaker()
CtClassgetAccessorMaker in class CtClasspublic 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()final void incGetCounter()
incGetCounter in class CtClassvoid compress()
private void saveClassFile()
private void removeClassFile()
public ClassPool getClassPool()
CtClassClassPool for this class.getClassPool in class CtClassvoid setClassPool(ClassPool cp)
public java.net.URL getURL()
throws NotFoundException
CtClassgetURL in class CtClassNotFoundExceptionpublic boolean isModified()
CtClassisModified in class CtClasspublic boolean isFrozen()
CtClassisFrozen in class CtClassCtClass.defrost(),
CtClass.detach()public void freeze()
CtClassfreeze in class CtClassCtClass.isFrozen(),
CtClass.defrost()void checkModify()
throws java.lang.RuntimeException
checkModify in class CtClassjava.lang.RuntimeExceptionpublic void defrost()
CtClassTo 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 CtClassCtClass.isFrozen(),
CtClass.stopPruning(boolean),
CtClass.detach()public 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(java.lang.String name)
throws java.lang.RuntimeException
CtClasspublic java.lang.String getGenericSignature()
CtClassThe 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 CtClassSignatureAttribute.toClassSignature(String),
CtMember.getGenericSignature()public void setGenericSignature(java.lang.String sig)
CtClassThe 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 CtClasssig - a generic signature.SignatureAttribute.ClassSignature.encode(),
SignatureAttribute.MethodSignature.encode(),
CtMember.setGenericSignature(String)public void replaceClassName(ClassMap classnames) throws java.lang.RuntimeException
CtClassmap.
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 CtClassclassnames - the hashtable associating replaced class names
with substituted names.java.lang.RuntimeExceptionpublic void replaceClassName(java.lang.String oldname,
java.lang.String newname)
throws java.lang.RuntimeException
CtClassnewName for all occurrences of a class
name oldName in the class file.replaceClassName in class CtClassoldname - replaced class namenewname - substituted class namejava.lang.RuntimeExceptionpublic 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(java.lang.Class clz)
CtClasshasAnnotation in class CtClassclz - 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 CtClassclz - the annotation class.null.java.lang.ClassNotFoundExceptionstatic java.lang.Object getAnnotationType(java.lang.Class clz,
ClassPool cp,
AnnotationsAttribute a1,
AnnotationsAttribute a2)
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundExceptionpublic 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 CtClassjava.lang.ClassNotFoundExceptionCtMember.getAnnotations()public java.lang.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()private java.lang.Object[] getAnnotations(boolean ignoreNotFound)
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundExceptionstatic java.lang.Object[] toAnnotationType(boolean ignoreNotFound,
ClassPool cp,
AnnotationsAttribute a1,
AnnotationsAttribute a2)
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundExceptionstatic java.lang.Object[][] toAnnotationType(boolean ignoreNotFound,
ClassPool cp,
ParameterAnnotationsAttribute a1,
ParameterAnnotationsAttribute a2,
MethodInfo minfo)
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundExceptionprivate static java.lang.Object toAnnoType(Annotation anno, ClassPool cp) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundExceptionpublic 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) throws CannotCompileException
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 CtClassCannotCompileExceptionCtClass.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 CtMethod getEnclosingMethod() throws NotFoundException
CtClassgetEnclosingMethod in class CtClassNotFoundExceptionpublic CtClass makeNestedClass(java.lang.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.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()
CtClassCtField 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
CtClassgetField in class CtClassname - the field name.desc - the type descriptor of the field. It is available by
CtField.getSignature().NotFoundExceptionCtField.getSignature()private CtField checkGetField(CtField f, java.lang.String name, java.lang.String desc) throws NotFoundException
NotFoundExceptionCtField getField2(java.lang.String name, java.lang.String desc)
public CtField[] getDeclaredFields()
CtClassNote: the result does not include inherited fields.
getDeclaredFields in class CtClasspublic CtField getDeclaredField(java.lang.String name) throws NotFoundException
CtClassNote: this method does not search the super classes.
getDeclaredField in class CtClassNotFoundExceptionpublic CtField getDeclaredField(java.lang.String name, java.lang.String desc) throws NotFoundException
CtClassNote: this method does not search the super classes.
getDeclaredField in class CtClassname - the field name.desc - the type descriptor of the field. It is available by
CtField.getSignature().NotFoundExceptionCtField.getSignature()private CtField getDeclaredField2(java.lang.String name, java.lang.String desc)
public CtBehavior[] getDeclaredBehaviors()
CtClassgetDeclaredBehaviors in class CtClasspublic CtConstructor[] getConstructors()
CtClassCtConstructor objects
representing all the non-private constructors of the class.getConstructors in class CtClassprivate static boolean isPubCons(CtConstructor cons)
public CtConstructor getConstructor(java.lang.String desc) throws NotFoundException
CtClassjavassist.bytecode.Descriptor.getConstructor in class CtClassdesc - method descriptorNotFoundExceptionDescriptorpublic CtConstructor[] getDeclaredConstructors()
CtClassgetDeclaredConstructors in class CtClassCtConstructorpublic CtConstructor getClassInitializer()
CtClassnull if
no class initializer is not declared.getClassInitializer in class CtClassCtClass.makeClassInitializer(),
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 CtClassprivate static void getMethods0(java.util.HashMap h,
CtClass cc)
public CtMethod getMethod(java.lang.String name, java.lang.String desc) throws NotFoundException
CtClassgetMethod in class CtClassname - method namedesc - method descriptorNotFoundExceptionCtBehavior.getSignature(),
Descriptorprivate static CtMethod getMethod0(CtClass cc, java.lang.String name, java.lang.String desc)
public CtMethod[] getDeclaredMethods()
CtClassgetDeclaredMethods in class CtClassCtMethodpublic CtMethod getDeclaredMethod(java.lang.String name) throws NotFoundException
CtClassNote: this method does not search the superclasses.
getDeclaredMethod in class CtClassNotFoundExceptionCtMethodpublic CtMethod getDeclaredMethod(java.lang.String name, CtClass[] params) throws NotFoundException
CtClassNote: this method does not search the superclasses.
getDeclaredMethod in class CtClassname - method nameparams - parameter typesNotFoundExceptionCtMethodpublic void addField(CtField f, java.lang.String init) throws CannotCompileException
CtClassThe 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 CtClassinit - an expression for the initial value.CannotCompileExceptionCtField.Initializer.byExpr(String),
CtField(CtField,CtClass)public void addField(CtField f, CtField.Initializer init) throws CannotCompileException
CtClassThe 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 CtClassinit - specifies the initial value of the field.CannotCompileExceptionCtField(CtField,CtClass)public void removeField(CtField f) throws NotFoundException
CtClassremoveField in class CtClassf - removed field.NotFoundException - if the field is not found.public CtConstructor makeClassInitializer() throws CannotCompileException
CtClassmakeClassInitializer in class CtClassCannotCompileExceptionCtClass.getClassInitializer()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(java.lang.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 nameAttributeInfopublic void setAttribute(java.lang.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 valueAttributeInfopublic void instrument(CodeConverter converter) throws CannotCompileException
CtClassinstrument()
on every CtMethod and CtConstructor object
in the class.instrument in class CtClassconverter - specifies how to modify.CannotCompileExceptionpublic void instrument(ExprEditor editor) throws CannotCompileException
CtClassinstrument()
on every CtMethod and CtConstructor object
in the class.instrument in class CtClasseditor - specifies how to modify.CannotCompileExceptionpublic 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.
prune in class CtClassCtClass.prune(),
CtClass.stopPruning(boolean)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 void toBytecode(java.io.DataOutputStream out)
throws CannotCompileException,
java.io.IOException
CtClassThis method dose not close the output stream in the end.
toBytecode in class CtClassout - the output stream that a class file is written to.CannotCompileExceptionjava.io.IOExceptionprivate void dumpClassFile(ClassFile cf) throws java.io.IOException
java.io.IOExceptionprivate void checkPruned(java.lang.String method)
public 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.doPruningprivate void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException
private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException
CannotCompileExceptionprivate void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException
private static void insertAuxInitializer(CodeAttribute codeAttr, Bytecode initializer, int stacksize) throws BadBytecode
BadBytecodeprivate int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException
java.util.Hashtable getHiddenMethods()
int getUniqueNumber()
public java.lang.String makeUniqueName(java.lang.String prefix)
CtClassmakeUniqueName in class CtClassprefix - 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)