Class ClassFinder
java.lang.Object
net.simplace.core.service.ClassFinder
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 Summary
Modifier and TypeMethodDescriptionstatic booleanChecks whether a class can be found by the context class loader of the current thread.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.findSubclasses(String aStartsWithFilter, String fqcn) Finds all classes in the cached class path locations that are assignable to the given class or interface.Determines every package location (directory or JAR entry) on the current class path, together with its package name.static ClassFinderfinal URLgetLocationOf(Class<?> cls) The result of the last search is cached in this object, along with the URL that corresponds to each class returned.getResources(String pattern) Collects all resources of all elements ofjava.class.pathwhose name matches a regular expression.static voidCommand line entry point: prints all subclasses of a class found on the class path.final voidRescans the class path, caching all possible package locations.
-
Method Details
-
getInstance
- Returns:
- The singleton instance
-
refreshLocations
public final void refreshLocations()Rescans the class path, caching all possible package locations. -
findSubclasses
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 withgetErrors().- Parameters:
aStartsWithFilter- only packages whose name contains this string are searched (despite the parameter name, it is a contains check);nullsearches all packagesfqcn- 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
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:
trueif the class exists, otherwisefalse(a warning is logged)
-
findResources
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.simaRegexp- regular expression the complete URL must match- Returns:
- the matching URLs, sorted
-
getErrors
-
getLocationOf
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
nullif the class was not found during the last search
-
getClasspathLocations
-
getResources
Collects all resources of all elements ofjava.class.pathwhose 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
Command line entry point: prints all subclasses of a class found on the class path.- Parameters:
args-args[0]: fully qualified superclass name; optionalargs[1]: package filter
-