public class ClassPool
extends java.lang.Object
CtClass
objects.
A CtClass
object must be obtained from this object.
If get()
is called on this object,
it searches various sources represented by ClassPath
to find a class file and then it creates a CtClass
object
representing that class file. The created object is returned to the
caller.
Memory consumption memo:
ClassPool
objects hold all the CtClass
es
that have been created so that the consistency among modified classes
can be guaranteed. Thus if a large number of CtClass
es
are processed, the ClassPool
will consume a huge amount
of memory. To avoid this, a ClassPool
object
should be recreated, for example, every hundred classes processed.
Note that getDefault()
is a singleton factory.
Otherwise, detach()
in CtClass
should be used
to avoid huge memory consumption.
ClassPool
hierarchy:
ClassPool
s can make a parent-child hierarchy as
java.lang.ClassLoader
s. If a ClassPool
has
a parent pool, get()
first asks the parent pool to find
a class file. Only if the parent could not find the class file,
get()
searches the ClassPath
s of
the child ClassPool
. This search order is reversed if
ClassPath.childFirstLookup
is true
.
Modifier and Type | Field and Description |
---|---|
boolean |
childFirstLookup
Determines the search order.
|
static boolean |
doPruning
Turning the automatic pruning on/off.
|
static boolean |
releaseUnmodifiedClassFile
If true, unmodified and not-recently-used class files are
periodically released for saving memory.
|
Constructor and Description |
---|
ClassPool()
Creates a root class pool.
|
ClassPool(boolean useDefaultPath)
Creates a root class pool.
|
ClassPool(ClassPool parent)
Creates a class pool.
|
Modifier and Type | Method and Description |
---|---|
ClassPath |
appendClassPath(ClassPath cp)
Appends a
ClassPath object to the end of the
search path. |
ClassPath |
appendClassPath(java.lang.String pathname)
Appends a directory or a jar (or zip) file to the end of the
search path.
|
void |
appendPathList(java.lang.String pathlist)
Appends directories and jar files for search.
|
ClassPath |
appendSystemPath()
Appends the system search path to the end of the
search path.
|
void |
clearImportedPackages()
Clear all the package names recorded by
importPackage() . |
java.net.URL |
find(java.lang.String classname)
Searches the class path to obtain the URL of the class file
specified by classname.
|
CtClass |
get(java.lang.String classname)
Reads a class file from the source and returns a reference
to the
CtClass
object representing that class file. |
CtClass[] |
get(java.lang.String[] classnames)
Reads class files from the source and returns an array of
CtClass
objects representing those class files. |
CtClass |
getAndRename(java.lang.String orgName,
java.lang.String newName)
Reads a class file and constructs a
CtClass
object with a new name. |
java.lang.ClassLoader |
getClassLoader()
Get the classloader for
toClass() , getAnnotations() in
CtClass , etc. |
CtClass |
getCtClass(java.lang.String classname)
Returns a
CtClass object with the given name. |
static ClassPool |
getDefault()
Returns the default class pool.
|
java.util.Iterator |
getImportedPackages()
Returns all the package names recorded by
importPackage() . |
CtMethod |
getMethod(java.lang.String classname,
java.lang.String methodname)
Reads a class file and obtains a compile-time method.
|
CtClass |
getOrNull(java.lang.String classname)
Reads a class file from the source and returns a reference
to the
CtClass
object representing that class file. |
void |
importPackage(java.lang.String packageName)
Record a package name so that the Javassist compiler searches
the package to resolve a class name.
|
ClassPath |
insertClassPath(ClassPath cp)
Insert a
ClassPath object at the head of the
search path. |
ClassPath |
insertClassPath(java.lang.String pathname)
Inserts a directory or a jar (or zip) file at the head of the
search path.
|
java.lang.Object[] |
lookupCflow(java.lang.String name)
Undocumented method.
|
CtClass |
makeAnnotation(java.lang.String name)
Creates a new annotation.
|
CtClass |
makeClass(ClassFile classfile)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeClass(ClassFile classfile,
boolean ifNotFrozen)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeClass(java.io.InputStream classfile)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeClass(java.io.InputStream classfile,
boolean ifNotFrozen)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeClass(java.lang.String classname)
Creates a new public class.
|
CtClass |
makeClass(java.lang.String classname,
CtClass superclass)
Creates a new public class.
|
CtClass |
makeClassIfNew(java.io.InputStream classfile)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeInterface(java.lang.String name)
Creates a new public interface.
|
CtClass |
makeInterface(java.lang.String name,
CtClass superclass)
Creates a new public interface.
|
void |
makePackage(java.lang.ClassLoader loader,
java.lang.String name)
Defines a new package.
|
void |
recordInvalidClassName(java.lang.String name)
Deprecated.
|
void |
removeClassPath(ClassPath cp)
Detatches the
ClassPath object from the search path. |
java.lang.Class |
toClass(CtClass clazz)
Converts the given class to a
java.lang.Class object. |
java.lang.Class |
toClass(CtClass ct,
java.lang.ClassLoader loader)
Deprecated.
Replaced by
toClass(CtClass,ClassLoader,ProtectionDomain) .
A subclass of ClassPool that has been
overriding this method should be modified. It should override
toClass(CtClass,ClassLoader,ProtectionDomain) . |
java.lang.Class |
toClass(CtClass ct,
java.lang.ClassLoader loader,
java.security.ProtectionDomain domain)
Converts the class to a
java.lang.Class object. |
java.lang.String |
toString()
Returns the class search path.
|
public boolean childFirstLookup
If this field is true, get()
first searches the
class path associated to this ClassPool
and then
the class path associated with the parent ClassPool
.
Otherwise, the class path associated with the parent is searched
first.
The default value is false.
public static boolean doPruning
If this field is true, CtClass
objects are
automatically pruned by default when toBytecode()
etc.
are called. The automatic pruning can be turned on/off individually
for each CtClass
object.
The initial value is false.
public static boolean releaseUnmodifiedClassFile
The initial value is true.
public ClassPool()
public ClassPool(boolean useDefaultPath)
useDefaultPath
is
true, appendSystemPath()
is called. Otherwise,
this constructor is equivalent to the constructor taking no
parameter.useDefaultPath
- true if the system search path is
appended.public ClassPool(ClassPool parent)
parent
- the parent of this class pool. If this is a root
class pool, this parameter must be null
.getDefault()
public static ClassPool getDefault()
The default class pool searches the system search path,
which usually includes the platform library, extension
libraries, and the search path specified by the
-classpath
option or the CLASSPATH
environment variable.
When this method is called for the first time, the default class pool is created with the following code snippet:
ClassPool cp = new ClassPool(); cp.appendSystemPath();
If the default class pool cannot find any class files,
try ClassClassPath
and LoaderClassPath
.
ClassClassPath
,
LoaderClassPath
public java.lang.String toString()
toString
in class java.lang.Object
public void importPackage(java.lang.String packageName)
java.lang
package, which has
been implicitly recorded by default.
Since version 3.14, packageName
can be a
fully-qualified class name.
Note that get()
in ClassPool
does
not search the recorded package. Only the compiler searches it.
packageName
- the package name.
It must not include the last '.' (dot).
For example, "java.util" is valid but "java.util." is wrong.public void clearImportedPackages()
importPackage()
.
The java.lang
package is not removed.importPackage(String)
public java.util.Iterator getImportedPackages()
importPackage()
.importPackage(String)
public void recordInvalidClassName(java.lang.String name)
get()
quickly throw an exception
without searching the class path at all
if the given name is an invalid name recorded by this method.
Note that searching the class path takes relatively long time.
The current implementation of this method performs nothing.
name
- an invalid class name (separeted by dots).public java.lang.Object[] lookupCflow(java.lang.String name)
name
- the name of $cflow
variablepublic CtClass getAndRename(java.lang.String orgName, java.lang.String newName) throws NotFoundException
CtClass
object with a new name.
This method is useful if you want to generate a new class as a copy
of another class (except the class name). For example,
getAndRename("Point", "Pair")returns a
CtClass
object representing Pair
class. The definition of Pair
is the same as that of
Point
class except the class name since Pair
is defined by reading Point.class
.orgName
- the original (fully-qualified) class namenewName
- the new class nameNotFoundException
public CtClass get(java.lang.String classname) throws NotFoundException
CtClass
object representing that class file. If that class file has been
already read, this method returns a reference to the
CtClass
created when that class file was read at the
first time.
If classname
ends with "[]", then this method
returns a CtClass
object for that array type.
To obtain an inner class, use "$" instead of "." for separating the enclosing class name and the inner class name.
classname
- a fully-qualified class name.NotFoundException
public CtClass getOrNull(java.lang.String classname)
CtClass
object representing that class file.
This method is equivalent to get
except
that it returns null
when a class file is
not found and it never throws an exception.classname
- a fully-qualified class name.CtClass
object or null
.get(String)
,
find(String)
public CtClass getCtClass(java.lang.String classname) throws NotFoundException
CtClass
object with the given name.
This is almost equivalent to get(String)
except
that classname can be an array-type "descriptor" (an encoded
type name) such as [Ljava/lang/Object;
.
Using this method is not recommended; this method should be
used only to obtain the CtClass
object
with a name returned from getClassInfo
in
javassist.bytecode.ClassPool
. getClassInfo
returns a fully-qualified class name but, if the class is an array
type, it returns a descriptor.
classname
- a fully-qualified class name or a descriptor
representing an array type.NotFoundException
get(String)
,
ConstPool.getClassInfo(int)
,
Descriptor.toCtClass(String, ClassPool)
public java.net.URL find(java.lang.String classname)
classname
- a fully-qualified class name.CtClass.getURL()
public CtClass[] get(java.lang.String[] classnames) throws NotFoundException
CtClass
objects representing those class files.
If an element of classnames
ends with "[]",
then this method
returns a CtClass
object for that array type.
classnames
- an array of fully-qualified class name.NotFoundException
public CtMethod getMethod(java.lang.String classname, java.lang.String methodname) throws NotFoundException
classname
- the class namemethodname
- the method nameNotFoundException
CtClass.getDeclaredMethod(String)
public CtClass makeClass(java.io.InputStream classfile) throws java.io.IOException, java.lang.RuntimeException
This method is used for creating a CtClass
object
directly from a class file. The qualified class name is obtained
from the class file; you do not have to explicitly give the name.
classfile
- class file.java.lang.RuntimeException
- if there is a frozen class with the
the same name.java.io.IOException
makeClassIfNew(InputStream)
,
ByteArrayClassPath
public CtClass makeClass(java.io.InputStream classfile, boolean ifNotFrozen) throws java.io.IOException, java.lang.RuntimeException
This method is used for creating a CtClass
object
directly from a class file. The qualified class name is obtained
from the class file; you do not have to explicitly give the name.
classfile
- class file.ifNotFrozen
- throws a RuntimeException if this parameter is true
and there is a frozen class with the same name.java.io.IOException
java.lang.RuntimeException
ByteArrayClassPath
public CtClass makeClass(ClassFile classfile) throws java.lang.RuntimeException
This method is used for creating a CtClass
object
directly from a class file. The qualified class name is obtained
from the class file; you do not have to explicitly give the name.
classfile
- class file.java.lang.RuntimeException
- if there is a frozen class with the
the same name.public CtClass makeClass(ClassFile classfile, boolean ifNotFrozen) throws java.lang.RuntimeException
This method is used for creating a CtClass
object
directly from a class file. The qualified class name is obtained
from the class file; you do not have to explicitly give the name.
classfile
- class file.ifNotFrozen
- throws a RuntimeException if this parameter is true
and there is a frozen class with the same name.java.lang.RuntimeException
public CtClass makeClassIfNew(java.io.InputStream classfile) throws java.io.IOException, java.lang.RuntimeException
This method is used for creating a CtClass
object
directly from a class file. The qualified class name is obtained
from the class file; you do not have to explicitly give the name.
classfile
- the class file.java.io.IOException
java.lang.RuntimeException
makeClass(InputStream)
,
ByteArrayClassPath
public CtClass makeClass(java.lang.String classname) throws java.lang.RuntimeException
If no constructor is explicitly added to the created new class, Javassist generates constructors and adds it when the class file is generated. It generates a new constructor for each constructor of the super class. The new constructor takes the same set of parameters and invokes the corresponding constructor of the super class. All the received parameters are passed to it.
classname
- a fully-qualified class name.java.lang.RuntimeException
- if the existing class is frozen.public CtClass makeClass(java.lang.String classname, CtClass superclass) throws java.lang.RuntimeException
If no constructor is explicitly added to the created new class, Javassist generates constructors and adds it when the class file is generated. It generates a new constructor for each constructor of the super class. The new constructor takes the same set of parameters and invokes the corresponding constructor of the super class. All the received parameters are passed to it.
classname
- a fully-qualified class name.superclass
- the super class.java.lang.RuntimeException
- if the existing class is frozen.public CtClass makeInterface(java.lang.String name) throws java.lang.RuntimeException
name
- a fully-qualified interface name.java.lang.RuntimeException
- if the existing interface is frozen.public CtClass makeInterface(java.lang.String name, CtClass superclass) throws java.lang.RuntimeException
name
- a fully-qualified interface name.superclass
- the super interface.java.lang.RuntimeException
- if the existing interface is frozen.public CtClass makeAnnotation(java.lang.String name) throws java.lang.RuntimeException
name
- a fully-qualified interface name.
Or null if the annotation has no super interface.java.lang.RuntimeException
- if the existing interface is frozen.public ClassPath appendSystemPath()
-classpath
option or the CLASSPATH
environment variable.public ClassPath insertClassPath(ClassPath cp)
ClassPath
object at the head of the
search path.ClassPath
,
URLClassPath
,
ByteArrayClassPath
public ClassPath appendClassPath(ClassPath cp)
ClassPath
object to the end of the
search path.ClassPath
,
URLClassPath
,
ByteArrayClassPath
public ClassPath insertClassPath(java.lang.String pathname) throws NotFoundException
pathname
- the path name of the directory or jar file.
It must not end with a path separator ("/").
If the path name ends with "/*", then all the
jar files matching the path name are inserted.NotFoundException
- if the jar file is not found.public ClassPath appendClassPath(java.lang.String pathname) throws NotFoundException
pathname
- the path name of the directory or jar file.
It must not end with a path separator ("/").
If the path name ends with "/*", then all the
jar files matching the path name are appended.NotFoundException
- if the jar file is not found.public void removeClassPath(ClassPath cp)
ClassPath
object from the search path.
The detached ClassPath
object cannot be added
to the path again.public void appendPathList(java.lang.String pathlist) throws NotFoundException
The elements of the given path list must be separated by colons in Unix or semi-colons in Windows.
pathlist
- a (semi)colon-separated list of
the path names of directories and jar files.
The directory name must not end with a path
separator ("/").NotFoundException
- if a jar file is not found.public java.lang.Class toClass(CtClass clazz) 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. It is obtained by calling
getClassLoader()
.
This behavior can be changed by subclassing the pool and changing
the getClassLoader()
method.
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.
Warining: A Class object returned by this method may not work with a security manager or a signed jar file because a protection domain is not specified.
public java.lang.ClassLoader getClassLoader()
toClass()
, getAnnotations()
in
CtClass
, etc.
The default is the context class loader.
toClass(CtClass)
,
CtClass.getAnnotations()
public java.lang.Class toClass(CtClass ct, java.lang.ClassLoader loader) throws CannotCompileException
toClass(CtClass,ClassLoader,ProtectionDomain)
.
A subclass of ClassPool
that has been
overriding this method should be modified. It should override
toClass(CtClass,ClassLoader,ProtectionDomain)
.java.lang.Class
object.
Do not override this method any more at a subclass because
toClass(CtClass)
never calls this method.
Warining: A Class object returned by this method may not work with a security manager or a signed jar file because a protection domain is not specified.
CannotCompileException
public java.lang.Class toClass(CtClass ct, java.lang.ClassLoader loader, java.security.ProtectionDomain domain) throws CannotCompileException
java.lang.Class
object.
Once this method is called, further modifications are not allowed
any more.
The class file represented by the given 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.
An easy way to obtain ProtectionDomain
object is
to call getProtectionDomain()
in java.lang.Class
. It returns the domain that the
class belongs to.
This method is provided for convenience. If you need more complex functionality, you should write your own class loader.
loader
- the class loader used to load this class.
For example, the loader returned by
getClassLoader()
can be used
for this parameter.domain
- the protection domain for the class.
If it is null, the default domain created
by java.lang.ClassLoader
is used.CannotCompileException
getClassLoader()
public void makePackage(java.lang.ClassLoader loader, java.lang.String name) throws CannotCompileException
You do not necessarily need to
call this method. If this method is called, then
getPackage()
on the Class
object returned
by toClass()
will return a non-null object.
loader
- the class loader passed to toClass()
or
the default one obtained by getClassLoader()
.name
- the package name.CannotCompileException
getClassLoader()
,
toClass(CtClass)
,
CtClass.toClass()
Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.