Package org.apache.tools.ant
Class AntClassLoader
- java.lang.Object
- 
- java.lang.ClassLoader
- 
- org.apache.tools.ant.AntClassLoader
 
 
- 
- All Implemented Interfaces:
- java.io.Closeable,- java.lang.AutoCloseable,- java.util.EventListener,- BuildListener,- SubBuildListener
 - Direct Known Subclasses:
- AntClassLoader2,- AntClassLoader5,- SplitClassLoader
 
 public class AntClassLoader extends java.lang.ClassLoader implements SubBuildListener, java.io.Closeable Used to load classes within ant with a different classpath from that used to start ant. Note that it is possible to force a class into this loader even when that class is on the system classpath by using the forceLoadClass method. Any subsequent classes loaded by that class will then use this loader rather than the system class loader.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 SummaryConstructors Constructor Description AntClassLoader()Create an Ant Class LoaderAntClassLoader(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.
 - 
Method SummaryModifier and Type Method Description voidaddJavaLibraries()add any libraries that come with different java versions herevoidaddLoaderPackageRoot(java.lang.String packageRoot)Adds a package root to the list of packages which must be loaded using this loader.voidaddPathComponent(java.io.File file)Add a path component.voidaddPathElement(java.lang.String pathElement)Adds an element to the classpath to be searched.protected voidaddPathFile(java.io.File pathComponent)Add a file to the path.voidaddSystemPackageRoot(java.lang.String packageRoot)Adds a package root to the list of packages which must be loaded on the parent loader.voidbuildFinished(BuildEvent event)Cleans up any resources held by this classloader at the end of a build.voidbuildStarted(BuildEvent event)Empty implementation to satisfy the BuildListener interface.voidcleanup()Cleans up any resources held by this classloader.voidclose()protected java.lang.Class<?>defineClassFromData(java.io.File container, byte[] classData, java.lang.String classname)Define a class given its bytesprotected voiddefinePackage(java.io.File container, java.lang.String className)Define the package information associated with a class.protected voiddefinePackage(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 manifestjava.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.StringgetClasspath()Returns the classpath this classloader will consult.java.lang.ClassLoadergetConfiguredParent()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.URLgetResource(java.lang.String name)Finds the resource with the given name.java.io.InputStreamgetResourceAsStream(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.URLgetResourceURL(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 voidinitializeClass(java.lang.Class<?> theClass)Deprecated.since 1.6.x.protected booleanisInPath(java.io.File component)Indicate if the given file is in this loader's pathprotected java.lang.Class<?>loadClass(java.lang.String classname, boolean resolve)Loads a class with this class loader.protected voidlog(java.lang.String message, int priority)Logs a message through the project object if one has been provided.voidmessageLogged(BuildEvent event)Empty implementation to satisfy the BuildListener interface.static AntClassLoadernewAntClassLoader(java.lang.ClassLoader parent, Project project, Path path, boolean parentFirst)Factory methodvoidresetThreadContextLoader()Resets the current thread's context loader to its original value.voidsetClassPath(Path classpath)Set the classpath to search for classes to load.voidsetIsolated(boolean isolated)Sets whether this classloader should run in isolated mode.voidsetParent(java.lang.ClassLoader parent)Set the parent for this class loader.voidsetParentFirst(boolean parentFirst)Control whether class lookup is delegated to the parent loader first or after this loader.voidsetProject(Project project)Set the project associated with this class loadervoidsetThreadContextLoader()Sets the current thread's context loader to this classloader, storing the current loader value for later resetting.voidsubBuildFinished(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.voidsubBuildStarted(BuildEvent event)Empty implementation to satisfy the BuildListener interface.voidtargetFinished(BuildEvent event)Empty implementation to satisfy the BuildListener interface.voidtargetStarted(BuildEvent event)Empty implementation to satisfy the BuildListener interface.voidtaskFinished(BuildEvent event)Empty implementation to satisfy the BuildListener interface.voidtaskStarted(BuildEvent event)Empty implementation to satisfy the BuildListener interface.java.lang.StringtoString()Returns aStringrepresenting this loader.- 
Methods inherited from class java.lang.ClassLoaderclearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
- 
 
- 
- 
- 
Constructor Detail- 
AntClassLoaderpublic 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.- Parameters:
- parent- the parent for this classloader.
- project- The project to which this classloader is to belong.
- classpath- The classpath to use to load classes.
- Since:
- Ant 1.7.
 
 - 
AntClassLoaderpublic AntClassLoader() Create an Ant Class Loader
 - 
AntClassLoaderpublic AntClassLoader(Project project, Path classpath) Creates a classloader for the given project using the classpath given.- Parameters:
- 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.
 
 - 
AntClassLoaderpublic AntClassLoader(java.lang.ClassLoader parent, Project project, Path classpath, boolean parentFirst)Creates a classloader for the given project using the classpath given.- Parameters:
- 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.
 
 - 
AntClassLoaderpublic AntClassLoader(Project project, Path classpath, boolean parentFirst) Creates a classloader for the given project using the classpath given.- Parameters:
- 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.
 
 - 
AntClassLoaderpublic AntClassLoader(java.lang.ClassLoader parent, boolean parentFirst)Creates an empty class loader. The classloader should be configured with path elements to specify where the loader is to look for classes.- Parameters:
- 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.
 
 
- 
 - 
Method Detail- 
setProjectpublic void setProject(Project project) Set the project associated with this class loader- Parameters:
- project- the project instance
 
 - 
setClassPathpublic void setClassPath(Path classpath) Set the classpath to search for classes to load. This should not be changed once the classloader starts to server classes- Parameters:
- classpath- the search classpath consisting of directories and jar/zip files.
 
 - 
setParentpublic void setParent(java.lang.ClassLoader parent) Set the parent for this class loader. This is the class loader to which this class loader will delegate to load classes- Parameters:
- parent- the parent class loader.
 
 - 
setParentFirstpublic void setParentFirst(boolean parentFirst) Control whether class lookup is delegated to the parent loader first or after this loader. Use with extreme caution. Setting this to false violates the class loader hierarchy and can lead to Linkage errors- Parameters:
- parentFirst- if true, delegate initial class search to the parent classloader.
 
 - 
logprotected void log(java.lang.String message, int priority)Logs a message through the project object if one has been provided.- Parameters:
- message- The message to log. Should not be- null.
- priority- The logging priority of the message.
 
 - 
setThreadContextLoaderpublic void setThreadContextLoader() Sets the current thread's context loader to this classloader, storing the current loader value for later resetting.
 - 
resetThreadContextLoaderpublic void resetThreadContextLoader() Resets the current thread's context loader to its original value.
 - 
addPathElementpublic void addPathElement(java.lang.String pathElement) throws BuildExceptionAdds an element to the classpath to be searched.- Parameters:
- pathElement- The path element to add. Must not be- null.
- Throws:
- BuildException- if the given path element cannot be resolved against the project.
 
 - 
addPathComponentpublic void addPathComponent(java.io.File file) Add a path component. This simply adds the file, unlike addPathElement it does not open jar files and load files from their CLASSPATH entry in the manifest file.- Parameters:
- file- the jar file or directory to add.
 
 - 
addPathFileprotected void addPathFile(java.io.File pathComponent) throws java.io.IOExceptionAdd a file to the path. Reads the manifest, if available, and adds any additional class path jars specified in the manifest.- Parameters:
- pathComponent- the file which is to be added to the path for this class loader
- Throws:
- java.io.IOException- if data needed from the file cannot be read.
 
 - 
getClasspathpublic java.lang.String getClasspath() Returns the classpath this classloader will consult.- Returns:
- the classpath used for this classloader, with elements separated by the path separator for the system.
 
 - 
setIsolatedpublic void setIsolated(boolean isolated) Sets whether this classloader should run in isolated mode. In isolated mode, classes not found on the given classpath will not be referred to the parent class loader but will cause a ClassNotFoundException.- Parameters:
- isolated- Whether or not this classloader should run in isolated mode.
 
 - 
initializeClass@Deprecated public static void initializeClass(java.lang.Class<?> theClass) Deprecated.since 1.6.x. Use Class.forName with initialize=true instead.Forces initialization of a class in a JDK 1.1 compatible, albeit hacky way.- Parameters:
- theClass- The class to initialize. Must not be- null.
 
 - 
addSystemPackageRootpublic void addSystemPackageRoot(java.lang.String packageRoot) Adds a package root to the list of packages which must be loaded on the parent loader. All subpackages are also included.- Parameters:
- packageRoot- The root of all packages to be included. Should not be- null.
 
 - 
addLoaderPackageRootpublic void addLoaderPackageRoot(java.lang.String packageRoot) Adds a package root to the list of packages which must be loaded using this loader. All subpackages are also included.- Parameters:
- packageRoot- The root of all packages to be included. Should not be- null.
 
 - 
forceLoadClasspublic java.lang.Class<?> forceLoadClass(java.lang.String classname) throws java.lang.ClassNotFoundExceptionLoads a class through this class loader even if that class is available on the parent classpath. This ensures that any classes which are loaded by the returned class will use this classloader.- Parameters:
- classname- The name of the class to be loaded. Must not be- null.
- Returns:
- the required Class object
- Throws:
- java.lang.ClassNotFoundException- if the requested class does not exist on this loader's classpath.
 
 - 
forceLoadSystemClasspublic java.lang.Class<?> forceLoadSystemClass(java.lang.String classname) throws java.lang.ClassNotFoundExceptionLoads a class through this class loader but defer to the parent class loader. This ensures that instances of the returned class will be compatible with instances which have already been loaded on the parent loader.- Parameters:
- classname- The name of the class to be loaded. Must not be- null.
- Returns:
- the required Class object
- Throws:
- java.lang.ClassNotFoundException- if the requested class does not exist on this loader's classpath.
 
 - 
getResourceAsStreampublic java.io.InputStream getResourceAsStream(java.lang.String name) Returns a stream to read the requested resource name.- Overrides:
- getResourceAsStreamin class- java.lang.ClassLoader
- Parameters:
- name- The name of the resource for which a stream is required. Must not be- null.
- Returns:
- a stream to the required resource or nullif the resource cannot be found on the loader's classpath.
 
 - 
getResourcepublic java.net.URL getResource(java.lang.String name) Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.- Overrides:
- getResourcein class- java.lang.ClassLoader
- Parameters:
- name- The name of the resource for which a stream is required. Must not be- null.
- Returns:
- a URL for reading the resource, or nullif the resource could not be found or the caller doesn't have adequate privileges to get the resource.
 
 - 
getNamedResourcespublic java.util.Enumeration<java.net.URL> getNamedResources(java.lang.String name) throws java.io.IOExceptionFinds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.Would override getResources if that wasn't final in Java 1.4. - Parameters:
- name- name of the resource
- Returns:
- possible URLs as enumeration
- Throws:
- java.io.IOException- if something goes wrong
- Since:
- Ant 1.8.0
- See Also:
- findResources(String, boolean)
 
 - 
findResourcesprotected java.util.Enumeration<java.net.URL> findResources(java.lang.String name) throws java.io.IOExceptionReturns an enumeration of URLs representing all the resources with the given name by searching the class loader's classpath.- Overrides:
- findResourcesin class- java.lang.ClassLoader
- Parameters:
- name- The resource name to search for. Must not be- null.
- Returns:
- an enumeration of URLs for the resources
- Throws:
- java.io.IOException- if I/O errors occurs (can't happen)
 
 - 
findResourcesprotected java.util.Enumeration<java.net.URL> findResources(java.lang.String name, boolean parentHasBeenSearched) throws java.io.IOExceptionReturns an enumeration of URLs representing all the resources with the given name by searching the class loader's classpath.- Parameters:
- 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.getResources
- Returns:
- an enumeration of URLs for the resources
- Throws:
- java.io.IOException- if I/O errors occurs (can't happen)
 
 - 
getResourceURLprotected 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.- Parameters:
- 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.
- Returns:
- a stream to the required resource or nullif the resource cannot be found in the given file object.
 
 - 
loadClassprotected java.lang.Class<?> loadClass(java.lang.String classname, boolean resolve) throws java.lang.ClassNotFoundExceptionLoads a class with this class loader. This class attempts to load the class in an order determined by whether or not the class matches the system/loader package lists, with the loader package list taking priority. If the classloader is in isolated mode, failure to load the class in this loader will result in a ClassNotFoundException.- Overrides:
- loadClassin class- java.lang.ClassLoader
- Parameters:
- classname- The name of the class to be loaded. Must not be- null.
- resolve-- trueif all classes upon which this class depends are to be loaded.
- Returns:
- the required Class object
- Throws:
- java.lang.ClassNotFoundException- if the requested class does not exist on the system classpath (when not in isolated mode) or this loader's classpath.
 
 - 
defineClassFromDataprotected java.lang.Class<?> defineClassFromData(java.io.File container, byte[] classData, java.lang.String classname) throws java.io.IOExceptionDefine a class given its bytes- Parameters:
- 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 class
- classname- the name of the class
- Returns:
- the Class instance created from the given data
- Throws:
- java.io.IOException- if the class data cannot be read.
 
 - 
definePackageprotected void definePackage(java.io.File container, java.lang.String className) throws java.io.IOExceptionDefine the package information associated with a class.- Parameters:
- container- the file containing the class definition.
- className- the class name of for which the package information is to be determined.
- Throws:
- java.io.IOException- if the package information cannot be read from the container.
 
 - 
definePackageprotected 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- Parameters:
- container- the jar file containing the manifest
- packageName- the name of the package being defined.
- manifest- the jar's manifest
 
 - 
findClasspublic java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundExceptionSearches for and load a class on the classpath of this class loader.- Overrides:
- findClassin class- java.lang.ClassLoader
- Parameters:
- name- The name of the class to be loaded. Must not be- null.
- Returns:
- the required Class object
- Throws:
- java.lang.ClassNotFoundException- if the requested class does not exist on this loader's classpath.
 
 - 
isInPathprotected boolean isInPath(java.io.File component) Indicate if the given file is in this loader's path- Parameters:
- component- the file which is to be checked
- Returns:
- true if the file is in the class path
 
 - 
cleanuppublic void cleanup() Cleans up any resources held by this classloader. Any open archive files are closed.
 - 
getConfiguredParentpublic java.lang.ClassLoader getConfiguredParent() Gets the parent as has been specified in the constructor or via setParent.- Returns:
- classloader
- Since:
- Ant 1.8.0
 
 - 
buildStartedpublic void buildStarted(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- buildStartedin interface- BuildListener
- Parameters:
- event- the buildStarted event
 
 - 
buildFinishedpublic void buildFinished(BuildEvent event) Cleans up any resources held by this classloader at the end of a build.- Specified by:
- buildFinishedin interface- BuildListener
- Parameters:
- event- the buildFinished event
- See Also:
- BuildEvent.getException()
 
 - 
subBuildFinishedpublic 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.- Specified by:
- subBuildFinishedin interface- SubBuildListener
- Parameters:
- event- the buildFinished event
- Since:
- Ant 1.6.2
- See Also:
- BuildEvent.getException()
 
 - 
subBuildStartedpublic void subBuildStarted(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- subBuildStartedin interface- SubBuildListener
- Parameters:
- event- the buildStarted event
- Since:
- Ant 1.6.2
 
 - 
targetStartedpublic void targetStarted(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- targetStartedin interface- BuildListener
- Parameters:
- event- the targetStarted event
- See Also:
- BuildEvent.getTarget()
 
 - 
targetFinishedpublic void targetFinished(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- targetFinishedin interface- BuildListener
- Parameters:
- event- the targetFinished event
- See Also:
- BuildEvent.getException()
 
 - 
taskStartedpublic void taskStarted(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- taskStartedin interface- BuildListener
- Parameters:
- event- the taskStarted event
- See Also:
- BuildEvent.getTask()
 
 - 
taskFinishedpublic void taskFinished(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- taskFinishedin interface- BuildListener
- Parameters:
- event- the taskFinished event
- See Also:
- BuildEvent.getException()
 
 - 
messageLoggedpublic void messageLogged(BuildEvent event) Empty implementation to satisfy the BuildListener interface.- Specified by:
- messageLoggedin interface- BuildListener
- Parameters:
- event- the messageLogged event
- See Also:
- BuildEvent.getMessage(),- BuildEvent.getException(),- BuildEvent.getPriority()
 
 - 
addJavaLibrariespublic void addJavaLibraries() add any libraries that come with different java versions here
 - 
toStringpublic java.lang.String toString() Returns aStringrepresenting this loader.- Overrides:
- toStringin class- java.lang.Object
- Returns:
- the path that this classloader has.
 
 - 
getResourcespublic java.util.Enumeration<java.net.URL> getResources(java.lang.String name) throws java.io.IOException- Overrides:
- getResourcesin class- java.lang.ClassLoader
- Throws:
- java.io.IOException
 
 - 
closepublic void close() - Specified by:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.io.Closeable
 
 - 
newAntClassLoaderpublic static AntClassLoader newAntClassLoader(java.lang.ClassLoader parent, Project project, Path path, boolean parentFirst) Factory method- Parameters:
- parent- ClassLoader
- project- Project
- path- Path
- parentFirst- boolean
- Returns:
- AntClassLoader
 
 
- 
 
-