Class ClassFinder

java.lang.Object
net.simplace.core.service.ClassFinder

public class ClassFinder extends Object
Scans the class path for classes and resources. The principal use is to determine what subclasses/implementations of a given class/interface exist in the current runtime environment.

This utility class was originally based on Daniel Le Berre's RTSI class.

Author:
Daniel Le Berre, Elliott Wade
  • Method Details

    • getInstance

      public static ClassFinder getInstance()
      Returns:
      The singleton instance
    • refreshLocations

      public final void refreshLocations()
      Rescans the class path, caching all possible package locations.
    • findSubclasses

      public final Vector<Class<?>> findSubclasses(String aStartsWithFilter, String fqcn)
      Finds all classes in the cached class path locations that are assignable to the given class or interface. The class itself is not included. Errors during the search can be retrieved with getErrors().
      Parameters:
      aStartsWithFilter - only packages whose name contains this string are searched (despite the parameter name, it is a contains check); null searches all packages
      fqcn - fully qualified name of the superclass/interface to search for
      Returns:
      the found classes, sorted by name; empty if the name is malformed or the class cannot be loaded
    • exists

      public static boolean exists(String aClassName)
      Checks whether a class can be found by the context class loader of the current thread. The class is not initialized.
      Parameters:
      aClassName - fully qualified class name
      Returns:
      true if the class exists, otherwise false (a warning is logged)
    • findResources

      public Vector<URL> findResources(String aPackage, String aRegexp)
      Finds the locations of a package on the class path (directories or JAR entries) whose URL matches a regular expression.
      Parameters:
      aPackage - package name, e.g. net.simplace.sim
      aRegexp - regular expression the complete URL must match
      Returns:
      the matching URLs, sorted
    • getErrors

      public final List<Throwable> getErrors()
      Returns:
      a copy of the list of errors that occurred during the last search
    • getLocationOf

      public final URL getLocationOf(Class<?> cls)
      The result of the last search is cached in this object, along with the URL that corresponds to each class returned. This method may be called to query the cache for the location at which the given class was found.
      Parameters:
      cls - class to look up
      Returns:
      location of the class, or null if the class was not found during the last search
    • getClasspathLocations

      public final Map<URL,String> getClasspathLocations()
      Determines every package location (directory or JAR entry) on the current class path, together with its package name.
      Returns:
      map from package location to package name
    • getResources

      public Collection<String> getResources(String pattern)
      Collects all resources of all elements of java.class.path whose name matches a regular expression. For directories the canonical file path is matched, for JAR files the entry name. The pattern .* returns all resources.
      Parameters:
      pattern - regular expression to match
      Returns:
      the resources in the order they are found
    • main

      public static void main(String[] args)
      Command line entry point: prints all subclasses of a class found on the class path.
      Parameters:
      args - args[0]: fully qualified superclass name; optional args[1]: package filter