Top Description Fields Constructors Methods
javassist

public Class ClassPool

extends Object
Class Inheritance
Known Direct Subclasses
javassist.scopedpool.ScopedClassPool
Annotations
@SuppressWarnings:unchecked, rawtypes
Imports
java.io.BufferedInputStream, .File, .IOException, .InputStream, .OutputStream, java.net.URL, java.security.ProtectionDomain, java.util.ArrayList, .Enumeration, .Hashtable, .Iterator, javassist.bytecode.ClassFile, .Descriptor, javassist.util.proxy.DefinePackageHelper

A container of 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.

See Also
javassist.CtClass, javassist.ClassPath

Field Summary

Modifier and TypeField and Description
public static boolean
cacheOpenedJarFile

If true, the contents of a jar file are cached after the jar file is opened.

private Hashtable<K, V>
cflow

Table of registered cflow variables.

public boolean
childFirstLookup

Determines the search order.

protected Hashtable<K, V>
private static final int
private int
private static ClassPool
public static boolean
doPruning

Turning the automatic pruning on/off.

private ArrayList<E>
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

Constructor Summary

AccessConstructor and Description
public
ClassPool()

Creates a root class pool.

public
ClassPool(boolean
true if the system search path is appended.
useDefaultPath
)

Creates a root class pool.

public
ClassPool(ClassPool
the parent of this class pool. If this is a root class pool, this parameter must be null.
parent
)

Creates a class pool.

Method Summary

Modifier and TypeMethod and Description
public ClassPath

Returns:

the appended class path.
appendClassPath
(ClassPath cp)

Appends a ClassPath object to the end of the search path.

public ClassPath

Returns:

