Top Description Inners Fields Constructors Methods
org.apache.tools.ant

public Class AntClassLoader

extends ClassLoader
implements SubBuildListener, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, org.apache.tools.ant.SubBuildListener, org.apache.tools.ant.BuildListener, java.util.EventListener
Known Direct Subclasses
org.apache.tools.ant.loader.AntClassLoader2, org.apache.tools.ant.loader.AntClassLoader5, org.apache.tools.ant.util.SplitClassLoader
Imports
java.io.ByteArrayOutputStream, .Closeable, .File, .IOException, .InputStream, java.lang.reflect.Constructor, java.net.MalformedURLException, .URL, java.nio.file.Files, java.security.CodeSource, .ProtectionDomain, java.security.cert.Certificate, java.util.Collections, .Enumeration, .HashMap, .Hashtable, .Map, .NoSuchElementException, .Objects, .StringTokenizer, .Vector, java.util.jar.Attributes, .Attributes.Name, .JarEntry, .JarFile, .Manifest, java.util.stream.Collectors, .Stream, java.util.zip.ZipFile, org.apache.tools.ant.launch.Locator, org.apache.tools.ant.types.Path, org.apache.tools.ant.util.FileUtils, .JavaEnvUtils, .LoaderUtils, .ReflectUtil, .StringUtils, .VectorSet, org.apache.tools.zip.ZipLong

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
AntClassLoader.ResourceEnumeration

An enumeration of all resources of a given name found within the classpath of this class loader.

Field Summary

Modifier and TypeField and Description
private static final int
BUFFER_SIZE

The size of buffers to be used in this classloader.

private static final ZipLong
private static final FileUtils
private boolean
ignoreBase

Whether or not this classloader will ignore the base classloader if it can't find a class.

private static final boolean
private boolean
isContextLoaderSaved

Whether or not the context loader is currently saved.

private Hashtable<File, JarFile>
jarFiles

A hashtable of zip files opened by the classloader (File to JarFile).

private final Vector<String>
loaderPackages

These are the package roots that are to be loaded by this class loader regardless of whether the parent class loader is being searched first or not.

private static final Class<T>[]
private static final Object
private static final int
NUMBER_OF_STRINGS

Number of array elements in a test array of strings

private ClassLoader
parent

Hides java.lang.ClassLoader.parent.

The parent class loader, if one is given or can be determined.
private boolean
parentFirst

Indicates whether the parent class loader should be consulted before trying to load with this class loader.

private final Vector<File>
pathComponents

The components of the classpath that the classloader searches for classes.

private static Map<String, String>
pathMap

Static map of jar file/time to manifest class-path entries

private Project
project

The project to which this class loader belongs.

private ClassLoader
savedContextLoader

The context loader saved when setting the thread's current context loader.

private static final ZipLong
private final Vector<String>
systemPackages

These are the package roots that are to be loaded by the parent class loader regardless of whether the parent class loader is being searched first or not.

Constructor Summary

AccessConstructor and Description
public
AntClassLoader(final ClassLoader
the parent for this classloader.
parent
,
final Project
The project to which this classloader is to belong.
project
,
final Path
The classpath to use to load classes.
classpath
)

Create an Ant ClassLoader for a given project, with a parent classloader and an initial classpath.

public
AntClassLoader()

Create an Ant Class Loader

public
AntClassLoader(final Project
The project to which this classloader is to belong. Must not be null.
project
,
final Path
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.
classpath
)

Creates a classloader for the given project using the classpath given.

public
AntClassLoader(final ClassLoader
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.
parent
,
final Project
The project to which this classloader is to belong. Must not be null.
project
,
final Path
the classpath to use to load the classes. May be null, in which case no path elements are set up to start with.
classpath
,
final boolean
If true, indicates that the parent classloader should be consulted before trying to load the a class through this loader.
parentFirst
)

Creates a classloader for the given project using the classpath given.

public
AntClassLoader(final Project
The project to which this classloader is to belong. Must not be null.
project
,
final Path
The classpath to use to load the classes. May be null, in which case no path elements are set up to start with.
classpath
,
final boolean
If true, indicates that the parent classloader should be consulted before trying to load the a class through this loader.
parentFirst
)

