Package org.rribbit.creation
Class AbstractClassBasedListenerObjectCreator
java.lang.Object
org.rribbit.creation.ObjectBasedListenerObjectCreator
org.rribbit.creation.AbstractClassBasedListenerObjectCreator
- All Implemented Interfaces:
ListenerObjectCreator
- Direct Known Subclasses:
InstantiatingClassBasedListenerObjectCreator
,SpringBeanClassBasedListenerObjectCreator
public abstract class AbstractClassBasedListenerObjectCreator
extends ObjectBasedListenerObjectCreator
This
ListenerObjectCreator
creates ListenerObject
s from classes. Users can pass in Class
es or packagenames and this class will scan the Class
es
and create ListenerObject
s for the public methods that are annotated with Listener
. Note that public methods inherited from superclasses and superinterfaces will also be
scanned. This means that users must take care not to scan a method twice, once as a method of a class and once as a method of a superclass, by passing a class/interface and its
superclass/superinterface separately to this ListenerObjectCreator
.
Please note that in Java, method annotations are NOT inherited. This means that, if you override/implement a method in a subclass or subinterface, and the overriding/implementing method
does not have the annotation, then that method will not inherit it. If a class or interface just inherits a method, without overriding it, then the annotation WILL exist.
This class does NOT set the execution target for the created ListenerObject
s. That is the responsibility of the implementations of this abstract class.- Author:
- G.J. Schouten
-
Field Summary
Fields inherited from class org.rribbit.creation.ObjectBasedListenerObjectCreator
listenerObjects, observers
-
Constructor Summary
ConstructorDescriptionAbstractClassBasedListenerObjectCreator
(Class<?>... classes) CallsaddClass(Class)
on each given class.AbstractClassBasedListenerObjectCreator
(Collection<Class<?>> excludedClasses, boolean scanSubPackages, String... packageNames) CallsaddPackage(String, boolean)
on each given package name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Scans all public methods in the givenClass
and createsListenerObject
s for them if they have aListener
annotation, provided thatgetTargetObjectForClass(Class)
can provide a suitable execution target.void
addPackage
(String packageName, boolean scanSubPackages) Scans all classes in the given package and callsaddClass(Class)
on each of them if they're not contained in the collection of excluded classes.void
excludeClass
(Class<?> excludedClass) Adds 'excludedClass' to the list of classes that are excluded from scanning when package are scanned.protected Collection
<Class<?>> findClassesInDirectory
(File directory, String packageName, boolean scanSubPackages) Method used to find all classes in a given directory.protected Collection
<Class<?>> findClassesInZipFile
(ZipFile zipFile, String path, boolean scanSubPackages) Method used to find all classes in a given zipFile.protected Collection
<Class<?>> getClasses
(String packageName, boolean scanSubPackages) Scans all classes accessible in the given package, using theClassLoader
associated with thisAbstractClassBasedListenerObjectCreator
.protected ClassLoader
This method gets theClassLoader
that is used to get the classes in a package.protected abstract Object
getTargetObjectForClass
(Class<?> clazz) Gets a target executionObject
for the given class to be used by aListenerObject
to execute itsMethod
.Methods inherited from class org.rribbit.creation.ObjectBasedListenerObjectCreator
addObject, getIncompleteListenerObjectsFromClass, getListenerObjects, notifyObserversOnClassAdded, registerObserver
-
Field Details
-
excludedClasses
-
-
Constructor Details
-
AbstractClassBasedListenerObjectCreator
CallsaddClass(Class)
on each given class.- Parameters:
classes
-
-
AbstractClassBasedListenerObjectCreator
public AbstractClassBasedListenerObjectCreator(Collection<Class<?>> excludedClasses, boolean scanSubPackages, String... packageNames) CallsaddPackage(String, boolean)
on each given package name.- Parameters:
excludedClasses
- the classes to be excluded from scanning, can be null if not neededscanSubPackages
- whether to scan the subpackages in the given packagespackageNames
- the packages to be scanned
-
-
Method Details
-
excludeClass
Adds 'excludedClass' to the list of classes that are excluded from scanning when package are scanned. When classes are manually scanned withaddClass(Class)
, excluding it beforehand has NO effect. Excluded classes are only for package scanning and have NO effect on manual class scanning.- Parameters:
excludedClass
-
-
addClass
Scans all public methods in the givenClass
and createsListenerObject
s for them if they have aListener
annotation, provided thatgetTargetObjectForClass(Class)
can provide a suitable execution target. Otherwise, theClass
is ignored. The createdListenerObject
s are added to theCollection
ofListenerObject
s that this instance keeps.- Parameters:
clazz
-
-
addPackage
Scans all classes in the given package and callsaddClass(Class)
on each of them if they're not contained in the collection of excluded classes.- Parameters:
packageName
- the package to be scannedscanSubPackages
- whether to scan the subpackages in the given package
-
getClassLoader
This method gets theClassLoader
that is used to get the classes in a package. Please override it if you want to use a differentClassLoader
. The default is the ContextClassLoader
. -
getClasses
protected Collection<Class<?>> getClasses(String packageName, boolean scanSubPackages) throws ClassNotFoundException, IOException Scans all classes accessible in the given package, using theClassLoader
associated with thisAbstractClassBasedListenerObjectCreator
.- Parameters:
packageName
- the packagescanSubPackages
- whether to scan the subpackages in the given package- Returns:
- the classes, or an empty
Collection
if none were found - Throws:
ClassNotFoundException
IOException
-
findClassesInZipFile
protected Collection<Class<?>> findClassesInZipFile(ZipFile zipFile, String path, boolean scanSubPackages) throws ClassNotFoundException Method used to find all classes in a given zipFile.- Parameters:
zipFile
- the zipFilepath
- the package name in path format for classes found inside the zipFilescanSubPackages
- whether to scan the subpackages in the given path- Returns:
- the classes, or an empty
Collection
if none were found - Throws:
ClassNotFoundException
-
findClassesInDirectory
protected Collection<Class<?>> findClassesInDirectory(File directory, String packageName, boolean scanSubPackages) throws ClassNotFoundException Method used to find all classes in a given directory.- Parameters:
directory
- the directorypackageName
- the package name for classes found inside the directoryscanSubPackages
- whether to scan the subpackages in the given package- Returns:
- the classes, or an empty
Collection
if none were found - Throws:
ClassNotFoundException
-
getTargetObjectForClass
Gets a target executionObject
for the given class to be used by aListenerObject
to execute itsMethod
.
-