public class AntClassLoader extends java.lang.ClassLoader implements SubBuildListener, 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(String packageRoot)
Adds a package root to the list of packages which must be loaded using
this loader.
|
void |
addPathComponent(File file)
Add a path component.
|
void |
addPathElement(String pathElement)
Adds an element to the classpath to be searched.
|
protected void |
addPathFile(File pathComponent)
Add a file to the path.
|
void |
addSystemPackageRoot(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()
Closes this stream and releases any system resources associated with it.
|
protected Class<?> |
defineClassFromData(File container,
byte[] classData,
String classname)
Define a class given its bytes
|
protected void |
definePackage(File container,
String className)
Define the package information associated with a class.
|
protected void |
definePackage(File container,
String packageName,
java.util.jar.Manifest manifest)
Define the package information when the class comes from a
jar with a manifest
|
Class<?> |
findClass(String name)
Searches for and load a class on the classpath of this class loader.
|
protected Enumeration<URL> |
findResources(String name)
Returns an enumeration of URLs representing all the resources with the
given name by searching the class loader's classpath.
|
protected Enumeration<URL> |
findResources(String name,
boolean parentHasBeenSearched)
Returns an enumeration of URLs representing all the resources with the
given name by searching the class loader's classpath.
|
Class<?> |
forceLoadClass(String classname)
Loads a class through this class loader even if that class is available
on the parent classpath.
|
Class<?> |
forceLoadSystemClass(String classname)
Loads a class through this class loader but defer to the parent class
loader.
|
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.
|
Enumeration<URL> |
getNamedResources(String name)
Finds all the resources with the given name.
|
URL |
getResource(String name)
Finds the resource with the given name.
|
InputStream |
getResourceAsStream(String name)
Returns a stream to read the requested resource name.
|
Enumeration<URL> |
getResources(String name) |
protected URL |
getResourceURL(File file,
String resourceName)
Returns the URL of a given resource in the given file which may
either be a directory or a zip file.
|
protected boolean |
isInPath(File component)
Indicate if the given file is in this loader's path
|
protected Class<?> |
loadClass(String classname,
boolean resolve)
Loads a class with this class loader.
|
protected void |
log(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.
|
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()
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 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(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)
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(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 void addJavaLibraries()
public void addLoaderPackageRoot(String packageRoot)
packageRoot
- The root of all packages to be included.
Should not be null
.public void addPathComponent(File file)
file
- the jar file or directory to add.public void addPathElement(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.protected void addPathFile(File pathComponent) throws IOException
pathComponent
- the file which is to be added to the path for
this class loaderIOException
- if data needed from the file cannot be read.public void addSystemPackageRoot(String packageRoot)
packageRoot
- The root of all packages to be included.
Should not be null
.public void buildFinished(BuildEvent event)
buildFinished
in interface BuildListener
event
- the buildFinished eventBuildEvent.getException()
public void buildStarted(BuildEvent event)
buildStarted
in interface BuildListener
event
- the buildStarted eventpublic void cleanup()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
protected Class<?> defineClassFromData(File container, byte[] classData, String classname) throws 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 classIOException
- if the class data cannot be read.protected void definePackage(File container, String className) throws IOException
container
- the file containing the class definition.className
- the class name of for which the package information
is to be determined.IOException
- if the package information cannot be read from the
container.protected void definePackage(File container, 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 Class<?> findClass(String name) throws ClassNotFoundException
findClass
in class java.lang.ClassLoader
name
- The name of the class to be loaded. Must not be
null
.ClassNotFoundException
- if the requested class does not exist
on this loader's classpath.protected Enumeration<URL> findResources(String name) throws IOException
findResources
in class java.lang.ClassLoader
name
- The resource name to search for.
Must not be null
.IOException
- if I/O errors occurs (can't happen)protected Enumeration<URL> findResources(String name, boolean parentHasBeenSearched) throws 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.getResourcesIOException
- if I/O errors occurs (can't happen)public Class<?> forceLoadClass(String classname) throws ClassNotFoundException
classname
- The name of the class to be loaded.
Must not be null
.ClassNotFoundException
- if the requested class does not exist
on this loader's classpath.public Class<?> forceLoadSystemClass(String classname) throws ClassNotFoundException
classname
- The name of the class to be loaded.
Must not be null
.ClassNotFoundException
- if the requested class does not exist
on this loader's classpath.public String getClasspath()
public java.lang.ClassLoader getConfiguredParent()
public Enumeration<URL> getNamedResources(String name) throws IOException
Would override getResources if that wasn't final in Java 1.4.
name
- name of the resourceIOException
#findResources(String, boolean)}
public URL getResource(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 InputStream getResourceAsStream(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 Enumeration<URL> getResources(String name) throws IOException
getResources
in class java.lang.ClassLoader
IOException
protected URL getResourceURL(File file, 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 boolean isInPath(File component)
component
- the file which is to be checkedprotected Class<?> loadClass(String classname, boolean resolve) throws 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.ClassNotFoundException
- if the requested class does not exist
on the system classpath (when not in isolated mode) or this loader's
classpath.protected void log(String message, int priority)
message
- The message to log.
Should not be null
.priority
- The logging priority of the message.public void messageLogged(BuildEvent event)
messageLogged
in interface BuildListener
event
- the messageLogged eventBuildEvent.getMessage()
,
BuildEvent.getException()
,
BuildEvent.getPriority()
public static AntClassLoader newAntClassLoader(java.lang.ClassLoader parent, Project project, Path path, boolean parentFirst)
public void resetThreadContextLoader()
public void setClassPath(Path classpath)
classpath
- the search classpath consisting of directories and
jar/zip files.public void setIsolated(boolean isolated)
isolated
- Whether or not this classloader should run in
isolated mode.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.public void setProject(Project project)
project
- the project instancepublic void setThreadContextLoader()
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 targetFinished(BuildEvent event)
targetFinished
in interface BuildListener
event
- the targetFinished eventBuildEvent.getException()
public void targetStarted(BuildEvent event)
targetStarted
in interface BuildListener
event
- the targetStarted eventBuildEvent.getTarget()
public void taskFinished(BuildEvent event)
taskFinished
in interface BuildListener
event
- the taskFinished eventBuildEvent.getException()
public void taskStarted(BuildEvent event)
taskStarted
in interface BuildListener
event
- the taskStarted eventBuildEvent.getTask()