Creates a classloader for the given project using the classpath given.

public
AntClassLoader(final ClassLoader
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.
parent
,
final boolean
If true, indicates that the parent classloader should be consulted before trying to load the a class through this loader.
parentFirst
)

Creates an empty class loader.

Method Summary

Modifier and TypeMethod and Description
public void
addJavaLibraries()

add any libraries that come with different java versions here

public void
addLoaderPackageRoot(final String
The root of all packages to be included. Should not be null.
packageRoot
)

Adds a package root to the list of packages which must be loaded using this loader.

public void
addPathComponent(final File
the jar file or directory to add.
file
)

Add a path component.

public void
addPathElement(final String
The path element to add. Must not be null.
pathElement
)

Adds an element to the classpath to be searched.

protected void
addPathFile(final File
the file which is to be added to the path for this class loader
pathComponent
)

Add a file to the path.

public void
addSystemPackageRoot(final String
The root of all packages to be included. Should not be null.
packageRoot
)

Adds a package root to the list of packages which must be loaded on the parent loader.

private static Enumeration<URL>
public void
buildFinished(final BuildEvent
the buildFinished event
event
)

Implements org.apache.tools.ant.BuildListener.buildFinished.

Cleans up any resources held by this classloader at the end of a build.
public void
buildStarted(final BuildEvent
the buildStarted event
event
)

Implements org.apache.tools.ant.BuildListener.buildStarted.

Empty implementation to satisfy the BuildListener interface.
public synchronized void
cleanup()

Cleans up any resources held by this classloader.

public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
protected Class<?>

Returns:

the Class instance created from the given data
defineClassFromData
(final File
the container from which the class data has been read may be a directory or a jar/zip file.
container
,
final byte[]
the bytecode data for the class
classData
,
final String
the name of the class
classname
)

Define a class given its bytes

protected void
definePackage(final File
the file containing the class definition.
container
,
final String
the class name of for which the package information is to be determined.
className
)

Define the package information associated with a class.

protected void
definePackage(final File
the jar file containing the manifest
container
,
final String
the name of the package being defined.
packageName
,
final Manifest
the jar's manifest
manifest
)

Define the package information when the class comes from a jar with a manifest

private Class<?>

Returns:

the required Class object
findBaseClass
(final String
The name of the class to be loaded. Must not be null.
name
)

Finds a system class (which should be loaded from the same classloader as the Ant core).

public Class<?>

Returns:

the required Class object
findClass
(final String
The name of the class to be loaded. Must not be null.
name
)

Overrides java.lang.ClassLoader.findClass.

Searches for and load a class on the classpath of this class loader.
private Class<?>

Returns:

the required Class object
findClassInComponents
(final String
The name of the class to be loaded. Must not be null.
name
)

Finds a class on the given classpath.

protected URL

Returns:

A URL object for reading the resource, or null if the resource could not be found
findResource
(final String
The resource name
name
)

Overrides java.lang.ClassLoader.findResource.

Finds the resource with the given name.
protected Enumeration<URL>

Returns:

an enumeration of URLs for the resources
findResources
(final String
The resource name to search for. Must not be null.
name
)

Overrides java.lang.ClassLoader.findResources.

Returns an enumeration of URLs representing all the resources with the given name by searching the class loader's classpath.
protected Enumeration<URL>

Returns:

an enumeration of URLs for the resources
findResources
(final String
The resource name to search for. Must not be null.
name
,
final boolean
whether to skip searching the parent first - will be false if the method is invoked from getResources(String) or getNamedResources(String) and true if the method is invoked from findResources(String).
skipParent
)

Returns an enumeration of URLs representing all the resources with the given name by searching the class loader's classpath.

public Class<?>

Returns:

the required Class object
forceLoadClass
(final String
The name of the class to be loaded. Must not be null.
classname
)

Loads a class through this class loader even if that class is available on the parent classpath.

public Class<?>

Returns:

