Class ConfigurableClassLoader

java.lang.Object
java.lang.ClassLoader
eu.bandm.tools.util.classes.ConfigurableClassLoader

public class ConfigurableClassLoader extends ClassLoader
A class loader that mimics the -classpath behavior of java. It supports loading from local directories and jar files, also honoring Class-Path manifest entries.
  • Field Details

    • sources

      protected final List<ConfigurableClassLoader.ClassSource> sources
      Internal list of all sources. These will be searched in sequential order for the first found class.
    • local

      protected final Map<String,Class<?>> local
      Directory of all classes which already have been delivered by this class loader.
  • Constructor Details

    • ConfigurableClassLoader

      public ConfigurableClassLoader()
      Default constructor. Its result has no source from which to load a class. At least one must be added by calling addSource(String) or addSource(String[]) explicitly.
    • ConfigurableClassLoader

      public ConfigurableClassLoader(String... classpath)
      Constructor which sets up the sources for class loading according to its string atguments. These are parsed as described with addSource(String).
      Parameters:
      classpath - array of strings, each standing for places to load classes from.
  • Method Details

    • watchClass

      protected void watchClass(String name, byte[] data)
      Callback-hook to be overwritten by a user for diagnosis. Is called whenever a class is successfully loaded by findClass(String).
    • addSource

      public void addSource(String... classpath)
      Executes addSource(String) for all arguments.
      Parameters:
      classpath - list of strings, see addSource(String).
    • addSource

      public void addSource(String classpath)
      Add one or more sources. The string can have the format of a Java "-classpath" argument and will be split by File.pathSeparatorChar. Otherwise it stands for one directory from which to load classes. If it stands for a single (non-directory) file, this must be a ".jar" file.
      Parameters:
      classpath - either a single source (directory or jar file) or multiples joined by File.pathSeparatorChar
    • sources

      Returns all currently known sources to search for class files.
    • getLocalClasses

      public final Map<String,Class<?>> getLocalClasses()
      Get an unmodifiable copy of the directory of all classes which already have been delivered by this class loader.
    • loadClass

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Overrides:
      loadClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • findClass

      protected Class<?> findClass(String name) throws ClassNotFoundException
      Central point of service: return the loaded class object for the class with the given name. All sources contained in sources are searched in sequential order for the first hit, by calling their findClass(name) method is called.
      Overrides:
      findClass in class ClassLoader
      Parameters:
      name - the "binary name" of the class to look for, see the discussion with ClassLoader
      Returns:
      the class object
      Throws:
      ClassNotFoundException - if no class with the given name is found in any source.
    • getClassData

      public byte[] getClassData(String name) throws ClassNotFoundException
      Central point of service: return all class data for the class with the given name. All sources contained in sources are searched in sequential order for the first hit.
      Parameters:
      name - of the class to look for
      Returns:
      the byte array containing the complete byte code of the class.
      Throws:
      ClassNotFoundException - if no class with the given name is found in any source.