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 CtClasses
that have been created so that the consistency among modified classes
can be guaranteed. Thus if a large number of CtClasses
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:
ClassPools can make a parent-child hierarchy as
java.lang.ClassLoaders. 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 ClassPaths of
the child ClassPool. This search order is reversed if
ClassPath.childFirstLookup is true.
javassist.CtClass , javassist.ClassPath
| Modifier and Type | Field and Description |
|---|---|
| public static boolean | cacheOpenedJarFile
If true, the contents of a jar file are cached after the jar file is opened. |
| private Hashtable | cflow
Table of registered cflow variables. |
| public boolean | childFirstLookup
Determines the search order. |
| protected Hashtable | |
| private static final int | |
| private int | |
| private static ClassPool | |
| public static boolean | doPruning
Turning the automatic pruning on/off. |
| private ArrayList | |
| private static final int | |
| protected ClassPool | |
| public static boolean | releaseUnmodifiedClassFile
If true, unmodified and not-recently-used class files are periodically released for saving memory. |
| protected ClassPoolTail |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ClassPool(boolean
true if the system search path is
appended. useDefaultPath)Creates a root class pool. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public ClassPath | Returns: the appended class path.Appends a |
| public ClassPath | Returns: the appended class path.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. pathname)Appends a directory or a jar (or zip) file to the end of the search path. |
| public void | appendPathList(String
a (semi)colon-separated list of
the path names of directories and jar files.
The directory name must not end with a path
separator ("/"). pathlist)Appends directories and jar files for search. |
| public ClassPath | Returns: the appended class path.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. |
| pack-priv CtClass | |
| pack-priv void | |
| pack-priv synchronized void | |
| public void | |
| pack-priv void | |
| protected CtClass | Returns: null if the class file could not be found.Creates a CtClass object representing the specified class. |
| public URL | |
| public CtClass | |
| public CtClass[] | |
| protected synchronized CtClass | |
| public CtClass | getAndRename(String
the original (fully-qualified) class name orgName, String the new class name newName)Reads a class file and constructs a |
| protected CtClass | |
| public ClassLoader | Returns: the classloader for the poolGet the classloader for The default is the context class loader. |
| pack-priv static ClassLoader | |
| public CtClass | getCtClass(String
a fully-qualified class name or a descriptor
representing an array type. classname)Returns a |
| public static synchronized ClassPool | |
| public Iterator | |
| public CtMethod | |
| public CtClass | |
| public void | importPackage(String
the package name.
It must not include the last '.' (dot).
For example, "java.util" is valid but "java.util." is wrong. packageName)Record a package name so that the Javassist compiler searches the package to resolve a class name. |
| public ClassPath | Returns: the inserted class path.Insert a |
| public ClassPath | Returns: the inserted class path.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. pathname)Inserts a directory or a jar (or zip) file at the head of the search path. |
| public Object[] | |
| public CtClass | makeAnnotation(String
a fully-qualified interface name.
Or null if the annotation has no super interface. name)Creates a new annotation. |
| public CtClass | makeClass(InputStream
class file. classfile)Creates a new class (or interface) from the given class file. |
| public CtClass | makeClass(InputStream
class file. classfile, boolean throws a RuntimeException if this parameter is true
and there is a frozen class with the same name. ifNotFrozen)Creates a new class (or interface) from the given class file. |
| public CtClass | |
| public CtClass | |
| public CtClass | |
| public synchronized CtClass | |
| public CtClass | makeClassIfNew(InputStream
the class file. classfile)Creates a new class (or interface) from the given class file. |
| public CtClass | |
| public synchronized CtClass | makeInterface(String
a fully-qualified interface name. name, CtClass the super interface. superclass)Creates a new public interface. |
| pack-priv synchronized CtClass | Returns: the nested class.a fully-qualified class name. classname)Creates a new public nested class. |
| public void | makePackage(ClassLoader
the class loader passed to loader, String toClass() or
the default one obtained by getClassLoader().the package name. name)
Deprecated
Defines a new package. |
| pack-priv InputStream | |
| pack-priv void | recordCflow(String
variable name name, String class name cname, String field name fname)Records the |
| public void | recordInvalidClassName(String
an invalid class name (separeted by dots). name)
Deprecated
Records a class name that never exists. |
| protected CtClass | removeCached(String classname)
Provide a hook so that subclasses can do their own caching of classes. |
| public void | |
| public Class | |
| public Class | toClass(CtClass ct, ClassLoader loader)
Deprecated
Replaced by
toClass(CtClass, Class, ClassLoader, ProtectionDomain).
Converts the class to a |
| public Class | toClass(CtClass
the class converted into ct, ClassLoader java.lang.Class.the class loader used to load this class.
For example, the loader returned by
loader, ProtectionDomain getClassLoader() can be used
for this parameter.the protection domain for the class.
If it is null, the default domain created
by domain)java.lang.ClassLoader is used.
Deprecated
Replaced by
toClass(CtClass, Class, ClassLoader, ProtectionDomain).
Converts the class to a |
| public Class | |
| public Class | toClass(CtClass
the class converted into ct, MethodHandles.java.lang.Class.Converts the class to a |
| public Class | toClass(CtClass
the class converted into ct, Class<?> java.lang.Class.a class belonging to the same package that
the converted class belongs to.
It can be null. neighbor, ClassLoader the class loader used to load this class.
For example, the loader returned by
loader, ProtectionDomain getClassLoader() can be used
for this parameter.the protection domain for the class.
If it is null, the default domain created
by domain)java.lang.ClassLoader is used.Converts the class to a |
| public String | |
| pack-priv void |
| cacheOpenedJarFile | back to summary |
|---|---|
| public static boolean cacheOpenedJarFile If true, the contents of a jar file are cached after the jar file is opened. The initial value is true. | |
| cflow | back to summary |
|---|---|
| private Hashtable<K, V> cflow Table of registered cflow variables. | |
| childFirstLookup | back to summary |
|---|---|
| public boolean childFirstLookup Determines the search order. If this field is true, The default value is false. | |
| classes | back to summary |
|---|---|
| protected Hashtable<K, V> classes | |
| COMPRESS_THRESHOLD | back to summary |
|---|---|
| private static final int COMPRESS_THRESHOLD | |
| compressCount | back to summary |
|---|---|
| private int compressCount | |
| defaultPool | back to summary |
|---|---|
| private static ClassPool defaultPool | |
| doPruning | back to summary |
|---|---|
| public static boolean doPruning Turning the automatic pruning on/off. If this field is true, The initial value is false. | |
| importedPackages | back to summary |
|---|---|
| private ArrayList<E> importedPackages | |
| INIT_HASH_SIZE | back to summary |
|---|---|
| private static final int INIT_HASH_SIZE | |
| parent | back to summary |
|---|---|
| protected ClassPool parent | |
| releaseUnmodifiedClassFile | back to summary |
|---|---|
| public static boolean releaseUnmodifiedClassFile If true, unmodified and not-recently-used class files are periodically released for saving memory. The initial value is true. | |
| source | back to summary |
|---|---|
| protected ClassPoolTail source | |
| ClassPool | back to summary |
|---|---|
| public ClassPool() Creates a root class pool. No parent class pool is specified. | |
| ClassPool | back to summary |
|---|---|
| public ClassPool(boolean useDefaultPath) Creates a root class pool. If
| |
| ClassPool | back to summary |
|---|---|
| public ClassPool(ClassPool parent) Creates a class pool.
| |
| appendClassPath | back to summary |
|---|---|
| public ClassPath appendClassPath(ClassPath cp) Appends a
| |
| appendClassPath | back to summary |
|---|---|
| public ClassPath appendClassPath(String pathname) throws NotFoundException Appends a directory or a jar (or zip) file to the end of the search path.
| |
| appendPathList | back to summary |
|---|---|
| public void appendPathList(String pathlist) throws NotFoundException Appends directories and jar files for search. The elements of the given path list must be separated by colons in Unix or semi-colons in Windows.
| |
| appendSystemPath | back to summary |
|---|---|
| public ClassPath appendSystemPath() Appends the system search path to the end of the
search path. The system search path
usually includes the platform library, extension
libraries, and the search path specified by the
| |
| cacheCtClass | back to summary |
|---|---|
| protected void cacheCtClass(String classname, CtClass c, boolean dynamic) Provides a hook so that subclasses can do their own caching of classes.
| |
| checkNotExists | back to summary |
|---|---|
| pack-priv CtClass checkNotExists(String classname) | |
| checkNotFrozen | back to summary |
|---|---|
| pack-priv void checkNotFrozen(String classname) throws RuntimeException | |
| classNameChanged | back to summary |
|---|---|
| pack-priv synchronized void classNameChanged(String oldname, CtClass clazz) | |
| clearImportedPackages | back to summary |
|---|---|
| public void clearImportedPackages() Clear all the package names recorded by
| |
| compress | back to summary |
|---|---|
| pack-priv void compress() This method is periodically invoked so that memory footprint will be minimized. | |
| createCtClass | back to summary |
|---|---|
| protected CtClass createCtClass(String classname, boolean useCache) Creates a CtClass object representing the specified class. It first examines whether or not the corresponding class file exists. If yes, it creates a CtClass object.
| |
| find | back to summary |
|---|---|
| public URL find(String classname) Searches the class path to obtain the URL of the class file specified by classname. It is also used to determine whether the class file exists.
| |
| get | back to summary |
|---|---|
| public CtClass get(String classname) throws NotFoundException Reads a class file from the source and returns a reference
to the If To obtain an inner class, use "$" instead of "." for separating the enclosing class name and the inner class name.
| |
| get | back to summary |
|---|---|
| public CtClass[] get(String[] classnames) throws NotFoundException Reads class files from the source and returns an array of
If an element of
| |
| get0 | back to summary |
|---|---|
protected synchronized CtClass get0(String classname, boolean useCache) throws NotFoundException
| |
| getAndRename | back to summary |
|---|---|
| public CtClass getAndRename(String orgName, String newName) throws NotFoundException Reads a class file and constructs a
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.
| |
| getCached | back to summary |
|---|---|
| protected CtClass getCached(String classname) Provide a hook so that subclasses can do their own caching of classes. | |
| getClassLoader | back to summary |
|---|---|
| public ClassLoader getClassLoader() Get the classloader for The default is the context class loader.
| |
| getContextClassLoader | back to summary |
|---|---|
| pack-priv static ClassLoader getContextClassLoader() Obtains a class loader that seems appropriate to look up a class by name. | |
| getCtClass | back to summary |
|---|---|
| public CtClass getCtClass(String classname) throws NotFoundException Returns a Using this method is not recommended; this method should be
used only to obtain the
| |
| getDefault | back to summary |
|---|---|
| public static synchronized ClassPool getDefault() Returns the default class pool. The returned object is always identical since this method is a singleton factory. The default class pool searches the system search path,
which usually includes the platform library, extension
libraries, and the search path specified by the
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
| |
| getImportedPackages | back to summary |
|---|---|
| public Iterator Returns all the package names recorded by
| |
| getMethod | back to summary |
|---|---|
| public CtMethod getMethod(String classname, String methodname) throws NotFoundException Reads a class file and obtains a compile-time method.
| |
| getOrNull | back to summary |
|---|---|
| public CtClass getOrNull(String classname) Reads a class file from the source and returns a reference
to the
| |
| importPackage | back to summary |
|---|---|
| public void importPackage(String packageName) Record a package name so that the Javassist compiler searches
the package to resolve a class name.
Don't record the Since version 3.14, Note that
| |
| insertClassPath | back to summary |
|---|---|
| public ClassPath insertClassPath(ClassPath cp) Insert a
| |
| insertClassPath | back to summary |
|---|---|
| public ClassPath insertClassPath(String pathname) throws NotFoundException Inserts a directory or a jar (or zip) file at the head of the search path.
| |
| lookupCflow | back to summary |
|---|---|
| public Object[] lookupCflow(String name) Undocumented method. Do not use; internal-use only.
| |
| makeAnnotation | back to summary |
|---|---|
| public CtClass makeAnnotation(String name) throws RuntimeException Creates a new annotation. If there already exists a class/interface with the same name, the new interface overwrites that previous one.
| |
| makeClass | back to summary |
|---|---|
| public CtClass makeClass(InputStream classfile) throws IOException, RuntimeException Creates a new class (or interface) from the given class file. If there already exists a class with the same name, the new class overwrites that previous class. This method is used for creating a
| |
| makeClass | back to summary |
|---|---|
| public CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, RuntimeException Creates a new class (or interface) from the given class file. If there already exists a class with the same name, the new class overwrites that previous class. This method is used for creating a
| |
| makeClass | back to summary |
|---|---|
| public CtClass makeClass(ClassFile classfile) throws RuntimeException Creates a new class (or interface) from the given class file. If there already exists a class with the same name, the new class overwrites that previous class. This method is used for creating a
| |
| makeClass | back to summary |
|---|---|
| public CtClass makeClass(ClassFile classfile, boolean ifNotFrozen) throws RuntimeException Creates a new class (or interface) from the given class file. If there already exists a class with the same name, the new class overwrites that previous class. This method is used for creating a
| |
| makeClass | back to summary |
|---|---|
| public CtClass makeClass(String classname) throws RuntimeException Creates a new public class. If there already exists a class with the same name, the new class overwrites that previous class. 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.
| |
| makeClass | back to summary |
|---|---|
| public synchronized CtClass makeClass(String classname, CtClass superclass) throws RuntimeException Creates a new public class. If there already exists a class/interface with the same name, the new class overwrites that previous class. 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.
| |
| makeClassIfNew | back to summary |
|---|---|
| public CtClass makeClassIfNew(InputStream classfile) throws IOException, RuntimeException Creates a new class (or interface) from the given class file. If there already exists a class with the same name, this method returns the existing class; a new class is never created from the given class file. This method is used for creating a
| |
| makeInterface | back to summary |
|---|---|
| public CtClass makeInterface(String name) throws RuntimeException Creates a new public interface. If there already exists a class/interface with the same name, the new interface overwrites that previous one.
| |
| makeInterface | back to summary |
|---|---|
| public synchronized CtClass makeInterface(String name, CtClass superclass) throws RuntimeException Creates a new public interface. If there already exists a class/interface with the same name, the new interface overwrites that previous one.
| |
| makeNestedClass | back to summary |
|---|---|
| pack-priv synchronized CtClass makeNestedClass(String classname) Creates a new public nested class.
This method is called by | |
| makePackage | back to summary |
|---|---|
| public void makePackage(ClassLoader loader, String name) throws CannotCompileException
Deprecated
Defines a new package. If the package is already defined, this method performs nothing. You do not necessarily need to
call this method. If this method is called, then
The jigsaw module introduced by Java 9 has broken this method.
| |
| openClassfile | back to summary |
|---|---|
| pack-priv InputStream openClassfile(String classname) throws NotFoundException | |
| recordCflow | back to summary |
|---|---|
| pack-priv void recordCflow(String name, String cname, String fname) Records the | |
| recordInvalidClassName | back to summary |
|---|---|
| public void recordInvalidClassName(String name)
Deprecated
Records a class name that never exists.
For example, a package name can be recorded by this method.
This would improve execution performance
since The current implementation of this method performs nothing.
| |
| removeCached | back to summary |
|---|---|
| protected CtClass removeCached(String classname) Provide a hook so that subclasses can do their own caching of classes. | |
| removeClassPath | back to summary |
|---|---|
| public void removeClassPath(ClassPath cp) Detatches the | |
| toClass | back to summary |
|---|---|
| public Class Converts the given class to a This behavior can be changed by subclassing the pool and changing
the This method is provided for convenience. If you need more complex functionality, you should write your own class loader. Warining:
This method should not be used in Java 11 or later.
Use 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. | |
| toClass | back to summary |
|---|---|
| public Class Deprecated Replaced by Converts the class to a 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. | |
| toClass | back to summary |
|---|---|
| public Class Deprecated Replaced by Converts the class to a The class file represented by the given An easy way to obtain This method is provided for convenience. If you need more complex functionality, you should write your own class loader.
| |
| toClass | back to summary |
|---|---|
| public Class Converts the class to a This method is available in Java 9 or later.
It loads the class
by using | |
| toClass | back to summary |
|---|---|
| public Class Converts the class to a This method is available in Java 9 or later.
It loads the class
by using the given
| |
| toClass | back to summary |
|---|---|
| public Class Converts the class to a When the JVM is Java 11 or later, this method loads the class
by using Otherwise, or when An easy way to obtain If your program is for only Java 9 or later, don't use this method.
Use
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns the class search path.
| |
| writeClassfile | back to summary |
|---|---|
| pack-priv void writeClassfile(String classname, OutputStream out) throws NotFoundException, IOException, CannotCompileException | |