public class ClassPool extends Object
| Modifier and Type | Field and Description |
|---|---|
boolean |
childFirstLookup
Determines the search order.
|
protected Hashtable<String,CtClass> |
classes |
static boolean |
doPruning
Turning the automatic pruning on/off.
|
protected ClassPool |
parent |
static boolean |
releaseUnmodifiedClassFile
If true, unmodified and not-recently-used class files are
periodically released for saving memory.
|
protected xapi.bytecode.ClassPoolTail |
source |
| 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(String pathname)
Appends a directory or a jar (or zip) file to the end of the
search path.
|
void |
appendPathList(String pathlist)
Appends directories and jar files for search.
|
ClassPath |
appendSystemPath()
Appends the system search path to the end of the
search path.
|
protected void |
cacheCtClass(String classname,
CtClass c,
boolean dynamic)
Provides a hook so that subclasses can do their own
caching of classes.
|
void |
clearImportedPackages()
Clear all the package names recorded by
importPackage(). |
protected CtClass |
createCtClass(String classname,
boolean useCache)
Creates a CtClass object representing the specified class.
|
URL |
find(String classname)
Searches the class path to obtain the URL of the class file
specified by classname.
|
CtClass |
get(String classname)
Reads a class file from the source and returns a reference
to the
CtClass
object representing that class file. |
CtClass[] |
get(String[] classnames)
Reads class files from the source and returns an array of
CtClass
objects representing those class files. |
protected CtClass |
get0(String classname,
boolean useCache) |
CtClass |
getAndRename(String orgName,
String newName)
Reads a class file and constructs a
CtClass
object with a new name. |
protected CtClass |
getCached(String classname)
Provide a hook so that subclasses can do their own
caching of classes.
|
ClassLoader |
getClassLoader()
Get the classloader for
toClass(), getAnnotations() in
CtClass, etc. |
CtClass |
getCtClass(String classname)
Returns a
CtClass object with the given name. |
static ClassPool |
getDefault()
Returns the default class pool.
|
Iterator<String> |
getImportedPackages()
Returns all the package names recorded by
importPackage(). |
CtClass |
getOrNull(String classname)
Reads a class file from the source and returns a reference
to the
CtClass
object representing that class file. |
void |
importPackage(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(String pathname)
Inserts a directory or a jar (or zip) file at the head of the
search path.
|
CtClass |
makeClass(InputStream classfile)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeClass(InputStream classfile,
boolean ifNotFrozen)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeClass(String classname)
Creates a new public class.
|
CtClass |
makeClass(String classname,
CtClass superclass)
Creates a new public class.
|
CtClass |
makeClassIfNew(InputStream classfile)
Creates a new class (or interface) from the given class file.
|
CtClass |
makeInterface(String name)
Creates a new public interface.
|
CtClass |
makeInterface(String name,
CtClass superclass)
Creates a new public interface.
|
void |
recordInvalidClassName(String name)
Records a name that never exists.
|
protected CtClass |
removeCached(String classname)
Provide a hook so that subclasses can do their own
caching of classes.
|
void |
removeClassPath(ClassPath cp)
Detatches the
ClassPath object from the search path. |
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.
protected xapi.bytecode.ClassPoolTail source
protected ClassPool parent
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.javassist.ClassPool#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,
LoaderClassPathprotected CtClass getCached(String classname)
protected void cacheCtClass(String classname, CtClass c, boolean dynamic)
getCached(String),
#removeCached(String,CtClass)protected CtClass removeCached(String classname)
public String toString()
public void importPackage(String packageName)
java.lang package, which has
been implicitly recorded by default.
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 Iterator<String> getImportedPackages()
importPackage().importPackage(String)public void recordInvalidClassName(String name)
get() does not search 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.name - a class name (separeted by dot).public CtClass getAndRename(String orgName, 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 nameNotFoundExceptionpublic CtClass get(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.NotFoundExceptionpublic CtClass getOrNull(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(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.NotFoundExceptionget(String),
ConstPool.getClassInfo(int),
javassist.bytecode.Descriptor#toCtClass(String, ClassPool)protected CtClass get0(String classname, boolean useCache) throws NotFoundException
useCache - false if the cached CtClass must be ignored.searchParent - false if the parent class pool is not searched.NotFoundExceptionprotected CtClass createCtClass(String classname, boolean useCache)
public URL find(String classname)
classname - a fully-qualified class name.CtClass.getURL()public CtClass[] get(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.NotFoundExceptionpublic CtClass makeClass(InputStream classfile) throws IOException, 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.RuntimeException - if there is a frozen class with the
the same name.IOExceptionmakeClassIfNew(InputStream),
javassist.ByteArrayClassPathpublic CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, 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.IOExceptionRuntimeExceptionjavassist.ByteArrayClassPathpublic CtClass makeClassIfNew(InputStream classfile) throws IOException, 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.IOExceptionRuntimeExceptionmakeClass(InputStream),
javassist.ByteArrayClassPathpublic CtClass makeClass(String classname) throws 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.RuntimeException - if the existing class is frozen.public CtClass makeClass(String classname, CtClass superclass) throws 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.RuntimeException - if the existing class is frozen.public CtClass makeInterface(String name) throws RuntimeException
name - a fully-qualified interface name.RuntimeException - if the existing interface is frozen.public CtClass makeInterface(String name, CtClass superclass) throws RuntimeException
name - a fully-qualified interface name.superclass - the super interface.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.javassist.ClassPath,
javassist.URLClassPath,
javassist.ByteArrayClassPathpublic ClassPath appendClassPath(ClassPath cp)
ClassPath object to the end of the
search path.javassist.ClassPath,
javassist.URLClassPath,
javassist.ByteArrayClassPathpublic ClassPath insertClassPath(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(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 pathagain.public void appendPathList(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 ClassLoader getClassLoader()
toClass(), getAnnotations() in
CtClass, etc.
The default is the context class loader.
#toClass(CtClass),
CtClass.getAnnotations()Copyright © December 07, 2012–2015 The Internet Party. All rights reserved.