public class AntClassLoader
extends java.lang.ClassLoader
implements org.apache.tools.ant.SubBuildListener
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,
org.apache.tools.ant.Project project,
org.apache.tools.ant.types.Path classpath)
Create an Ant ClassLoader for a given project, with
a parent classloader and an initial classpath.
|
AntClassLoader(java.lang.ClassLoader parent,
org.apache.tools.ant.Project project,
org.apache.tools.ant.types.Path classpath,
boolean parentFirst)
Creates a classloader for the given project using the classpath given.
|
AntClassLoader(org.apache.tools.ant.Project project,
org.apache.tools.ant.types.Path classpath)
Creates a classloader for the given project using the classpath given.
|
AntClassLoader(org.apache.tools.ant.Project project,
org.apache.tools.ant.types.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(org.apache.tools.ant.BuildEvent event)
Cleans up any resources held by this classloader at the end
of a build.
|
void |
buildStarted(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
cleanup()
Cleans up any resources held by this classloader.
|
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 |
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 |
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.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.
|
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(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
resetThreadContextLoader()
Resets the current thread's context loader to its original value.
|
void |
setClassPath(org.apache.tools.ant.types.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(org.apache.tools.ant.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(org.apache.tools.ant.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(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
targetFinished(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
targetStarted(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
taskFinished(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
void |
taskStarted(org.apache.tools.ant.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, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public AntClassLoader(java.lang.ClassLoader parent, org.apache.tools.ant.Project project, org.apache.tools.ant.types.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(org.apache.tools.ant.Project project, org.apache.tools.ant.types.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, org.apache.tools.ant.Project project, org.apache.tools.ant.types.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(org.apache.tools.ant.Project project, org.apache.tools.ant.types.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(org.apache.tools.ant.Project project)
project
- the project instancepublic void setClassPath(org.apache.tools.ant.types.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)
pathElement
- The path element to add. Must not be
null
.org.apache.tools.ant.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.protected java.util.Enumeration 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 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 void buildStarted(org.apache.tools.ant.BuildEvent event)
buildStarted
in interface org.apache.tools.ant.BuildListener
event
- the buildStarted eventpublic void buildFinished(org.apache.tools.ant.BuildEvent event)
buildFinished
in interface org.apache.tools.ant.BuildListener
event
- the buildFinished eventpublic void subBuildFinished(org.apache.tools.ant.BuildEvent event)
subBuildFinished
in interface org.apache.tools.ant.SubBuildListener
event
- the buildFinished eventpublic void subBuildStarted(org.apache.tools.ant.BuildEvent event)
subBuildStarted
in interface org.apache.tools.ant.SubBuildListener
event
- the buildStarted eventpublic void targetStarted(org.apache.tools.ant.BuildEvent event)
targetStarted
in interface org.apache.tools.ant.BuildListener
event
- the targetStarted eventpublic void targetFinished(org.apache.tools.ant.BuildEvent event)
targetFinished
in interface org.apache.tools.ant.BuildListener
event
- the targetFinished eventpublic void taskStarted(org.apache.tools.ant.BuildEvent event)
taskStarted
in interface org.apache.tools.ant.BuildListener
event
- the taskStarted eventpublic void taskFinished(org.apache.tools.ant.BuildEvent event)
taskFinished
in interface org.apache.tools.ant.BuildListener
event
- the taskFinished eventpublic void messageLogged(org.apache.tools.ant.BuildEvent event)
messageLogged
in interface org.apache.tools.ant.BuildListener
event
- the messageLogged eventpublic void addJavaLibraries()
public java.lang.String toString()
String
representing this loader.toString
in class java.lang.Object