ClasspathUtilspublic class ClasspathUtils extends Object Offers some helper methods on the Path structure in ant.
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();
|
Fields Summary |
---|
public static final String | REUSE_LOADER_REFName of the magic property that controls classloader reuse in Ant 1.4. |
Methods Summary |
---|
public static java.lang.ClassLoader | getClassLoaderForPath(org.apache.tools.ant.Project p, org.apache.tools.ant.types.Reference ref)Convenience overloaded version of {@link
#getClassLoaderForPath(Project, Reference, boolean)}.
Assumes the logical 'false' for the reverseLoader.
return getClassLoaderForPath(p, ref, false);
| public static java.lang.ClassLoader | getClassLoaderForPath(org.apache.tools.ant.Project p, org.apache.tools.ant.types.Reference ref, boolean reverseLoader)Convenience overloaded version of {@link #getClassLoaderForPath(Project, Path,
String, boolean)}.
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.
String pathId = ref.getRefId();
Object path = p.getReference(pathId);
if (!(path instanceof Path)) {
throw new BuildException(
"The specified classpathref "
+ pathId
+ " does not reference a Path.");
}
String loaderId = MagicNames.REFID_CLASSPATH_LOADER_PREFIX + pathId;
return getClassLoaderForPath(p, (Path) path, loaderId, reverseLoader);
| public static java.lang.ClassLoader | getClassLoaderForPath(org.apache.tools.ant.Project p, org.apache.tools.ant.types.Path path, java.lang.String loaderId)Convenience overloaded version of {@link
#getClassLoaderForPath(Project, Path, String, boolean)}.
Assumes the logical 'false' for the reverseLoader.
return getClassLoaderForPath(p, path, loaderId, false);
| public static java.lang.ClassLoader | getClassLoaderForPath(org.apache.tools.ant.Project p, org.apache.tools.ant.types.Path path, java.lang.String loaderId, boolean reverseLoader)Convenience overloaded version of {@link
#getClassLoaderForPath(Project, Path, String, boolean, boolean)}.
Sets value for 'reuseLoader' to true if the magic property
has been set.
return getClassLoaderForPath(p, path, loaderId, reverseLoader,
isMagicPropertySet(p));
| public static java.lang.ClassLoader | getClassLoaderForPath(org.apache.tools.ant.Project p, org.apache.tools.ant.types.Path path, java.lang.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.
ClassLoader cl = null;
// magic property
if (loaderId != null && reuseLoader) {
Object reusedLoader = p.getReference(loaderId);
if (reusedLoader != null
&& !(reusedLoader instanceof ClassLoader)) {
throw new BuildException("The specified loader id " + loaderId
+ " does not reference a class loader");
}
cl = (ClassLoader) reusedLoader;
}
if (cl == null) {
cl = getUniqueClassLoaderForPath(p, path, reverseLoader);
if (loaderId != null && reuseLoader) {
p.addReference(loaderId, cl);
}
}
return cl;
| public static org.apache.tools.ant.util.ClasspathUtils$Delegate | getDelegate(org.apache.tools.ant.ProjectComponent component)Obtains a delegate that helps out with classic classpath configuration.
return new Delegate(component);
| public static java.lang.ClassLoader | getUniqueClassLoaderForPath(org.apache.tools.ant.Project p, org.apache.tools.ant.types.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.
AntClassLoader acl = p.createClassLoader(path);
if (reverseLoader) {
acl.setParentFirst(false);
acl.addJavaLibraries();
}
return acl;
| private static boolean | isMagicPropertySet(org.apache.tools.ant.Project p)Checks for the magic property that enables class loader reuse
for and in Ant 1.5 and earlier.
return p.getProperty(REUSE_LOADER_REF) != null;
| public static java.lang.Object | newInstance(java.lang.String className, java.lang.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.
return newInstance(className, userDefinedLoader, Object.class);
| public static java.lang.Object | newInstance(java.lang.String className, java.lang.ClassLoader userDefinedLoader, java.lang.Class 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.
try {
Class clazz = Class.forName(className, true, userDefinedLoader);
Object o = clazz.newInstance();
if (!expectedType.isInstance(o)) {
throw new BuildException(
"Class of unexpected Type: "
+ className
+ " expected :"
+ expectedType);
}
return o;
} catch (ClassNotFoundException e) {
throw new BuildException(
"Class not found: "
+ className,
e);
} catch (InstantiationException e) {
throw new BuildException(
"Could not instantiate "
+ className
+ ". Specified class should have a no "
+ "argument constructor.",
e);
} catch (IllegalAccessException e) {
throw new BuildException(
"Could not instantiate "
+ className
+ ". Specified class should have a "
+ "public constructor.",
e);
} catch (LinkageError e) {
throw new BuildException(
"Class "
+ className
+ " could not be loaded because of an invalid dependency.",
e);
}
|
|