public class AntClassLoader extends java.lang.ClassLoader implements SubBuildListener, java.io.Closeable
Note that this classloader has a feature to allow loading in reverse order and for "isolation". Due to the fact that a number of methods in java.lang.ClassLoader are final (at least in java 1.4 getResources) this means that the class has to fake the given parent.
Constructor and Description |
---|
AntClassLoader()
Create an Ant Class Loader
|
AntClassLoader(java.lang.ClassLoader parent,
boolean parentFirst)
Creates an empty class loader.
|
AntClassLoader(java.lang.ClassLoader parent,
Project project,
Path classpath)
Create an Ant ClassLoader for a given project, with
a parent classloader and an initial classpath.
|
AntClassLoader(java.lang.ClassLoader parent,
Project project,
Path classpath,
boolean parentFirst)
Creates a classloader for the given project using the classpath given.
|
AntClassLoader(Project project,
Path classpath)
Creates a classloader for the given project using the classpath given.
|
AntClassLoader(Project project,
Path classpath,
boolean parentFirst)
Creates a classloader for the given project using the classpath given.
|
Modifier and Type | Method and Description |
---|---|
void |
addJavaLibraries()
add any libraries that come with different java versions
here
|
void |
addLoaderPackageRoot(java.lang.String packageRoot)
Adds a package root to the list of packages which must be loaded using
this loader.
|
void |
addPathComponent(java.io.File file)
Add a path component.
|
void |
addPathElement(java.lang.String pathElement)
Adds an element to the classpath to be searched.
|
protected void |
addPathFile(java.io.File pathComponent)
Add a file to the path.
|
void |
addSystemPackageRoot(java.lang.String packageRoot)
Adds a package root to the list of packages which must be loaded on the
parent loader.
|
void |
buildFinished(BuildEvent event)
Cleans up any resources held by this classloader at the end
of a build.
|
void |
buildStarted(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
cleanup()
Cleans up any resources held by this classloader.
|
void |
close() |
protected java.lang.Class<?> |
defineClassFromData(java.io.File container,
byte[] classData,
java.lang.String classname)
Define a class given its bytes
|
protected void |
definePackage(java.io.File container,
java.lang.String className)
Define the package information associated with a class.
|
protected void |
definePackage(java.io.File container,
java.lang.String packageName,
java.util.jar.Manifest manifest)
Define the package information when the class comes from a
jar with a manifest
|
java.lang.Class<?> |
findClass(java.lang.String name)
Searches for and load a class on the classpath of this class loader.
|
protected java.util.Enumeration<java.net.URL> |
findResources(java.lang.String name)
Returns an enumeration of URLs representing all the resources with the
given name by searching the class loader's classpath.
|
protected java.util.Enumeration<java.net.URL> |
findResources(java.lang.String name,
boolean parentHasBeenSearched)
Returns an enumeration of URLs representing all the resources with the
given name by searching the class loader's classpath.
|
java.lang.Class<?> |
forceLoadClass(java.lang.String classname)
Loads a class through this class loader even if that class is available
on the parent classpath.
|
java.lang.Class<?> |
forceLoadSystemClass(java.lang.String classname)
Loads a class through this class loader but defer to the parent class
loader.
|
java.lang.String |
getClasspath()
Returns the classpath this classloader will consult.
|
java.lang.ClassLoader |
getConfiguredParent()
Gets the parent as has been specified in the constructor or via
setParent.
|
java.util.Enumeration<java.net.URL> |
getNamedResources(java.lang.String name)
Finds all the resources with the given name.
|
java.net.URL |
getResource(java.lang.String name)
Finds the resource with the given name.
|
java.io.InputStream |
getResourceAsStream(java.lang.String name)
Returns a stream to read the requested resource name.
|
java.util.Enumeration<java.net.URL> |
getResources(java.lang.String name) |
protected java.net.URL |
getResourceURL(java.io.File file,
java.lang.String resourceName)
Returns the URL of a given resource in the given file which may
either be a directory or a zip file.
|
static void |
initializeClass(java.lang.Class<?> theClass)
Deprecated.
since 1.6.x.
Use Class.forName with initialize=true instead.
|
protected boolean |
isInPath(java.io.File component)
Indicate if the given file is in this loader's path
|
protected java.lang.Class<?> |
loadClass(java.lang.String classname,
boolean resolve)
Loads a class with this class loader.
|
protected void |
log(java.lang.String message,
int priority)
Logs a message through the project object if one has been provided.
|
void |
messageLogged(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
static AntClassLoader |
newAntClassLoader(java.lang.ClassLoader parent,
Project project,
Path path,
boolean parentFirst)
Factory method
|
void |
resetThreadContextLoader()
Resets the current thread's context loader to its original value.
|
void |
setClassPath(Path classpath)
Set the classpath to search for classes to load.
|
void |
setIsolated(boolean isolated)
Sets whether this classloader should run in isolated mode.
|
void |
setParent(java.lang.ClassLoader parent)
Set the parent for this class loader.
|
void |
setParentFirst(boolean parentFirst)
Control whether class lookup is delegated to the parent loader first
or after this loader.
|
void |
setProject(Project project)
Set the project associated with this class loader
|
void |
setThreadContextLoader()
Sets the current thread's context loader to this classloader, storing
the current loader value for later resetting.
|
void |
subBuildFinished(BuildEvent event)
Cleans up any resources held by this classloader at the end of
a subbuild if it has been created for the subbuild's project
instance.
|
void |
subBuildStarted(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
targetFinished(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
targetStarted(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
taskFinished(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
taskStarted(BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
java.lang.String |
toString()
Returns a
String representing this loader. |
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public AntClassLoader(java.lang.ClassLoader parent, Project project, Path classpath)
parent
- the parent for this classloader.project
- The project to which this classloader is to
belong.classpath
- The classpath to use to load classes.public AntClassLoader()
public AntClassLoader(Project project, Path classpath)
project
- The project to which this classloader is to belong.
Must not be null
.classpath
- The classpath to use to load the classes. This
is combined with the system classpath in a manner
determined by the value of ${build.sysclasspath}.
May be null
, in which case no path
elements are set up to start with.public AntClassLoader(java.lang.ClassLoader parent, Project project, Path classpath, boolean parentFirst)
parent
- The parent classloader to which unsatisfied loading
attempts are delegated. May be null
,
in which case the classloader which loaded this
class is used as the parent.project
- The project to which this classloader is to belong.
Must not be null
.classpath
- the classpath to use to load the classes.
May be null
, in which case no path
elements are set up to start with.parentFirst
- If true
, indicates that the parent
classloader should be consulted before trying to
load the a class through this loader.public AntClassLoader(Project project, Path classpath, boolean parentFirst)
project
- The project to which this classloader is to belong.
Must not be null
.classpath
- The classpath to use to load the classes. May be
null
, in which case no path
elements are set up to start with.parentFirst
- If true
, indicates that the parent
classloader should be consulted before trying to
load the a class through this loader.public AntClassLoader(java.lang.ClassLoader parent, boolean parentFirst)
parent
- The parent classloader to which unsatisfied loading
attempts are delegated. May be null
,
in which case the classloader which loaded this
class is used as the parent.parentFirst
- If true
, indicates that the parent
classloader should be consulted before trying to
load the a class through this loader.public void setProject(Project project)
project
- the project instancepublic void setClassPath(Path classpath)
classpath
- the search classpath consisting of directories and
jar/zip files.public void setParent(java.lang.ClassLoader parent)
parent
- the parent class loader.public void setParentFirst(boolean parentFirst)
parentFirst
- if true, delegate initial class search to the parent
classloader.protected void log(java.lang.String message, int priority)
message
- The message to log.
Should not be null
.priority
- The logging priority of the message.public void setThreadContextLoader()
public void resetThreadContextLoader()
public void addPathElement(java.lang.String pathElement) throws BuildException
pathElement
- The path element to add. Must not be
null
.BuildException
- if the given path element cannot be resolved
against the project.public void addPathComponent(java.io.File file)
file
- the jar file or directory to add.protected void addPathFile(java.io.File pathComponent) throws java.io.IOException
pathComponent
- the file which is to be added to the path for
this class loaderjava.io.IOException
- if data needed from the file cannot be read.public java.lang.String getClasspath()
public void setIsolated(boolean isolated)
isolated
- Whether or not this classloader should run in
isolated mode.@Deprecated public static void initializeClass(java.lang.Class<?> theClass)
theClass
- The class to initialize.
Must not be null
.public void addSystemPackageRoot(java.lang.String packageRoot)
packageRoot
- The root of all packages to be included.
Should not be null
.public void addLoaderPackageRoot(java.lang.String packageRoot)
packageRoot
- The root of all packages to be included.
Should not be null
.public java.lang.Class<?> forceLoadClass(java.lang.String classname) throws java.lang.ClassNotFoundException
classname
- The name of the class to be loaded.
Must not be null
.java.lang.ClassNotFoundException
- if the requested class does not exist
on this loader's classpath.public java.lang.Class<?> forceLoadSystemClass(java.lang.String classname) throws java.lang.ClassNotFoundException
classname
- The name of the class to be loaded.
Must not be null
.java.lang.ClassNotFoundException
- if the requested class does not exist
on this loader's classpath.public java.io.InputStream getResourceAsStream(java.lang.String name)
getResourceAsStream
in class java.lang.ClassLoader
name
- The name of the resource for which a stream is required.
Must not be null
.null
if the
resource cannot be found on the loader's classpath.public java.net.URL getResource(java.lang.String name)
getResource
in class java.lang.ClassLoader
name
- The name of the resource for which a stream is required.
Must not be null
.null
if the
resource could not be found or the caller doesn't have
adequate privileges to get the resource.public java.util.Enumeration<java.net.URL> getNamedResources(java.lang.String name) throws java.io.IOException
Would override getResources if that wasn't final in Java 1.4.
name
- name of the resourcejava.io.IOException
- if something goes wrongfindResources(String, boolean)
protected java.util.Enumeration<java.net.URL> findResources(java.lang.String name) throws java.io.IOException
findResources
in class java.lang.ClassLoader
name
- The resource name to search for.
Must not be null
.java.io.IOException
- if I/O errors occurs (can't happen)protected java.util.Enumeration<java.net.URL> findResources(java.lang.String name, boolean parentHasBeenSearched) throws java.io.IOException
name
- The resource name to search for.
Must not be null
.parentHasBeenSearched
- whether ClassLoader.this.parent
has been searched - will be true if the method is (indirectly)
called from ClassLoader.getResourcesjava.io.IOException
- if I/O errors occurs (can't happen)protected java.net.URL getResourceURL(java.io.File file, java.lang.String resourceName)
file
- The file (directory or jar) in which to search for
the resource. Must not be null
.resourceName
- The name of the resource for which a stream
is required. Must not be null
.null
if the
resource cannot be found in the given file object.protected java.lang.Class<?> loadClass(java.lang.String classname, boolean resolve) throws java.lang.ClassNotFoundException
loadClass
in class java.lang.ClassLoader
classname
- The name of the class to be loaded.
Must not be null
.resolve
- true
if all classes upon which this class
depends are to be loaded.java.lang.ClassNotFoundException
- if the requested class does not exist
on the system classpath (when not in isolated mode) or this loader's
classpath.protected java.lang.Class<?> defineClassFromData(java.io.File container, byte[] classData, java.lang.String classname) throws java.io.IOException
container
- the container from which the class data has been read
may be a directory or a jar/zip file.classData
- the bytecode data for the classclassname
- the name of the classjava.io.IOException
- if the class data cannot be read.protected void definePackage(java.io.File container, java.lang.String className) throws java.io.IOException
container
- the file containing the class definition.className
- the class name of for which the package information
is to be determined.java.io.IOException
- if the package information cannot be read from the
container.protected void definePackage(java.io.File container, java.lang.String packageName, java.util.jar.Manifest manifest)
container
- the jar file containing the manifestpackageName
- the name of the package being defined.manifest
- the jar's manifestpublic java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException
findClass
in class java.lang.ClassLoader
name
- The name of the class to be loaded. Must not be
null
.java.lang.ClassNotFoundException
- if the requested class does not exist
on this loader's classpath.protected boolean isInPath(java.io.File component)
component
- the file which is to be checkedpublic void cleanup()
public java.lang.ClassLoader getConfiguredParent()
public void buildStarted(BuildEvent event)
buildStarted
in interface BuildListener
event
- the buildStarted eventpublic void buildFinished(BuildEvent event)
buildFinished
in interface BuildListener
event
- the buildFinished eventBuildEvent.getException()
public void subBuildFinished(BuildEvent event)
subBuildFinished
in interface SubBuildListener
event
- the buildFinished eventBuildEvent.getException()
public void subBuildStarted(BuildEvent event)
subBuildStarted
in interface SubBuildListener
event
- the buildStarted eventpublic void targetStarted(BuildEvent event)
targetStarted
in interface BuildListener
event
- the targetStarted eventBuildEvent.getTarget()
public void targetFinished(BuildEvent event)
targetFinished
in interface BuildListener
event
- the targetFinished eventBuildEvent.getException()
public void taskStarted(BuildEvent event)
taskStarted
in interface BuildListener
event
- the taskStarted eventBuildEvent.getTask()
public void taskFinished(BuildEvent event)
taskFinished
in interface BuildListener
event
- the taskFinished eventBuildEvent.getException()
public void messageLogged(BuildEvent event)
messageLogged
in interface BuildListener
event
- the messageLogged eventBuildEvent.getMessage()
,
BuildEvent.getException()
,
BuildEvent.getPriority()
public void addJavaLibraries()
public java.lang.String toString()
String
representing this loader.toString
in class java.lang.Object
public java.util.Enumeration<java.net.URL> getResources(java.lang.String name) throws java.io.IOException
getResources
in class java.lang.ClassLoader
java.io.IOException
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public static AntClassLoader newAntClassLoader(java.lang.ClassLoader parent, Project project, Path path, boolean parentFirst)
parent
- ClassLoaderproject
- Projectpath
- PathparentFirst
- boolean