the appended class path.
appendClassPath
(String
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.
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.

pack-priv CtClass
pack-priv void
pack-priv synchronized void
public void
clearImportedPackages()

Clear all the package names recorded by importPackage().

pack-priv void
compress()

This method is periodically invoked so that memory footprint will be minimized.

protected CtClass

Returns:

null if the class file could not be found.
createCtClass
(String classname, boolean useCache)

Creates a CtClass object representing the specified class.

public URL

Returns:

null if the class file could not be found.
find
(String
a fully-qualified class name.
classname
)

Searches the class path to obtain the URL of the class file specified by classname.

public CtClass
get(String
a fully-qualified class name.
classname
)

Reads a class file from the source and returns a reference to the CtClass object representing that class file.

public CtClass[]
get(String[]
an array of fully-qualified class name.
classnames
)

Reads class files from the source and returns an array of CtClass objects representing those class files.

protected synchronized CtClass

Returns:

null if the class could not be found.
get0
(String classname, boolean
false if the cached CtClass must be ignored.
useCache
)

public CtClass
getAndRename(String
the original (fully-qualified) class name
orgName
,
String
the new class name
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.

public ClassLoader

Returns:

the classloader for the pool
getClassLoader
()

Get the classloader for toClass(), getAnnotations() in CtClass, etc.

The default is the context class loader.

pack-priv static ClassLoader
getContextClassLoader()

Obtains a class loader that seems appropriate to look up a class by name.

public CtClass
getCtClass(String
a fully-qualified class name or a descriptor representing an array type.
classname
)

Returns a CtClass object with the given name.

public static synchronized ClassPool
getDefault()

Returns the default class pool.

public Iterator<String>
getImportedPackages()

Returns all the package names recorded by importPackage().

public CtMethod
getMethod(String
the class name
classname
,
String
the method name
methodname
)

Reads a class file and obtains a compile-time method.

public CtClass

Returns:

a CtClass object or null.
getOrNull
(String
a fully-qualified class name.
classname
)

Reads a class file from the source and returns a reference to the CtClass object representing that class file.

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.
insertClassPath
(ClassPath cp)

Insert a ClassPath object at the head of the search path.

public ClassPath

Returns:

the inserted class path.
insertClassPath
(String
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[]
lookupCflow(String
the name of $cflow variable
name
)

Undocumented method.

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
makeClass(ClassFile
class file.
classfile
)

Creates a new class (or interface) from the given class file.

public CtClass
makeClass(ClassFile
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
makeClass(String
a fully-qualified class name.
classname
)

Creates a new public class.

public synchronized CtClass
makeClass(String
a fully-qualified class name.
classname
,
CtClass
the super class.
superclass
)

Creates a new public class.

public CtClass
makeClassIfNew(InputStream
the class file.
classfile
)

Creates a new class (or interface) from the given class file.

public CtClass
makeInterface(String
a fully-qualified interface name.
name
)

Creates a new public interface.

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.
makeNestedClass
(String
a fully-qualified class name.
classname
)

Creates a new public nested class.

public void
makePackage(ClassLoader
the class loader passed to toClass() or the default one obtained by getClassLoader().
loader
,
String
the package name.
name
)
Deprecated

Defines a new package.

pack-priv InputStream
openClassfile(String classname)

pack-priv void
recordCflow(String
variable name
name
,
String
class name
cname
,
String
field name
fname
)

Records the $cflow variable for the field specified by cname and fname.

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
removeClassPath(ClassPath cp)

Detatches the ClassPath object from the search path.

public Class<T>
toClass(CtClass clazz)

Converts the given class to a java.lang.Class object.

public Class<T>
toClass(CtClass ct, ClassLoader loader)

Converts the class to a java.lang.Class object.

public Class<T>
toClass(CtClass
the class converted into java.lang.Class.
ct
,
ClassLoader
the class loader used to load this class. For example, the loader returned by getClassLoader() can be used for this parameter.
loader
,
ProtectionDomain
the protection domain for the class. If it is null, the default domain created by java.lang.ClassLoader is used.
domain
)

Converts the class to a java.lang.Class object.

public Class<?>
toClass(CtClass
the class converted into java.lang.Class.
ct
,
Class<?>
a class belonging to the same package that the converted class belongs to.
neighbor
)

Converts the class to a java.lang.Class object.

public Class<?>
toClass(CtClass
the class converted into java.lang.Class.
ct
,
MethodHandles.Lookup lookup)

Converts the class to a java.lang.Class object.

public Class<T>
toClass(CtClass
the class converted into java.lang.Class.
ct
,
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 getClassLoader() can be used for this parameter.
loader
,
ProtectionDomain
the protection domain for the class. If it is null, the default domain created by java.lang.ClassLoader is used.
domain
)

Converts the class to a java.lang.Class object.

public String
toString()

Overrides java.lang.Object.toString.

Returns the class search path.

pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cacheOpenedJarFileback 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.

cflowback to summary
private Hashtable<K, V> cflow

Table of registered cflow variables.

childFirstLookupback to summary
public boolean childFirstLookup

Determines the search order.

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.

classesback to summary
protected Hashtable<K, V> classes
COMPRESS_THRESHOLDback to summary
private static final int COMPRESS_THRESHOLD
compressCountback to summary
private int compressCount
defaultPoolback to summary
private static ClassPool defaultPool
doPruningback to summary
public static boolean doPruning

Turning the automatic pruning on/off.

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.

See Also
CtClass#prune(), CtClass#stopPruning(boolean), CtClass#detach()
importedPackagesback to summary
private ArrayList<E> importedPackages
INIT_HASH_SIZEback to summary
private static final int INIT_HASH_SIZE
parentback to summary
protected ClassPool parent
releaseUnmodifiedClassFileback 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.

sourceback to summary
protected ClassPoolTail source

Constructor Detail

ClassPoolback to summary
public ClassPool()

Creates a root class pool. No parent class pool is specified.

ClassPoolback to summary
public ClassPool(boolean useDefaultPath)

Creates a root class pool. If useDefaultPath is true, appendSystemPath() is called. Otherwise, this constructor is equivalent to the constructor taking no parameter.

Parameters
useDefaultPath:boolean

true if the system search path is appended.

ClassPoolback to summary
public ClassPool(ClassPool parent)

Creates a class pool.

Parameters
parent:ClassPool

the parent of this class pool. If this is a root class pool, this parameter must be null.

See Also
javassist.ClassPool#getDefault()

Method Detail

appendClassPathback to summary
public ClassPath appendClassPath(ClassPath cp)

Appends a ClassPath object to the end of the search path.

Returns:ClassPath

the appended class path.

See Also
javassist.ClassPath, javassist.URLClassPath, javassist.ByteArrayClassPath
appendClassPathback 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.

Parameters
pathname:String

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.

Returns:ClassPath

the appended class path.

Exceptions
NotFoundException:
if the jar file is not found.
appendPathListback 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.

Parameters
pathlist: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 ("/").

Exceptions
NotFoundException:
if a jar file is not found.
appendSystemPathback 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 -classpath option or the CLASSPATH environment variable.

Returns:ClassPath

the appended class path.

cacheCtClassback to summary
protected void cacheCtClass(String classname, CtClass c, boolean dynamic)

Provides a hook so that subclasses can do their own caching of classes.

See Also
getCached(String), removeCached(String)
checkNotExistsback to summary
pack-priv CtClass checkNotExists(String classname)
checkNotFrozenback to summary
pack-priv void checkNotFrozen(String classname) throws RuntimeException
classNameChangedback to summary
pack-priv synchronized void classNameChanged(String oldname, CtClass clazz)
clearImportedPackagesback to summary
public void clearImportedPackages()

Clear all the package names recorded by importPackage(). The java.lang package is not removed.

Since
3.1
See Also
importPackage(String)
compressback to summary
pack-priv void compress()

This method is periodically invoked so that memory footprint will be minimized.

createCtClassback 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.

Returns:CtClass

null if the class file could not be found.

findback 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.

Parameters
classname:String

a fully-qualified class name.

Returns:URL

null if the class file could not be found.

See Also
CtClass#getURL()
getback to summary
public CtClass get(String classname) throws NotFoundException

Reads a class file from the source and returns a reference to the 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.

Parameters
classname:String

a fully-qualified class name.

getback to summary
public CtClass[] get(String[] classnames) throws NotFoundException

Reads class files from the source and returns an array of CtClass objects representing those class files.

If an element of classnames ends with "[]", then this method returns a CtClass object for that array type.

Parameters
classnames:String[]

an array of fully-qualified class name.

get0back to summary
protected synchronized CtClass get0(String classname, boolean useCache) throws NotFoundException
Parameters
useCache:boolean

false if the cached CtClass must be ignored.

Returns:CtClass

null if the class could not be found.

getAndRenameback to summary
public CtClass getAndRename(String orgName, String newName) throws NotFoundException

Reads a class file and constructs a 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.
Parameters
orgName:String

the original (fully-qualified) class name

newName:String

the new class name

getCachedback to summary
protected CtClass getCached(String classname)

Provide a hook so that subclasses can do their own caching of classes.

See Also
cacheCtClass(String, CtClass, boolean), removeCached(String)
getClassLoaderback to summary
public ClassLoader getClassLoader()

Get the classloader for toClass(), getAnnotations() in CtClass, etc.

The default is the context class loader.

Returns:ClassLoader

the classloader for the pool

See Also
toClass(CtClass), CtClass#getAnnotations()
getContextClassLoaderback to summary
pack-priv static ClassLoader getContextClassLoader()

Obtains a class loader that seems appropriate to look up a class by name.

getCtClassback to summary
public CtClass getCtClass(String classname) throws NotFoundException

Returns a 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.

Parameters
classname:String

a fully-qualified class name or a descriptor representing an array type.

Since
3.8.1
See Also
get(String), javassist.bytecode.ConstPool#getClassInfo(int), javassist.bytecode.Descriptor#toCtClass(String, ClassPool)
getDefaultback 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 -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, ModuleClassPath, or LoaderClassPath.

See Also
ClassClassPath, LoaderClassPath
getImportedPackagesback to summary
public Iterator<String> getImportedPackages()

Returns all the package names recorded by importPackage().

Since
3.1
See Also
importPackage(String)
getMethodback to summary
public CtMethod getMethod(String classname, String methodname) throws NotFoundException

Reads a class file and obtains a compile-time method.

Parameters
classname:String

the class name

methodname:String

the method name

See Also
CtClass#getDeclaredMethod(String)
getOrNullback to summary
public CtClass getOrNull(String classname)

Reads a class file from the source and returns a reference to the 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.

Parameters
classname:String

a fully-qualified class name.

Returns:CtClass

a CtClass object or null.

Since
3.13
See Also
get(String), find(String)
importPackageback 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 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.

Parameters
packageName:String

the package name. It must not include the last '.' (dot). For example, "java.util" is valid but "java.util." is wrong.

Since
3.1
insertClassPathback to summary
public ClassPath insertClassPath(ClassPath cp)

Insert a ClassPath object at the head of the search path.

Returns:ClassPath

the inserted class path.

See Also
javassist.ClassPath, javassist.URLClassPath, javassist.ByteArrayClassPath
insertClassPathback 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.

Parameters
pathname:String

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.

Returns:ClassPath

the inserted class path.

Exceptions
NotFoundException:
if the jar file is not found.
lookupCflowback to summary
public Object[] lookupCflow(String name)

Undocumented method. Do not use; internal-use only.

Parameters
name:String

the name of $cflow variable

makeAnnotationback 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.

Parameters
name:String

a fully-qualified interface name. Or null if the annotation has no super interface.

Exceptions
RuntimeException:
if the existing interface is frozen.
Since
3.19
makeClassback 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 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.

Parameters
classfile:InputStream

class file.

Exceptions
RuntimeException:
if there is a frozen class with the the same name.
See Also
makeClassIfNew(InputStream), javassist.ByteArrayClassPath
makeClassback 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 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.

Parameters
classfile:InputStream

class file.

ifNotFrozen:boolean

throws a RuntimeException if this parameter is true and there is a frozen class with the same name.

See Also
javassist.ByteArrayClassPath
makeClassback 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 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.

Parameters
classfile:ClassFile

class file.

Exceptions
RuntimeException:
if there is a frozen class with the the same name.
Since
3.20
makeClassback 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 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.

Parameters
classfile:ClassFile

class file.

ifNotFrozen:boolean

throws a RuntimeException if this parameter is true and there is a frozen class with the same name.

Since
3.20
makeClassback 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.

Parameters
classname:String

a fully-qualified class name.

Exceptions
RuntimeException:
if the existing class is frozen.
makeClassback 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.

Parameters
classname:String

a fully-qualified class name.

superclass:CtClass

the super class.

Exceptions
RuntimeException:
if the existing class is frozen.
makeClassIfNewback 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 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.

Parameters
classfile:InputStream

the class file.

Since
3.9
See Also
makeClass(InputStream), javassist.ByteArrayClassPath
makeInterfaceback 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.

Parameters
name:String

a fully-qualified interface name.

Exceptions
RuntimeException:
if the existing interface is frozen.
makeInterfaceback 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.

Parameters
name:String

a fully-qualified interface name.

superclass:CtClass

the super interface.

Exceptions
RuntimeException:
if the existing interface is frozen.
makeNestedClassback to summary
pack-priv synchronized CtClass makeNestedClass(String classname)

Creates a new public nested class. This method is called by CtClassType#makeNestedClass().

Parameters
classname:String

a fully-qualified class name.

Returns:CtClass

the nested class.

makePackageback 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 getPackage() on the Class object returned by toClass() will return a non-null object.

The jigsaw module introduced by Java 9 has broken this method.

Parameters
loader:ClassLoader

the class loader passed to toClass() or the default one obtained by getClassLoader().

name:String

the package name.

Since
3.16
See Also
getClassLoader(), toClass(CtClass), CtClass#toClass()
openClassfileback to summary
pack-priv InputStream openClassfile(String classname) throws NotFoundException
recordCflowback to summary
pack-priv void recordCflow(String name, String cname, String fname)

Records the $cflow variable for the field specified by cname and fname.

Parameters
name:String

variable name

cname:String

class name

fname:String

field name

recordInvalidClassNameback 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 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.

Parameters
name:String

an invalid class name (separeted by dots).

removeCachedback to summary
protected CtClass removeCached(String classname)

Provide a hook so that subclasses can do their own caching of classes.

See Also
getCached(String), cacheCtClass(String, CtClass, boolean)
removeClassPathback to summary
public void removeClassPath(ClassPath cp)

Detatches the ClassPath object from the search path. The detached ClassPath object cannot be added to the path again.

toClassback to summary
public Class<T> toClass(CtClass clazz) throws CannotCompileException

Converts the given class to a 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: This method should not be used in Java 11 or later. Use toClass(CtClass, Class).

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.

See Also
toClass(CtClass, Class), toClass(CtClass, Class, java.lang.ClassLoader, ProtectionDomain), getClassLoader()
toClassback to summary
public Class<T> toClass(CtClass ct, ClassLoader loader) throws CannotCompileException

Deprecated

Replaced by toClass(CtClass, Class, ClassLoader, ProtectionDomain). A subclass of ClassPool that has been overriding this method should be modified. It should override toClass(CtClass, Class, ClassLoader, ProtectionDomain).

Converts the class to a java.lang.Class object. Do not override this method any more at a subclass because toClass(CtClass) will 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.

toClassback to summary
public Class<T> toClass(CtClass ct, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException

Converts the class to a 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.

Parameters
ct:CtClass

the class converted into java.lang.Class.

loader:ClassLoader

the class loader used to load this class. For example, the loader returned by getClassLoader() can be used for this parameter.

domain:ProtectionDomain

the protection domain for the class. If it is null, the default domain created by java.lang.ClassLoader is used.

Since
3.3
See Also
getClassLoader()
toClassback to summary
public Class<?> toClass(CtClass ct, Class<?> neighbor) throws CannotCompileException

Converts the class to a java.lang.Class object. Once this method is called, further modifications are not allowed any more.

This method is available in Java 9 or later. It loads the class by using java.lang.invoke.MethodHandles with neighbor.

Parameters
ct:CtClass

the class converted into java.lang.Class.

neighbor:Class<?>

a class belonging to the same package that the converted class belongs to.

Since
3.24
toClassback to summary
public Class<?> toClass(CtClass ct, MethodHandles.Lookup lookup) throws CannotCompileException

Converts the class to a java.lang.Class object. Once this method is called, further modifications are not allowed any more.

This method is available in Java 9 or later. It loads the class by using the given java.lang.invoke.MethodHandles.Lookup.

Parameters
ct:CtClass

the class converted into java.lang.Class.

Since
3.24
toClassback to summary
public Class<T> toClass(CtClass ct, Class<?> neighbor, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException

Converts the class to a java.lang.Class object. Once this method is called, further modifications are not allowed any more.

When the JVM is Java 11 or later, this method loads the class by using java.lang.invoke.MethodHandles with neighbor. The other arguments loader and domain are not used; so they can be null.

Otherwise, or when neighbor is null, 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.

If your program is for only Java 9 or later, don't use this method. Use toClass(CtClass, Class) or toClass(CtClass, java.lang.invoke.MethodHandles.Lookup).

Parameters
ct:CtClass

the class converted into java.lang.Class.

neighbor:Class<?>

a class belonging to the same package that the converted class belongs to. It can be null.

loader:ClassLoader

the class loader used to load this class. For example, the loader returned by getClassLoader() can be used for this parameter.

domain:ProtectionDomain

the protection domain for the class. If it is null, the default domain created by java.lang.ClassLoader is used.

Since
3.24
See Also
getClassLoader()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the class search path.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

writeClassfileback to summary
pack-priv void writeClassfile(String classname, OutputStream out) throws NotFoundException, IOException, CannotCompileException