the required Class object
forceLoadSystemClass
(final String
The name of the class to be loaded. Must not be null.
classname
)

Loads a class through this class loader but defer to the parent class loader.

private Certificate[]

Returns:

the entry's certificates or null is the container is not a jar or it has no certificates.
getCertificates
(final File
the File from which to read the entry
container
,
final String
the entry of which the certificates are requested
entry
)

Get the certificates for a given jar entry, if it is indeed a jar.

private String

Returns:

the classname in filesystem format (eg java/lang/Integer.class)
getClassFilename
(final String
The class name in dot format (eg java.lang.Integer). Must not be null.
classname
)

Converts the class dot notation to a filesystem equivalent for searching purposes.

private Class<?>

Returns:

the Class object read from the stream.
getClassFromStream
(final InputStream
The stream from which the class is to be read. Must not be null.
stream
,
final String
The name of the class in the stream. Must not be null.
classname
,
final File
the file or directory containing the class.
container
)

Reads a class definition from a stream.

public String

Returns:

the classpath used for this classloader, with elements separated by the path separator for the system.
getClasspath
()

Returns the classpath this classloader will consult.

public ClassLoader

Returns:

classloader
getConfiguredParent
()

Gets the parent as has been specified in the constructor or via setParent.

private Manifest

Returns:

the jar's manifest or null is the container is not a jar or it has no manifest.
getJarManifest
(final File
the File from which a manifest is required.
container
)

Get the manifest from the given jar, if it is indeed a jar and it has a manifest

public Enumeration<URL>

Returns:

possible URLs as enumeration
getNamedResources
(final String
name of the resource
name
)

Finds all the resources with the given name.

public URL

Returns:

a URL for reading the resource, or null if the resource could not be found or the caller doesn't have adequate privileges to get the resource.
getResource
(final String
The name of the resource for which a stream is required. Must not be null.
name
)

Overrides java.lang.ClassLoader.getResource.

Finds the resource with the given name.
public InputStream

Returns:

a stream to the required resource or null if the resource cannot be found on the loader's classpath.
getResourceAsStream
(final String
The name of the resource for which a stream is required. Must not be null.
name
)

Overrides java.lang.ClassLoader.getResourceAsStream.

Returns a stream to read the requested resource name.
public Enumeration<URL>
getResources(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResources.

Finds all the resources with the given name.
private InputStream

Returns:

a stream to the required resource or null if the resource cannot be found in the given file.
getResourceStream
(final File
the file (directory or jar) in which to search for the resource. Must not be null.
file
,
final String
The name of the resource for which a stream is required. Must not be null.
resourceName
)

Returns an inputstream to a given resource in the given file which may either be a directory or a zip file.

protected URL

Returns:

a stream to the required resource or null if the resource cannot be found in the given file object.
getResourceURL
(final File
The file (directory or jar) in which to search for the resource. Must not be null.
file
,
final String
The name of the resource for which a stream is required. Must not be null.
resourceName
)

Returns the URL of a given resource in the given file which may either be a directory or a zip file.

private ClassLoader

Returns:

the root classloader of AntClassLoader.
getRootLoader
()

Used for isolated resource searching.

private URL

Returns:

A URL object for reading the resource, or null if the resource could not be found
getUrl
(String
File to find
name
)

Finds a matching file by iterating through path components.

public static void
initializeClass(final Class<?>
The class to initialize. Must not be null.
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.
protected boolean

Returns:

true if the file is in the class path
isInPath
(final File
the file which is to be checked
component
)

Indicate if the given file is in this loader's path

private boolean

Returns:

whether or not the parent classloader should be checked for a resource before this one is.
isParentFirst
(final String
The name of the resource to check. Must not be null.
resourceName
)

Tests whether or not the parent classloader should be checked for a resource before this one.

private static boolean
isZip(final File file)

private InputStream

Returns:

a stream to the named resource, or null if the resource cannot be found.
loadBaseResource
(final String
The name of the system resource to load. Must not be null.
name
)

Finds a system resource (which should be loaded from the parent classloader).

protected synchronized Class<?>

Returns:

the required Class object
loadClass
(final String
The name of the class to be loaded. Must not be null.
classname
,
final boolean
true if all classes upon which this class depends are to be loaded.
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Loads a class with this class loader.
private InputStream

Returns:

a stream to the required resource or null if the resource cannot be found on the loader's classpath.
loadResource
(final String
The name of the resource for which a stream is required. Must not be null.
name
)

Returns a stream to read the requested resource name from this loader.

protected void
log(final String
The message to log. Should not be null.
message
,
final int
The logging priority of the message.
priority
)

Logs a message through the project object if one has been provided.

public void
messageLogged(final BuildEvent
the messageLogged event
event
)

Implements org.apache.tools.ant.BuildListener.messageLogged.

Empty implementation to satisfy the BuildListener interface.
public static AntClassLoader

Returns:

AntClassLoader
newAntClassLoader
(final ClassLoader
ClassLoader
parent
,
final Project
Project
project
,
final Path
Path
path
,
final boolean
boolean
parentFirst
)

Factory method

private static JarFile

Returns:

Returns a JarFile instance, which is constructed based upon the Java runtime version. Depending on the Java runtime version, the returned instance may or may not be multi-release aware (a feature introduced in Java 9)
newJarFile
(final File
The file representing the jar
file
)

private static boolean
readFully(final File f, final byte[] b)

public void
resetThreadContextLoader()

Resets the current thread's context loader to its original value.

public void
setClassPath(final Path
the search classpath consisting of directories and jar/zip files.
classpath
)

Set the classpath to search for classes to load.

public synchronized void
setIsolated(final boolean
Whether or not this classloader should run in isolated mode.
isolated
)

Sets whether this classloader should run in isolated mode.

public void
setParent(final ClassLoader
the parent class loader.
parent
)

Set the parent for this class loader.

public void
setParentFirst(final boolean
if true, delegate initial class search to the parent classloader.
parentFirst
)

Control whether class lookup is delegated to the parent loader first or after this loader.

public void
setProject(final Project
the project instance
project
)

Set the project associated with this class loader

public void
setThreadContextLoader()

Sets the current thread's context loader to this classloader, storing the current loader value for later resetting.

public void
subBuildFinished(final BuildEvent
the buildFinished event
event
)

Implements org.apache.tools.ant.SubBuildListener.subBuildFinished.

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.
public void
subBuildStarted(final BuildEvent
the buildStarted event
event
)

Implements org.apache.tools.ant.SubBuildListener.subBuildStarted.

Empty implementation to satisfy the BuildListener interface.
public void
targetFinished(final BuildEvent
the targetFinished event
event
)

Implements org.apache.tools.ant.BuildListener.targetFinished.

Empty implementation to satisfy the BuildListener interface.
public void
targetStarted(final BuildEvent
the targetStarted event
event
)

Implements org.apache.tools.ant.BuildListener.targetStarted.

Empty implementation to satisfy the BuildListener interface.
public void
taskFinished(final BuildEvent
the taskFinished event
event
)

Implements org.apache.tools.ant.BuildListener.taskFinished.

Empty implementation to satisfy the BuildListener interface.
public void
taskStarted(final BuildEvent
the taskStarted event
event
)

Implements org.apache.tools.ant.BuildListener.taskStarted.

Empty implementation to satisfy the BuildListener interface.
public String

Returns:

the path that this classloader has.
toString
()

Overrides java.lang.Object.toString.

Returns a String representing this loader.
Inherited from java.lang.ClassLoader:
clearAssertionStatusdefineClassdefineClassdefineClassdefineClassdefinePackagefindClassfindLibraryfindLoadedClassfindResourcefindSystemClassgetClassLoadingLockgetDefinedPackagegetDefinedPackagesgetNamegetPackagegetPackagesgetParentgetPlatformClassLoadergetSystemClassLoadergetSystemResourcegetSystemResourceAsStreamgetSystemResourcesgetUnnamedModuleisRegisteredAsParallelCapableloadClassregisterAsParallelCapableresolveClassresourcessetClassAssertionStatussetDefaultAssertionStatussetPackageAssertionStatussetSigners