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 ListenerObjects from classes. Users can pass in Classes or packagenames and this class will scan the Classes and create ListenerObjects 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 ListenerObjects. That is the responsibility of the implementations of this abstract class.

Author:
G.J. Schouten
  • Field Details

  • Constructor Details

    • AbstractClassBasedListenerObjectCreator

      public AbstractClassBasedListenerObjectCreator(Class<?>... classes)
      Calls addClass(Class) on each given class.
      Parameters:
      classes -
    • AbstractClassBasedListenerObjectCreator

      public AbstractClassBasedListenerObjectCreator(Collection<Class<?>> excludedClasses, boolean scanSubPackages, String... packageNames)
      Calls addPackage(String, boolean) on each given package name.
      Parameters:
      excludedClasses - the classes to be excluded from scanning, can be null if not needed
      scanSubPackages - whether to scan the subpackages in the given packages
      packageNames - the packages to be scanned
  • Method Details

    • excludeClass

      public void excludeClass(Class<?> excludedClass)
      Adds 'excludedClass' to the list of classes that are excluded from scanning when package are scanned. When classes are manually scanned with addClass(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

      public void addClass(Class<?> clazz)
      Scans all public methods in the given Class and creates ListenerObjects for them if they have a Listener annotation, provided that getTargetObjectForClass(Class) can provide a suitable execution target. Otherwise, the Class is ignored. The created ListenerObjects are added to the Collection of ListenerObjects that this instance keeps.
      Parameters:
      clazz -
    • addPackage

      public void addPackage(String packageName, boolean scanSubPackages)
      Scans all classes in the given package and calls addClass(Class) on each of them if they're not contained in the collection of excluded classes.
      Parameters:
      packageName - the package to be scanned
      scanSubPackages - whether to scan the subpackages in the given package
    • getClassLoader

      protected ClassLoader getClassLoader()
      This method gets the ClassLoader that is used to get the classes in a package. Please override it if you want to use a different ClassLoader. The default is the Context ClassLoader.
    • getClasses

      protected Collection<Class<?>> getClasses(String packageName, boolean scanSubPackages) throws ClassNotFoundException, IOException
      Scans all classes accessible in the given package, using the ClassLoader associated with this AbstractClassBasedListenerObjectCreator.
      Parameters:
      packageName - the package
      scanSubPackages - 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 zipFile
      path - the package name in path format for classes found inside the zipFile
      scanSubPackages - 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 directory
      packageName - the package name for classes found inside the directory
      scanSubPackages - whether to scan the subpackages in the given package
      Returns:
      the classes, or an empty Collection if none were found
      Throws:
      ClassNotFoundException
    • getTargetObjectForClass

      protected abstract Object getTargetObjectForClass(Class<?> clazz)
      Gets a target execution Object for the given class to be used by a ListenerObject to execute its Method.
      Parameters:
      clazz -
      Returns:
      an Object that has the type 'clazz' and can be used as an execution target, or 'null' if no such Object can be found