|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavassist.CtClass
An instance of CtClass
represents a class.
It is obtained from ClassPool
.
ClassPool.get(String)
Field Summary | |
static CtClass |
booleanType
The CtClass object representing
the boolean type. |
static CtClass |
byteType
The CtClass object representing
the byte type. |
static CtClass |
charType
The CtClass object representing
the char type. |
static CtClass |
doubleType
The CtClass object representing
the double type. |
static CtClass |
floatType
The CtClass object representing
the float type. |
static CtClass |
intType
The CtClass object representing
the int type. |
static CtClass |
longType
The CtClass object representing
the long type. |
static CtClass |
shortType
The CtClass object representing
the short type. |
static java.lang.String |
version
The version number of this release. |
static CtClass |
voidType
The CtClass object representing
the void type. |
Method Summary | |
void |
addConstructor(CtConstructor c)
Adds a constructor. |
void |
addField(CtField f)
Adds a field. |
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. |
void |
defrost()
Defrosts the class so that the class can be modified again. |
void |
detach()
Removes this CtClass object from the
ClassPool . |
javassist.compiler.AccessorMaker |
getAccessorMaker()
Undocumented method. |
byte[] |
getAttribute(java.lang.String name)
Obtains an attribute with the given name. |
ClassFile |
getClassFile()
Returns a class file for this class. |
ClassFile |
getClassFile2()
Undocumented method. |
CtConstructor |
getClassInitializer()
Gets the class initializer (static constructor) declared in the class. |
ClassPool |
getClassPool()
Returns a ClassPool for this class. |
CtClass |
getComponentType()
If this object represents an array, this method returns the component type of the array. |
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 public constructors of the class. |
CtBehavior[] |
getDeclaredBehaviors()
Gets all the constructors and methods declared in the class. |
CtConstructor |
getDeclaredConstructor(CtClass[] params)
Returns a constructor receiving the specified parameters. |
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[] |
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. |
CtClass |
getEnclosingClass()
Returns the immediately enclosing class of this class. |
CtField |
getField(java.lang.String name)
Returns the field with the specified name. |
CtField[] |
getFields()
Returns an array containing CtField objects
representing all the public 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. |
CtMethod |
getMethod(java.lang.String name,
java.lang.String desc)
Returns the method with the given name and signature. |
CtMethod[] |
getMethods()
Returns an array containing CtMethod objects
representing all the public methods of the class. |
int |
getModifiers()
Returns the modifiers for this class, encoded in an integer. |
java.lang.String |
getName()
Obtains the fully-qualified name of the class. |
java.lang.String |
getPackageName()
Obtains the package name. |
java.util.Collection |
getRefClasses()
Returns a collection of the names of all the classes referenced in this class. |
java.lang.String |
getSimpleName()
Obtains the not-qualified class name. |
CtClass |
getSuperclass()
Obtains the class object representing the superclass of the class. |
java.net.URL |
getURL()
Returns the uniform resource locator (URL) of the class file. |
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 |
isArray()
Returns true if this object represents an array type. |
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. |
boolean |
isPrimitive()
Returns true if this object represents a primitive
Java type: boolean, byte, char, short, int, long, float, double,
or void. |
static void |
main(java.lang.String[] args)
Prints the version number and the copyright notice. |
CtConstructor |
makeClassInitializer()
Makes an empty class initializer (static constructor). |
CtClass |
makeNestedClass(java.lang.String name,
boolean isStatic)
Makes a new nested class. |
void |
removeConstructor(CtConstructor c)
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 map)
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. |
void |
setAttribute(java.lang.String name,
byte[] data)
Adds a named attribute. |
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. |
void |
stopPruning(boolean stop)
Disallows (or allows) pruning the data structure on memory when this CtClass object is converted into a class file. |
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 . |
byte[] |
toBytecode()
Converts this class to a class file. |
void |
toBytecode(java.io.DataOutputStream out)
Converts this class to a class file. |
java.lang.Class |
toClass()
Converts this class to a java.lang.Class object. |
java.lang.Class |
toClass(java.lang.ClassLoader loader)
Converts this class to a java.lang.Class object. |
java.lang.String |
toString()
Converts the object to a string. |
void |
writeFile()
Writes a class file represented by this CtClass
object in the current directory. |
void |
writeFile(java.lang.String directoryName)
Writes a class file represented by this CtClass
object on a local disk. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String version
public static CtClass booleanType
CtClass
object representing
the boolean
type.
public static CtClass charType
CtClass
object representing
the char
type.
public static CtClass byteType
CtClass
object representing
the byte
type.
public static CtClass shortType
CtClass
object representing
the short
type.
public static CtClass intType
CtClass
object representing
the int
type.
public static CtClass longType
CtClass
object representing
the long
type.
public static CtClass floatType
CtClass
object representing
the float
type.
public static CtClass doubleType
CtClass
object representing
the double
type.
public static CtClass voidType
CtClass
object representing
the void
type.
Method Detail |
public static void main(java.lang.String[] args)
The following command invokes this method:
java -jar javassist.jar
public java.lang.String toString()
public ClassPool getClassPool()
ClassPool
for this class.
public ClassFile getClassFile()
This method is not available if isFrozen()
is true.
public ClassFile getClassFile2()
public javassist.compiler.AccessorMaker getAccessorMaker()
public java.net.URL getURL() throws NotFoundException
NotFoundException
public boolean isModified()
public boolean isFrozen()
defrost()
public void defrost()
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.
isFrozen()
,
stopPruning(boolean)
public void stopPruning(boolean stop)
CtClass
object is converted into a class file.
Pruning saves memory space since a ClassPool
holds
all instances of CtClass
all the time of program execution.
However, pruning discards the data representing part of the
class definition, such as method bodies.
Therefore, once it is pruned, toBytecode()
,
writeFile()
, or toClass()
cannot
be called again.
Initially, pruning is allowed.
stop
- disallow pruning if true. Otherwise, allow.detach()
,
toBytecode()
,
toClass()
,
writeFile()
public boolean isPrimitive()
true
if this object represents a primitive
Java type: boolean, byte, char, short, int, long, float, double,
or void.
public boolean isArray()
true
if this object represents an array type.
public CtClass getComponentType() throws NotFoundException
null
.
NotFoundException
public boolean subtypeOf(CtClass clazz) throws NotFoundException
true
if this class extends or implements
clazz
. It also returns true
if
this class is the same as clazz
.
NotFoundException
public java.lang.String getName()
public final java.lang.String getSimpleName()
public final java.lang.String getPackageName()
null
.
public void setName(java.lang.String name)
name
- fully-qualified namepublic void replaceClassName(java.lang.String oldName, java.lang.String newName)
newName
for all occurrences of a class
name oldName
in the class file.
oldName
- replaced class namenewName
- substituted class namepublic void replaceClassName(ClassMap map)
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.
map
- the hashtable associating replaced class names
with substituted names.public java.util.Collection getRefClasses()
This method may return null
.
public boolean isInterface()
true
if this object represents an interface.
public int getModifiers()
javassist.Modifier
.
Modifier
public void setModifiers(int mod)
mod
- modifiers encoded by
javassist.Modifier
Modifier
public boolean subclassOf(CtClass superclass)
This method returns true if the given class is identical to the class represented by this object.
public CtClass getSuperclass() throws NotFoundException
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()
.
NotFoundException
public void setSuperclass(CtClass clazz) throws CannotCompileException
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.
CannotCompileException
public CtClass[] getInterfaces() throws NotFoundException
NotFoundException
public void setInterfaces(CtClass[] list)
list
- a list of the CtClass
objects
representing interfaces, or
null
if the class implements
no interfaces.public void addInterface(CtClass anInterface)
anInterface
- the added interface.public CtClass getDeclaringClass() throws NotFoundException
NotFoundException
public CtClass getEnclosingClass() throws NotFoundException
NotFoundException
public CtClass makeNestedClass(java.lang.String name, boolean isStatic)
CtClass
.
The current implementation only supports a static nested class.
isStatic
must be true.
name
- the simple name of the nested class.isStatic
- true if the nested class is static.public CtField[] getFields()
CtField
objects
representing all the public fields of the class.
That array includes public fields inherited from the
superclasses.
public CtField getField(java.lang.String name) throws NotFoundException
NotFoundException
public CtField[] getDeclaredFields()
Note: the result does not include inherited fields.
public CtField getDeclaredField(java.lang.String name) throws NotFoundException
Note: this method does not search the superclasses.
NotFoundException
public CtBehavior[] getDeclaredBehaviors()
public CtConstructor[] getConstructors()
CtConstructor
objects
representing all the public constructors of the class.
public CtConstructor getConstructor(java.lang.String desc) throws NotFoundException
javassist.bytecode.Descriptor
.
desc
- method descriptor
NotFoundException
Descriptor
public CtConstructor[] getDeclaredConstructors()
CtConstructor
public CtConstructor getDeclaredConstructor(CtClass[] params) throws NotFoundException
params
- parameter types.
NotFoundException
public CtConstructor getClassInitializer()
null
if
no class initializer is not declared.
makeClassInitializer()
,
CtConstructor
public CtMethod[] getMethods()
CtMethod
objects
representing all the public methods of the class.
That array includes public methods inherited from the
superclasses.
public CtMethod getMethod(java.lang.String name, java.lang.String desc) throws NotFoundException
name
- method namedesc
- method descriptor
NotFoundException
Descriptor
public CtMethod[] getDeclaredMethods()
CtMethod
public CtMethod getDeclaredMethod(java.lang.String name, CtClass[] params) throws NotFoundException
Note: this method does not search the superclasses.
name
- method nameparams
- parameter types
NotFoundException
CtMethod
public CtMethod getDeclaredMethod(java.lang.String name) throws NotFoundException
Note: this method does not search the superclasses.
NotFoundException
CtMethod
public CtConstructor makeClassInitializer() throws CannotCompileException
CannotCompileException
getClassInitializer()
public void addConstructor(CtConstructor c) throws CannotCompileException
makeClassInitializer()
.
CannotCompileException
makeClassInitializer()
public void removeConstructor(CtConstructor c) throws NotFoundException
c
- removed constructor.
NotFoundException
- if the constructor is not found.public void addMethod(CtMethod m) throws CannotCompileException
CannotCompileException
public void removeMethod(CtMethod m) throws NotFoundException
m
- removed method.
NotFoundException
- if the method is not found.public void addField(CtField f) throws CannotCompileException
The CtField
belonging to another
CtClass
cannot be directly added to this class.
Only a field created for this class can be added.
CannotCompileException
CtField.CtField(CtField,CtClass)
public void addField(CtField f, java.lang.String init) throws CannotCompileException
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
.
init
- an expression for the initial value.
CannotCompileException
CtField.Initializer.byExpr(String)
,
CtField.CtField(CtField,CtClass)
public void addField(CtField f, CtField.Initializer init) throws CannotCompileException
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.
init
- specifies the initial value of the field.
CannotCompileException
CtField.CtField(CtField,CtClass)
public void removeField(CtField f) throws NotFoundException
f
- removed field.
NotFoundException
- if the field is not found.public byte[] getAttribute(java.lang.String name)
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()
name
- attribute nameAttributeInfo
public void setAttribute(java.lang.String name, byte[] data)
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 of a class file.
getClassFile().addAttribute(info)
name
- attribute namedata
- attribute valueAttributeInfo
public void instrument(CodeConverter converter) throws CannotCompileException
instrument()
on every CtMethod
and CtConstructor
object
in the class.
converter
- specifies how to modify.
CannotCompileException
public void instrument(ExprEditor editor) throws CannotCompileException
instrument()
on every CtMethod
and CtConstructor
object
in the class.
editor
- specifies how to modify.
CannotCompileException
public java.lang.Class toClass() throws CannotCompileException
java.lang.Class
object.
Once this method is called, further modifications are not
allowed any more.
To load the class, this method uses the context class loader
of the current thread. If the program is running on some application
server, the context class loader might be inappropriate to load the
class.
This method is provided for convenience. If you need more complex functionality, you should write your own class loader.
Note: this method calls toClass()
in ClassPool
.
CannotCompileException
toClass(java.lang.ClassLoader)
,
ClassPool.toClass(CtClass)
public java.lang.Class toClass(java.lang.ClassLoader loader) throws CannotCompileException
java.lang.Class
object.
Once this method is called, further modifications are not allowed
any more.
The class file represented by this CtClass
is
loaded by the given class loader to construct a
java.lang.Class
object. Since a private method
on the class loader is invoked through the reflection API,
the caller must have permissions to do that.
This method is provided for convenience. If you need more complex functionality, you should write your own class loader.
Note: this method calls toClass()
in ClassPool
.
loader
- the class loader used to load this class.
CannotCompileException
ClassPool.toClass(CtClass,java.lang.ClassLoader)
public void detach()
CtClass
object from the
ClassPool
.
After this method is called, any method cannot be called on the
removed CtClass
object.
If get()
in ClassPool
is called
with the name of the removed method,
the ClassPool
will read the class file again
and constructs another CtClass
object representing
the same class.
public byte[] toBytecode() throws java.io.IOException, CannotCompileException
java.io.IOException
CannotCompileException
public void writeFile() throws NotFoundException, java.io.IOException, CannotCompileException
CtClass
object in the current directory.
Once this method is called, further modifications are not
possible any more.
NotFoundException
java.io.IOException
CannotCompileException
public void writeFile(java.lang.String directoryName) throws NotFoundException, CannotCompileException, java.io.IOException
CtClass
object on a local disk.
Once this method is called, further modifications are not
possible any more.
directoryName
- it must end without a directory separator.
NotFoundException
CannotCompileException
java.io.IOException
public void toBytecode(java.io.DataOutputStream out) throws CannotCompileException, java.io.IOException
This method dose not close the output stream in the end.
out
- the output stream that a class file is written to.
CannotCompileException
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |