@Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) public class AntClassLoader extends 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(ClassLoader parent,
boolean parentFirst)
Creates an empty class loader.
|
AntClassLoader(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(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(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(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 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,
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.
|
Enumeration |
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 |
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.
|
ClassLoader |
getConfiguredParent()
Gets the parent as has been specified in the constructor or via
setParent.
|
Enumeration |
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.
|
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 URL |
getUrl(Iterable<File> pathComponents,
String name)
Finds a matching file by iterating pathComponents.
|
static void |
initializeClass(Class theClass)
Deprecated.
since 1.6.x.
Use Class.forName with initialize=true instead.
|
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(org.apache.tools.ant.BuildEvent event)
Empty implementation to satisfy the BuildListener interface.
|
static AntClassLoader |
newAntClassLoader(ClassLoader parent,
org.apache.tools.ant.Project project,
org.apache.tools.ant.types.Path path,
boolean parentFirst)
Factory method
|
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(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.
|
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(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(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(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(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(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(String pathElement) throws org.apache.tools.ant.BuildException
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(File file)
file
- the jar file or directory to add.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 String getClasspath()
public void setIsolated(boolean isolated)
isolated
- Whether or not this classloader should run in
isolated mode.@Deprecated public static void initializeClass(Class theClass)
theClass
- The class to initialize.
Must not be null
.public void addSystemPackageRoot(String packageRoot)
packageRoot
- The root of all packages to be included.
Should not be null
.public void addLoaderPackageRoot(String packageRoot)
packageRoot
- The root of all packages to be included.
Should not be null
.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 InputStream getResourceAsStream(String name)
getResourceAsStream
in class 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 URL getResource(String name)
getResource
in class 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.@CheckForNull protected URL getUrl(Iterable<File> pathComponents, String name)
pathComponents
- Path to a folder, split into the individual folder namesname
- File to findpublic Enumeration getNamedResources(String name) throws IOException
Would override getResources if that wasn't final in Java 1.4.
IOException
public Enumeration findResources(String name) throws IOException
findResources
in class ClassLoader
name
- The resource name to search for.
Must not be null
.IOException
- if I/O errors occurs (can't happen)protected Enumeration 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)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 Class loadClass(String classname, boolean resolve) throws ClassNotFoundException
loadClass
in class 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 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, 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 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 boolean isInPath(File component)
component
- the file which is to be checkedpublic void cleanup()
public ClassLoader getConfiguredParent()
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 String toString()
String
representing this loader.public static AntClassLoader newAntClassLoader(ClassLoader parent, org.apache.tools.ant.Project project, org.apache.tools.ant.types.Path path, boolean parentFirst)
Copyright © 2004–2021. All rights reserved.