The basic idea behind this utility class is to use it from inside the different Ant objects (and user defined objects) that need classLoading for their operation. Normally those would have a setClasspathRef() {for the @classpathref} and/or a createClasspath() {for the nested <classpath>} Typically one would have in your Ant Task or DataType
ClasspathUtils.Delegate cpDelegate;
public void init() {
this.cpDelegate = ClasspathUtils.getDelegate(this);
super.init();
}
public void setClasspathRef(Reference r) {
this.cpDelegate.setClasspathRef(r);
}
public Path createClasspath() {
return this.cpDelegate.createClasspath();
}
public void setClassname(String fqcn) {
this.cpDelegate.setClassname(fqcn);
}
At execution time, when you actually need the classloading you can just:
Object o = this.cpDelegate.newInstance();
Modifier and Type | Class and Description |
---|---|
public static class | ClasspathUtils.Delegate
Delegate that helps out any specific ProjectComponent that needs dynamic classloading. |
Modifier and Type | Field and Description |
---|---|
public static final String | REUSE_LOADER_REF
Name of the magic property that controls classloader reuse in Ant 1.4. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static ClassLoader | Returns: The class loaderthe project p, Reference the reference ref)Convenience overloaded version of |
public static ClassLoader | Returns: The class loadercurrent Ant project p, Reference Reference to Path structure ref, boolean if set to true this new loader will take
precedence over its parent (which is contra the regular
classloader behaviour) reverseLoader)Convenience overloaded version of |
public static ClassLoader | Returns: The class loadercurrent Ant project p, Path the path path, String the loader id string loaderId)Convenience overloaded version of |
public static ClassLoader | Returns: The class loaderthe project p, Path the path path, String the loader id string loaderId, boolean if set to true this new loader will take
precedence over its parent (which is contra the regular
classloader behaviour) reverseLoader)Convenience overloaded version of |
public static ClassLoader | Returns: ClassLoader that uses the Path as its classpath.Ant Project where the handled components are living in. p, Path Path object to be used as classpath for this classloader path, String identification for this Loader, loaderId, boolean if set to true this new loader will take
precedence over its parent (which is contra the regular
classloader behaviour) reverseLoader, boolean if true reuse the loader if it is found reuseLoader)Gets a classloader that loads classes from the classpath defined in the path argument. |
public static ClasspathUtils. | Returns: the helper, delegate.your projectComponent that needs the assistance component)Obtains a delegate that helps out with classic classpath configuration. |
public static ClassLoader | Returns: The fresh, different, previously unused class loader.Ant Project where the handled components are living in. p, Path the classpath for this loader path, boolean if set to true this new loader will take
precedence over its parent (which is contra the regular
classloader behaviour) reverseLoader)Gets a fresh, different, previously unused classloader that uses the passed path as its classpath. |
private static boolean | isMagicPropertySet(Project p)
Checks for the magic property that enables class loader reuse for <taskdef> and <typedef> in Ant 1.5 and earlier. |
public static Object | Returns: The fresh object instancethe full qualified class name to load. className, ClassLoader the classloader to use. userDefinedLoader)Creates a fresh object instance of the specified classname. |
public static < desired type T> T | Returns: The fresh object instancethe full qualified class name to load. className, ClassLoader the classloader to use. userDefinedLoader, Class<T> the Class that the result should be assignment
compatible with. (No ClassCastException will be thrown in case
the result of this method is casted to the expectedType) expectedType)Creates a fresh object instance of the specified classname. |
REUSE_LOADER_REF | back to summary |
---|---|
public static final String REUSE_LOADER_REF Name of the magic property that controls classloader reuse in Ant 1.4. |
ClasspathUtils | back to summary |
---|---|
private ClasspathUtils() |
getClassLoaderForPath | back to summary |
---|---|
public static ClassLoader getClassLoaderForPath(Project p, Reference ref) Convenience overloaded version of Assumes the logical 'false' for the reverseLoader.
|
getClassLoaderForPath | back to summary |
---|---|
public static ClassLoader getClassLoaderForPath(Project p, Reference ref, boolean reverseLoader) Convenience overloaded version of Delegates to the other one after extracting the referenced Path from the Project. This checks also that the passed Reference is pointing to a Path all right.
|
getClassLoaderForPath | back to summary |
---|---|
public static ClassLoader getClassLoaderForPath(Project p, Path path, String loaderId) Convenience overloaded version of Assumes the logical 'false' for the reverseLoader.
|
getClassLoaderForPath | back to summary |
---|---|
public static ClassLoader getClassLoaderForPath(Project p, Path path, String loaderId, boolean reverseLoader) Convenience overloaded version of Sets value for 'reuseLoader' to true if the magic property has been set.
|
getClassLoaderForPath | back to summary |
---|---|
public static ClassLoader getClassLoaderForPath(Project p, Path path, String loaderId, boolean reverseLoader, boolean reuseLoader) Gets a classloader that loads classes from the classpath defined in the path argument. Based on the setting of the magic property 'ant.reuse.loader' this will try to reuse the previously created loader with that id, and of course store it there upon creation.
|
getDelegate | back to summary |
---|---|
public static ClasspathUtils. Obtains a delegate that helps out with classic classpath configuration.
|
getUniqueClassLoaderForPath | back to summary |
---|---|
public static ClassLoader getUniqueClassLoaderForPath(Project p, Path path, boolean reverseLoader) Gets a fresh, different, previously unused classloader that uses the passed path as its classpath. This method completely ignores the ant.reuse.loader magic property and should be used with caution.
|
isMagicPropertySet | back to summary |
---|---|
private static boolean isMagicPropertySet(Project p) Checks for the magic property that enables class loader reuse
for |
newInstance | back to summary |
---|---|
public static Object newInstance(String className, ClassLoader userDefinedLoader) Creates a fresh object instance of the specified classname. This uses the userDefinedLoader to load the specified class, and then makes an instance using the default no-argument constructor.
|
newInstance | back to summary |
---|---|
public static <T> T newInstance(String className, ClassLoader userDefinedLoader, Class<T> expectedType) Creates a fresh object instance of the specified classname. This uses the userDefinedLoader to load the specified class, and then makes an instance using the default no-argument constructor.
|
Ant ProjectComponents that need a to be able to dynamically load Classes and instantiate them often expose the following ant syntax sugar:
This class functions as a delegate handling the configuration issues for this recurring pattern. Its usage pattern, as the name suggests, is delegation rather than inheritance.
Modifier and Type | Field and Description |
---|---|
private String | |
private Path | |
private String | |
private final ProjectComponent | |
private String | |
private boolean |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public Path | |
public ClassLoader | |
public String | Returns: a loader identifier.Computes the loaderId based on the configuration of the component. |
public Path | |
private Project | |
public boolean | |
public Object | Returns: the fresh instantiated object.Helper method obtaining a fresh instance of the class specified in the @classname and using the specified classpath. |
public void | setClassname(String
the name of the class to load. fcqn)Delegate method handling the @classname attribute. |
public void | setClasspath(Path
the path to use for the classpath. classpath)This method is a Delegate method handling the @classpath attribute. |
public void | setClasspathref(Reference
the reference to the classpath. r)Delegate method handling the @classpathref attribute. |
public void | |
public void | setReverseLoader(boolean
if true reverse the order of looking up a class. reverseLoader)Delegate method handling the @reverseLoader attribute. |
className | back to summary |
---|---|
private String className |
classpath | back to summary |
---|---|
private Path classpath |
classpathId | back to summary |
---|---|
private String classpathId |
component | back to summary |
---|---|
private final ProjectComponent component |
loaderId | back to summary |
---|---|
private String loaderId |
reverseLoader | back to summary |
---|---|
private boolean reverseLoader |
Delegate | back to summary |
---|---|
pack-priv Delegate(ProjectComponent component) Construct a Delegate
|
createClasspath | back to summary |
---|---|
public Path createClasspath() Delegate method handling the <classpath> tag. This nested path-like structure can set a path to add to the classpath.
|
getClassLoader | back to summary |
---|---|
public ClassLoader getClassLoader() Finds or creates the classloader for this object.
|
getClassLoadId | back to summary |
---|---|
public String getClassLoadId() Computes the loaderId based on the configuration of the component.
|
getClasspath | back to summary |
---|---|
public Path getClasspath() The classpath.
|
getContextProject | back to summary |
---|---|
private Project getContextProject() The project of the ProjectComponent we are working for. |
isReverseLoader | back to summary |
---|---|
public boolean isReverseLoader() Get the reverseLoader setting.
|
newInstance | back to summary |
---|---|
public Object newInstance() Helper method obtaining a fresh instance of the class specified in the @classname and using the specified classpath.
|
setClassname | back to summary |
---|---|
public void setClassname(String fcqn) Delegate method handling the @classname attribute. This attribute sets the full qualified class name of the class to load and instantiate.
|
setClasspath | back to summary |
---|---|
public void setClasspath(Path classpath) This method is a Delegate method handling the @classpath attribute. This attribute can set a path to add to the classpath.
|
setClasspathref | back to summary |
---|---|
public void setClasspathref(Reference r) Delegate method handling the @classpathref attribute. This attribute can add a referenced path-like structure to the classpath.
|
setLoaderRef | back to summary |
---|---|
public void setLoaderRef(Reference r) Sets the loaderRef.
|
setReverseLoader | back to summary |
---|---|
public void setReverseLoader(boolean reverseLoader) Delegate method handling the @reverseLoader attribute. This attribute can set a boolean indicating that the used classloader should NOT follow the classical parent-first scheme. By default this is supposed to be false. Caution: this behaviour is contradictory to the normal way classloaders work. Do not let your ProjectComponent use it if you are not really sure.
|