Class ResolverUtil<T>
ResolverUtil is used to locate classes that are available in the/a class path and meet
 arbitrary conditions. The two most common conditions are that a class implements/extends
 another class, or that is it annotated with a specific annotation. However, through the use
 of the ResolverUtil.Test class it is possible to search using arbitrary conditions.
A ClassLoader is used to locate all locations (directories and jar files) in the class
 path that contain classes within certain packages, and then to load those classes and
 check them. By default the ClassLoader returned by
  Thread.currentThread().getContextClassLoader() is used, but this can be overridden
 by calling setClassLoader(ClassLoader) prior to invoking any of the find()
 methods.
General searches are initiated by calling the
 find(ResolverUtil.Test, String...) ()} method and supplying
 a package name and a Test instance. This will cause the named package and all sub-packages
 to be scanned for classes that meet the test. There are also utility methods for the common
 use cases of scanning multiple packages for extensions of particular classes, or classes
 annotated with a specific annotation.
The standard usage pattern for the ResolverUtil class is as follows:
ResolverUtil<ActionBean> resolver = new ResolverUtil<ActionBean>(); resolver.findImplementation(ActionBean.class, pkg1, pkg2); resolver.find(new CustomTest(), pkg1); resolver.find(new CustomTest(), pkg2); Collection<ActionBean> beans = resolver.getClasses();
This class was copied from Stripes - http://stripes.mc4j.org/confluence/display/stripes/Home
- Author:
- Tim Fennell
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classA Test that checks to see if each class is annotated with a specific annotation.static classstatic classA Test that checks to see if each class is assignable to the provided class.static classA Test that checks to see if each class name ends with the provided suffix.static classstatic classstatic interfaceA simple interface that specifies how to test classes to determine if they are to be included in the results produced by the ResolverUtil.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidaddIfMatching(ResolverUtil.Test test, String fqn) Add the class designated by the fully qualified class name provided to the set of resolved classes if and only if it is approved by the Test supplied.voidfind(ResolverUtil.Test test, String... packageNames) Attempts to discover classes that pass the test.voidfindAnnotated(Class<? extends Annotation> annotation, String... packageNames) Attempts to discover classes that are annotated with to the annotation.voidfindImplementations(Class parent, String... packageNames) Attempts to discover classes that are assignable to the type provided.voidfindInPackage(ResolverUtil.Test test, String packageName) Scans for classes starting at the package provided and descending into subpackages.voidfindNamedResource(String name, String... pathNames) voidfindSuffix(String suffix, String... packageNames) Attempts to discover classes who's name ends with the provided suffix.Provides access to the classes discovered so far.Returns the classloader that will be used for scanning for classes.voidsetClassLoader(ClassLoader classloader) Sets an explicit ClassLoader that should be used when scanning for classes.
- 
Constructor Details- 
ResolverUtilpublic ResolverUtil()
 
- 
- 
Method Details- 
getClassesProvides access to the classes discovered so far. If no calls have been made to any of thefind()methods, this set will be empty.- Returns:
- the set of classes that have been discovered.
 
- 
getResources
- 
getClassLoaderReturns the classloader that will be used for scanning for classes. If no explicit ClassLoader has been set by the calling, the context class loader will be used.- Returns:
- the ClassLoader that will be used to scan for classes
 
- 
setClassLoaderSets an explicit ClassLoader that should be used when scanning for classes. If none is set then the context classloader will be used.- Parameters:
- classloader- a ClassLoader to use when scanning for classes
 
- 
findImplementationsAttempts to discover classes that are assignable to the type provided. In the case that an interface is provided this method will collect implementations. In the case of a non-interface class, subclasses will be collected. Accumulated classes can be accessed by callinggetClasses().- Parameters:
- parent- the class of interface to find subclasses or implementations of
- packageNames- one or more package names to scan (including subpackages) for classes
 
- 
findSuffixAttempts to discover classes who's name ends with the provided suffix. Accumulated classes can be accessed by callinggetClasses().- Parameters:
- suffix- The class name suffix to match
- packageNames- one or more package names to scan (including subpackages) for classes
 
- 
findAnnotatedAttempts to discover classes that are annotated with to the annotation. Accumulated classes can be accessed by callinggetClasses().- Parameters:
- annotation- the annotation that should be present on matching classes
- packageNames- one or more package names to scan (including subpackages) for classes
 
- 
findNamedResource
- 
findAttempts to discover classes that pass the test. Accumulated classes can be accessed by callinggetClasses().- Parameters:
- test- the test to determine matching classes
- packageNames- one or more package names to scan (including subpackages) for classes
 
- 
findInPackageScans for classes starting at the package provided and descending into subpackages. Each class is offered up to the Test as it is discovered, and if the Test returns true the class is retained. Accumulated classes can be fetched by callinggetClasses().- Parameters:
- test- an instance of- ResolverUtil.Testthat will be used to filter classes
- packageName- the name of the package from which to start scanning for classes, e.g.- net.sourceforge.stripes
 
- 
addIfMatchingAdd the class designated by the fully qualified class name provided to the set of resolved classes if and only if it is approved by the Test supplied.- Parameters:
- test- the test used to determine if the class matches
- fqn- the fully qualified name of a class
 
 
-