Class ClassPool
- java.lang.Object
-
- javassist.ClassPool
-
- Direct Known Subclasses:
ScopedClassPool
public class ClassPool extends java.lang.Object
A container ofCtClass
objects. ACtClass
object must be obtained from this object. Ifget()
is called on this object, it searches various sources represented byClassPath
to find a class file and then it creates aCtClass
object representing that class file. The created object is returned to the caller.Memory consumption memo:
ClassPool
objects hold all theCtClass
es that have been created so that the consistency among modified classes can be guaranteed. Thus if a large number ofCtClass
es are processed, theClassPool
will consume a huge amount of memory. To avoid this, aClassPool
object should be recreated, for example, every hundred classes processed. Note thatgetDefault()
is a singleton factory. Otherwise,detach()
inCtClass
should be used to avoid huge memory consumption.ClassPool
hierarchy:ClassPool
s can make a parent-child hierarchy asjava.lang.ClassLoader
s. If aClassPool
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 theClassPath
s of the childClassPool
. This search order is reversed ifClassPath.childFirstLookup
istrue
.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Hashtable
cflow
Table of registered cflow variables.boolean
childFirstLookup
Determines the search order.protected java.util.Hashtable
classes
private static int
COMPRESS_THRESHOLD
private int
compressCount
private static ClassPool
defaultPool
private static java.lang.reflect.Method
defineClass1
private static java.lang.reflect.Method
defineClass2
private static java.lang.reflect.Method
definePackage
static boolean
doPruning
Turning the automatic pruning on/off.private java.util.ArrayList
importedPackages
private static int
INIT_HASH_SIZE
protected ClassPool
parent
static boolean
releaseUnmodifiedClassFile
If true, unmodified and not-recently-used class files are periodically released for saving memory.protected ClassPoolTail
source
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ClassPath
appendClassPath(java.lang.String pathname)
Appends a directory or a jar (or zip) file to the end of the search path.ClassPath
appendClassPath(ClassPath cp)
Appends aClassPath
object 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.protected void
cacheCtClass(java.lang.String classname, CtClass c, boolean dynamic)
Provides a hook so that subclasses can do their own caching of classes.(package private) CtClass
checkNotExists(java.lang.String classname)
(package private) void
checkNotFrozen(java.lang.String classname)
(package private) void
classNameChanged(java.lang.String oldname, CtClass clazz)
void
clearImportedPackages()
Clear all the package names recorded byimportPackage()
.(package private) void
compress()
This method is periodically invoked so that memory footprint will be minimized.protected CtClass
createCtClass(java.lang.String classname, boolean useCache)
Creates a CtClass object representing the specified class.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 theCtClass
object representing that class file.CtClass[]
get(java.lang.String[] classnames)
Reads class files from the source and returns an array ofCtClass
objects representing those class files.protected CtClass
get0(java.lang.String classname, boolean useCache)
CtClass
getAndRename(java.lang.String orgName, java.lang.String newName)
Reads a class file and constructs aCtClass
object with a new name.protected CtClass
getCached(java.lang.String classname)
Provide a hook so that subclasses can do their own caching of classes.java.lang.ClassLoader
getClassLoader()
Get the classloader fortoClass()
,getAnnotations()
inCtClass
, etc.(package private) static java.lang.ClassLoader
getContextClassLoader()
Obtains a class loader that seems appropriate to look up a class by name.CtClass
getCtClass(java.lang.String classname)
Returns aCtClass
object with the given name.static ClassPool
getDefault()
Returns the default class pool.java.util.Iterator
getImportedPackages()
Returns all the package names recorded byimportPackage()
.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 theCtClass
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(java.lang.String pathname)
Inserts a directory or a jar (or zip) file at the head of the search path.ClassPath
insertClassPath(ClassPath cp)
Insert aClassPath
object 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(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
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
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.(package private) CtClass
makeNestedClass(java.lang.String classname)
Creates a new public nested class.void
makePackage(java.lang.ClassLoader loader, java.lang.String name)
Defines a new package.(package private) java.io.InputStream
openClassfile(java.lang.String classname)
(package private) void
recordCflow(java.lang.String name, java.lang.String cname, java.lang.String fname)
Records the$cflow
variable for the field specified bycname
andfname
.void
recordInvalidClassName(java.lang.String name)
Deprecated.protected CtClass
removeCached(java.lang.String classname)
Provide a hook so that subclasses can do their own caching of classes.void
removeClassPath(ClassPath cp)
Detatches theClassPath
object from the search path.java.lang.Class
toClass(CtClass clazz)
Converts the given class to ajava.lang.Class
object.java.lang.Class
toClass(CtClass ct, java.lang.ClassLoader loader)
Deprecated.Replaced bytoClass(CtClass,ClassLoader,ProtectionDomain)
.java.lang.Class
toClass(CtClass ct, java.lang.ClassLoader loader, java.security.ProtectionDomain domain)
Converts the class to ajava.lang.Class
object.private static java.lang.Object
toClass2(java.lang.reflect.Method method, java.lang.ClassLoader loader, java.lang.Object[] args)
java.lang.String
toString()
Returns the class search path.(package private) void
writeClassfile(java.lang.String classname, java.io.OutputStream out)
-
-
-
Field Detail
-
defineClass1
private static java.lang.reflect.Method defineClass1
-
defineClass2
private static java.lang.reflect.Method defineClass2
-
definePackage
private static java.lang.reflect.Method definePackage
-
childFirstLookup
public boolean childFirstLookup
Determines the search order.If this field is true,
get()
first searches the class path associated to thisClassPool
and then the class path associated with the parentClassPool
. Otherwise, the class path associated with the parent is searched first.The default value is false.
-
doPruning
public static boolean doPruning
Turning the automatic pruning on/off.If this field is true,
CtClass
objects are automatically pruned by default whentoBytecode()
etc. are called. The automatic pruning can be turned on/off individually for eachCtClass
object.The initial value is false.
-
compressCount
private int compressCount
-
COMPRESS_THRESHOLD
private static final int COMPRESS_THRESHOLD
- See Also:
- Constant Field Values
-
releaseUnmodifiedClassFile
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
protected ClassPoolTail source
-
parent
protected ClassPool parent
-
classes
protected java.util.Hashtable classes
-
cflow
private java.util.Hashtable cflow
Table of registered cflow variables.
-
INIT_HASH_SIZE
private static final int INIT_HASH_SIZE
- See Also:
- Constant Field Values
-
importedPackages
private java.util.ArrayList importedPackages
-
defaultPool
private static ClassPool defaultPool
-
-
Constructor Detail
-
ClassPool
public ClassPool()
Creates a root class pool. No parent class pool is specified.
-
ClassPool
public ClassPool(boolean useDefaultPath)
Creates a root class pool. IfuseDefaultPath
is true,appendSystemPath()
is called. Otherwise, this constructor is equivalent to the constructor taking no parameter.- Parameters:
useDefaultPath
- true if the system search path is appended.
-
ClassPool
public ClassPool(ClassPool parent)
Creates a class pool.- Parameters:
parent
- the parent of this class pool. If this is a root class pool, this parameter must benull
.- See Also:
getDefault()
-
-
Method Detail
-
getDefault
public static 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 theCLASSPATH
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
andLoaderClassPath
.- See Also:
ClassClassPath
,LoaderClassPath
-
getCached
protected CtClass getCached(java.lang.String classname)
Provide a hook so that subclasses can do their own caching of classes.
-
cacheCtClass
protected void cacheCtClass(java.lang.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)
-
removeCached
protected CtClass removeCached(java.lang.String classname)
Provide a hook so that subclasses can do their own caching of classes.
-
toString
public java.lang.String toString()
Returns the class search path.- Overrides:
toString
in classjava.lang.Object
-
compress
void compress()
This method is periodically invoked so that memory footprint will be minimized.
-
importPackage
public void importPackage(java.lang.String packageName)
Record a package name so that the Javassist compiler searches the package to resolve a class name. Don't record thejava.lang
package, which has been implicitly recorded by default.Since version 3.14,
packageName
can be a fully-qualified class name.Note that
get()
inClassPool
does not search the recorded package. Only the compiler searches it.- Parameters:
packageName
- the package name. It must not include the last '.' (dot). For example, "java.util" is valid but "java.util." is wrong.- Since:
- 3.1
-
clearImportedPackages
public void clearImportedPackages()
Clear all the package names recorded byimportPackage()
. Thejava.lang
package is not removed.- Since:
- 3.1
- See Also:
importPackage(String)
-
getImportedPackages
public java.util.Iterator getImportedPackages()
Returns all the package names recorded byimportPackage()
.- Since:
- 3.1
- See Also:
importPackage(String)
-
recordInvalidClassName
public void recordInvalidClassName(java.lang.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 sinceget()
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
- an invalid class name (separeted by dots).
-
recordCflow
void recordCflow(java.lang.String name, java.lang.String cname, java.lang.String fname)
Records the$cflow
variable for the field specified bycname
andfname
.- Parameters:
name
- variable namecname
- class namefname
- field name
-
lookupCflow
public java.lang.Object[] lookupCflow(java.lang.String name)
Undocumented method. Do not use; internal-use only.- Parameters:
name
- the name of$cflow
variable
-
getAndRename
public CtClass getAndRename(java.lang.String orgName, java.lang.String newName) throws NotFoundException
Reads a class file and constructs aCtClass
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 aCtClass
object representingPair
class. The definition ofPair
is the same as that ofPoint
class except the class name sincePair
is defined by readingPoint.class
.- Parameters:
orgName
- the original (fully-qualified) class namenewName
- the new class name- Throws:
NotFoundException
-
classNameChanged
void classNameChanged(java.lang.String oldname, CtClass clazz)
-
get
public CtClass get(java.lang.String classname) throws NotFoundException
Reads a class file from the source and returns a reference to theCtClass
object representing that class file. If that class file has been already read, this method returns a reference to theCtClass
created when that class file was read at the first time.If
classname
ends with "[]", then this method returns aCtClass
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
- a fully-qualified class name.- Throws:
NotFoundException
-
getOrNull
public CtClass getOrNull(java.lang.String classname)
Reads a class file from the source and returns a reference to theCtClass
object representing that class file. This method is equivalent toget
except that it returnsnull
when a class file is not found and it never throws an exception.- Parameters:
classname
- a fully-qualified class name.- Returns:
- a
CtClass
object ornull
. - Since:
- 3.13
- See Also:
get(String)
,find(String)
-
getCtClass
public CtClass getCtClass(java.lang.String classname) throws NotFoundException
Returns aCtClass
object with the given name. This is almost equivalent toget(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 fromgetClassInfo
injavassist.bytecode.ClassPool
.getClassInfo
returns a fully-qualified class name but, if the class is an array type, it returns a descriptor.- Parameters:
classname
- a fully-qualified class name or a descriptor representing an array type.- Throws:
NotFoundException
- Since:
- 3.8.1
- See Also:
get(String)
,ConstPool.getClassInfo(int)
,Descriptor.toCtClass(String, ClassPool)
-
get0
protected CtClass get0(java.lang.String classname, boolean useCache) throws NotFoundException
- Parameters:
useCache
- false if the cached CtClass must be ignored.- Returns:
- null if the class could not be found.
- Throws:
NotFoundException
-
createCtClass
protected CtClass createCtClass(java.lang.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:
- null if the class file could not be found.
-
find
public java.net.URL find(java.lang.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
- a fully-qualified class name.- Returns:
- null if the class file could not be found.
- See Also:
CtClass.getURL()
-
checkNotFrozen
void checkNotFrozen(java.lang.String classname) throws java.lang.RuntimeException
- Throws:
java.lang.RuntimeException
-
checkNotExists
CtClass checkNotExists(java.lang.String classname)
-
openClassfile
java.io.InputStream openClassfile(java.lang.String classname) throws NotFoundException
- Throws:
NotFoundException
-
writeClassfile
void writeClassfile(java.lang.String classname, java.io.OutputStream out) throws NotFoundException, java.io.IOException, CannotCompileException
- Throws:
NotFoundException
java.io.IOException
CannotCompileException
-
get
public CtClass[] get(java.lang.String[] classnames) throws NotFoundException
Reads class files from the source and returns an array ofCtClass
objects representing those class files.If an element of
classnames
ends with "[]", then this method returns aCtClass
object for that array type.- Parameters:
classnames
- an array of fully-qualified class name.- Throws:
NotFoundException
-
getMethod
public CtMethod getMethod(java.lang.String classname, java.lang.String methodname) throws NotFoundException
Reads a class file and obtains a compile-time method.- Parameters:
classname
- the class namemethodname
- the method name- Throws:
NotFoundException
- See Also:
CtClass.getDeclaredMethod(String)
-
makeClass
public CtClass makeClass(java.io.InputStream classfile) throws java.io.IOException, java.lang.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
- class file.- Throws:
java.lang.RuntimeException
- if there is a frozen class with the the same name.java.io.IOException
- See Also:
makeClassIfNew(InputStream)
,ByteArrayClassPath
-
makeClass
public CtClass makeClass(java.io.InputStream classfile, boolean ifNotFrozen) throws java.io.IOException, java.lang.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
- class file.ifNotFrozen
- throws a RuntimeException if this parameter is true and there is a frozen class with the same name.- Throws:
java.io.IOException
java.lang.RuntimeException
- See Also:
ByteArrayClassPath
-
makeClass
public CtClass makeClass(ClassFile classfile) throws java.lang.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
- class file.- Throws:
java.lang.RuntimeException
- if there is a frozen class with the the same name.- Since:
- 3.20
-
makeClass
public CtClass makeClass(ClassFile classfile, boolean ifNotFrozen) throws java.lang.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
- class file.ifNotFrozen
- throws a RuntimeException if this parameter is true and there is a frozen class with the same name.- Throws:
java.lang.RuntimeException
- Since:
- 3.20
-
makeClassIfNew
public CtClass makeClassIfNew(java.io.InputStream classfile) throws java.io.IOException, java.lang.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
- the class file.- Throws:
java.io.IOException
java.lang.RuntimeException
- Since:
- 3.9
- See Also:
makeClass(InputStream)
,ByteArrayClassPath
-
makeClass
public CtClass makeClass(java.lang.String classname) throws java.lang.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
- a fully-qualified class name.- Throws:
java.lang.RuntimeException
- if the existing class is frozen.
-
makeClass
public CtClass makeClass(java.lang.String classname, CtClass superclass) throws java.lang.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
- a fully-qualified class name.superclass
- the super class.- Throws:
java.lang.RuntimeException
- if the existing class is frozen.
-
makeNestedClass
CtClass makeNestedClass(java.lang.String classname)
Creates a new public nested class. This method is called byCtClassType#makeNestedClass()
.- Parameters:
classname
- a fully-qualified class name.- Returns:
- the nested class.
-
makeInterface
public CtClass makeInterface(java.lang.String name) throws java.lang.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
- a fully-qualified interface name.- Throws:
java.lang.RuntimeException
- if the existing interface is frozen.
-
makeInterface
public CtClass makeInterface(java.lang.String name, CtClass superclass) throws java.lang.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
- a fully-qualified interface name.superclass
- the super interface.- Throws:
java.lang.RuntimeException
- if the existing interface is frozen.
-
makeAnnotation
public CtClass makeAnnotation(java.lang.String name) throws java.lang.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
- a fully-qualified interface name. Or null if the annotation has no super interface.- Throws:
java.lang.RuntimeException
- if the existing interface is frozen.- Since:
- 3.19
-
appendSystemPath
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 theCLASSPATH
environment variable.- Returns:
- the appended class path.
-
insertClassPath
public ClassPath insertClassPath(ClassPath cp)
Insert aClassPath
object at the head of the search path.- Returns:
- the inserted class path.
- See Also:
ClassPath
,URLClassPath
,ByteArrayClassPath
-
appendClassPath
public ClassPath appendClassPath(ClassPath cp)
Appends aClassPath
object to the end of the search path.- Returns:
- the appended class path.
- See Also:
ClassPath
,URLClassPath
,ByteArrayClassPath
-
insertClassPath
public ClassPath insertClassPath(java.lang.String pathname) throws NotFoundException
Inserts a directory or a jar (or zip) file at the head of the search path.- Parameters:
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.- Returns:
- the inserted class path.
- Throws:
NotFoundException
- if the jar file is not found.
-
appendClassPath
public ClassPath appendClassPath(java.lang.String pathname) throws NotFoundException
Appends a directory or a jar (or zip) file to the end of the search path.- Parameters:
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.- Returns:
- the appended class path.
- Throws:
NotFoundException
- if the jar file is not found.
-
removeClassPath
public void removeClassPath(ClassPath cp)
Detatches theClassPath
object from the search path. The detachedClassPath
object cannot be added to the path again.
-
appendPathList
public void appendPathList(java.lang.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
- a (semi)colon-separated list of the path names of directories and jar files. The directory name must not end with a path separator ("/").- Throws:
NotFoundException
- if a jar file is not found.
-
toClass
public java.lang.Class toClass(CtClass clazz) throws CannotCompileException
Converts the given class to ajava.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 callinggetClassLoader()
.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.
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
Get the classloader fortoClass()
,getAnnotations()
inCtClass
, etc.The default is the context class loader.
- Returns:
- the classloader for the pool
- See Also:
toClass(CtClass)
,CtClass.getAnnotations()
-
getContextClassLoader
static java.lang.ClassLoader getContextClassLoader()
Obtains a class loader that seems appropriate to look up a class by name.
-
toClass
public java.lang.Class toClass(CtClass ct, java.lang.ClassLoader loader) throws CannotCompileException
Deprecated.Replaced bytoClass(CtClass,ClassLoader,ProtectionDomain)
. A subclass ofClassPool
that has been overriding this method should be modified. It should overridetoClass(CtClass,ClassLoader,ProtectionDomain)
.Converts the class to ajava.lang.Class
object. Do not override this method any more at a subclass becausetoClass(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.
- Throws:
CannotCompileException
-
toClass
public java.lang.Class toClass(CtClass ct, java.lang.ClassLoader loader, java.security.ProtectionDomain domain) throws CannotCompileException
Converts the class to ajava.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 ajava.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 callgetProtectionDomain()
injava.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:
loader
- the class loader used to load this class. For example, the loader returned bygetClassLoader()
can be used for this parameter.domain
- the protection domain for the class. If it is null, the default domain created byjava.lang.ClassLoader
is used.- Throws:
CannotCompileException
- Since:
- 3.3
- See Also:
getClassLoader()
-
toClass2
private static java.lang.Object toClass2(java.lang.reflect.Method method, java.lang.ClassLoader loader, java.lang.Object[] args) throws java.lang.Exception
- Throws:
java.lang.Exception
-
makePackage
public void makePackage(java.lang.ClassLoader loader, java.lang.String name) throws CannotCompileException
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 theClass
object returned bytoClass()
will return a non-null object.- Parameters:
loader
- the class loader passed totoClass()
or the default one obtained bygetClassLoader()
.name
- the package name.- Throws:
CannotCompileException
- Since:
- 3.16
- See Also:
getClassLoader()
,toClass(CtClass)
,CtClass.toClass()
-
-