org.apache.tools.ant.util

## Class ClasspathUtils

- java.lang.Object
- - org.apache.tools.ant.util.ClasspathUtils

-     public class ClasspathUtils
      extends java.lang.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();

  Since:
    Ant 1.6

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ClasspathUtils.Delegate` Delegate that helps out any specific ProjectComponent that needs dynamic classloading.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String` | `REUSE_LOADER_REF` Name of the magic property that controls classloader reuse in Ant 1.4.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.lang.ClassLoader` | `getClassLoaderForPath(Project p, Path path, java.lang.String loaderId)` Convenience overloaded version of `getClassLoaderForPath(Project, Path, String, boolean)`.
    `static java.lang.ClassLoader` | `getClassLoaderForPath(Project p, Path path, java.lang.String loaderId, boolean reverseLoader)` Convenience overloaded version of `getClassLoaderForPath(Project, Path, String, boolean, boolean)`.
    `static java.lang.ClassLoader` | `getClassLoaderForPath(Project p, Path path, java.lang.String loaderId, boolean reverseLoader, boolean reuseLoader)` Gets a classloader that loads classes from the classpath defined in the path argument.
    `static java.lang.ClassLoader` | `getClassLoaderForPath(Project p, Reference ref)` Convenience overloaded version of `getClassLoaderForPath(Project, Reference, boolean)`.
    `static java.lang.ClassLoader` | `getClassLoaderForPath(Project p, Reference ref, boolean reverseLoader)` Convenience overloaded version of `getClassLoaderForPath(Project, Path, String, boolean)`.
    `static ClasspathUtils.Delegate` | `getDelegate(ProjectComponent component)` Obtains a delegate that helps out with classic classpath configuration.
    `static java.lang.ClassLoader` | `getUniqueClassLoaderForPath(Project p, Path path, boolean reverseLoader)` Gets a fresh, different, previously unused classloader that uses the passed path as its classpath.
    `static java.lang.Object` | `newInstance(java.lang.String className, java.lang.ClassLoader userDefinedLoader)` Creates a fresh object instance of the specified classname.
    `static <T> T` | `newInstance(java.lang.String className, java.lang.ClassLoader userDefinedLoader, java.lang.Class<T> expectedType)` Creates a fresh object instance of the specified classname.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### REUSE_LOADER_REF

          public static final java.lang.String REUSE_LOADER_REF

      Name of the magic property that controls classloader reuse in Ant 1.4.
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### getClassLoaderForPath

          public static java.lang.ClassLoader getClassLoaderForPath(Project p,
                                                                    Reference ref)

      Convenience overloaded version of `getClassLoaderForPath(Project, Reference, boolean)`.

      Assumes the logical 'false' for the reverseLoader.

      Parameters:
        `p` - the project
        `ref` - the reference
      Returns:
        The class loader

    - #### getClassLoaderForPath

          public static java.lang.ClassLoader getClassLoaderForPath(Project p,
                                                                    Reference ref,
                                                                    boolean reverseLoader)

      Convenience overloaded version of `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.

      Parameters:
        `p` - current Ant project
        `ref` - Reference to Path structure
        `reverseLoader` - if set to true this new loader will take precedence over its parent (which is contra the regular classloader behaviour)
      Returns:
        The class loader

    - #### getClassLoaderForPath

          public static java.lang.ClassLoader getClassLoaderForPath(Project p,
                                                                    Path path,
                                                                    java.lang.String loaderId)

      Convenience overloaded version of `getClassLoaderForPath(Project, Path, String, boolean)`.

      Assumes the logical 'false' for the reverseLoader.

      Parameters:
        `p` - current Ant project
        `path` - the path
        `loaderId` - the loader id string
      Returns:
        The class loader

    - #### getClassLoaderForPath

          public static java.lang.ClassLoader getClassLoaderForPath(Project p,
                                                                    Path path,
                                                                    java.lang.String loaderId,
                                                                    boolean reverseLoader)

      Convenience overloaded version of `getClassLoaderForPath(Project, Path, String, boolean, boolean)`.

      Sets value for 'reuseLoader' to true if the magic property has been set.

      Parameters:
        `p` - the project
        `path` - the path
        `loaderId` - the loader id string
        `reverseLoader` - if set to true this new loader will take precedence over its parent (which is contra the regular classloader behaviour)
      Returns:
        The class loader

    - #### getClassLoaderForPath

          public static java.lang.ClassLoader getClassLoaderForPath(Project p,
                                                                    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.

      Parameters:
        `p` - Ant Project where the handled components are living in.
        `path` - Path object to be used as classpath for this classloader
        `loaderId` - identification for this Loader,
        `reverseLoader` - if set to true this new loader will take precedence over its parent (which is contra the regular classloader behaviour)
        `reuseLoader` - if true reuse the loader if it is found
      Returns:
        ClassLoader that uses the Path as its classpath.

    - #### getUniqueClassLoaderForPath

          public static java.lang.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.

      Parameters:
        `p` - Ant Project where the handled components are living in.
        `path` - the classpath for this loader
        `reverseLoader` - if set to true this new loader will take precedence over its parent (which is contra the regular classloader behaviour)
      Returns:
        The fresh, different, previously unused class loader.

    - #### newInstance

          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.

      Parameters:
        `className` - the full qualified class name to load.
        `userDefinedLoader` - the classloader to use.
      Returns:
        The fresh object instance
      Throws:
        `BuildException` - when loading or instantiation failed.

    - #### newInstance

          public static <T> T newInstance(java.lang.String className,
                                          java.lang.ClassLoader userDefinedLoader,
                                          java.lang.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.

      Type Parameters:
        `T` - desired type
      Parameters:
        `className` - the full qualified class name to load.
        `userDefinedLoader` - the classloader to use.
        `expectedType` - 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)
      Returns:
        The fresh object instance
      Throws:
        `BuildException` - when loading or instantiation failed.
      Since:
        Ant 1.7

    - #### getDelegate

          public static ClasspathUtils.Delegate getDelegate(ProjectComponent component)

      Obtains a delegate that helps out with classic classpath configuration.
      Parameters:
        `component` - your projectComponent that needs the assistance
      Returns:
        the helper, delegate.
      See Also:
        `ClasspathUtils.Delegate`