net.sourceforge.jfacets.util
Class ResolverUtil<T>

java.lang.Object
  extended by net.sourceforge.jfacets.util.ResolverUtil<T>

public class ResolverUtil<T>
extends java.lang.Object

Utility for finding classes in the classpath. Borrowed from the Stripes Framework : http://www.stripesframework.org ! Thanks to Tim for that very useful piece of code :-)

Author:
Tim Fennell

Nested Class Summary
static class ResolverUtil.AnnotatedWith
          A Test that checks to see if each class is annotated with a specific annotation.
static class ResolverUtil.IsA
          A Test that checks to see if each class is assignable to the provided class.
static interface ResolverUtil.Test
          A simple interface that specifies how to test classes to determine if they are to be included in the results produced by the ResolverUtil.
 
Constructor Summary
ResolverUtil()
           
 
Method Summary
protected  void addIfMatching(ResolverUtil.Test test, java.lang.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.
 void find(ResolverUtil.Test test, java.lang.String packageName)
          Scans for classes starting at the package provided and descending into subpackages.
 void findAnnotated(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String... packageNames)
          Attempts to discover classes that are annotated with to the annotation.
 void findImplementations(java.lang.Class parent, java.lang.String... packageNames)
          Attempts to discover classes that are assignable to the type provided.
 java.util.Set<java.lang.Class<? extends T>> getClasses()
          Provides access to the classes discovered so far.
 java.lang.ClassLoader getClassLoader()
          Returns the classloader that will be used for scanning for classes.
 void setClassLoader(java.lang.ClassLoader classloader)
          Sets an explicit ClassLoader that should be used when scanning for classes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResolverUtil

public ResolverUtil()
Method Detail

getClasses

public java.util.Set<java.lang.Class<? extends T>> getClasses()
Provides access to the classes discovered so far. If no calls have been made to any of the find() methods, this set will be empty.

Returns:
the set of classes that have been discovered.

getClassLoader

public java.lang.ClassLoader getClassLoader()
Returns 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

setClassLoader

public void setClassLoader(java.lang.ClassLoader classloader)
Sets 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

findImplementations

public void findImplementations(java.lang.Class parent,
                                java.lang.String... packageNames)
Attempts 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 calling getClasses().

Parameters:
parent - the class of interface to find subclasses or implementations of
packageNames - one or more package names to scan (including subpackages) for classes

findAnnotated

public void findAnnotated(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
                          java.lang.String... packageNames)
Attempts to discover classes that are annotated with to the annotation. Accumulated classes can be accessed by calling getClasses().

Parameters:
annotation - the annotation that should be present on matching classes
packageNames - one or more package names to scan (including subpackages) for classes

find

public void find(ResolverUtil.Test test,
                 java.lang.String packageName)
Scans 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 calling getClasses().

Parameters:
test - an instance of ResolverUtil.Test that will be used to filter classes
packageName - the name of the package from which to start scanning for classes, e.g. net.sourceforge.stripes

addIfMatching

protected void addIfMatching(ResolverUtil.Test test,
                             java.lang.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.

Parameters:
test - the test used to determine if the class matches
fqn - the fully qualified name of a class


Copyright © 2011. All Rights Reserved.