de.jstacs.utils
Class SubclassFinder

java.lang.Object
  extended by de.jstacs.utils.SubclassFinder

public class SubclassFinder
extends Object

Utility-class with static methods to

The methods may fail to find a certain sub-class, if

Author:
Jens Keilwagen, Jan Grau

Field Summary
static String includePath
          This field can be set to include a path into the search performed in findSubclasses(Class, String) thereby enabling to find self-implemented classes not included in the Jstacs class hierarchy.
 
Constructor Summary
SubclassFinder()
           
 
Method Summary
static
<S,T> LinkedList<Class<? extends S>>
filterBySuperclass(Class<S> superclass, LinkedList<Class<? extends T>> listToFilter)
          Returns a LinkedList of the Class objects for all classes in listToFilter that are sub-classes of superClass.
static
<T> LinkedList<Class<? extends T>>
findInstantiableSubclasses(Class<T> clazz, String startPackage)
          Returns all sub-classes of T that can be instantiated, i.e.
static
<T> LinkedList<Class<? extends T>>
findSubclasses(Class<T> clazz, String startPackage)
          Returns all sub-classes of T including interfaces and abstract classes that are located in a package below startPackage.
static
<T> LinkedList<InstanceParameterSet<? extends T>>
getInstanceParameterSets(Class<T> clazz, String startPackage)
          This method returns a list of InstanceParameterSets that can be used to create a subclass of clazz.
static LinkedList<Class<? extends InstanceParameterSet>> getParameterSetFor(Class<? extends InstantiableFromParameterSet> clazz)
          Returns a LinkedList of the classes of the InstanceParameterSets that can be used to instantiate the sub-class of InstantiableFromParameterSet that is given by clazz
static
<T> SelectionParameter
getSelectionParameter(Class<? extends ParameterSet> clazz, String startPackage, String name, String comment, boolean required)
          This method creates an SelectionParameter that contains InstanceParameterSet for each possible class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

includePath

public static String includePath
This field can be set to include a path into the search performed in findSubclasses(Class, String) thereby enabling to find self-implemented classes not included in the Jstacs class hierarchy. The default value of this field is null.

Please use this field very careful, i.e., after setting a specific value and calling findSubclasses(Class, String) it is highly recommended to reset the value. If this is not done, preceeding searches might take longer.

Constructor Detail

SubclassFinder

public SubclassFinder()
Method Detail

findInstantiableSubclasses

public static <T> LinkedList<Class<? extends T>> findInstantiableSubclasses(Class<T> clazz,
                                                                            String startPackage)
                                                                 throws ClassNotFoundException,
                                                                        IOException
Returns all sub-classes of T that can be instantiated, i.e. are neither an interface nor abstract, and that are located in a package below startPackage.

Type Parameters:
T - The class to obtain the sub-classes for
Parameters:
clazz - the Class object for T
startPackage - the package under which to search
Returns:
the Class objects for the sub-classes
Throws:
ClassNotFoundException - if one of the classes is present in the file system or jar but cannot be loaded by the class loader
IOException - is thrown if the classes are searched for in a jar file, but that file could not be accessed or read

filterBySuperclass

public static <S,T> LinkedList<Class<? extends S>> filterBySuperclass(Class<S> superclass,
                                                                      LinkedList<Class<? extends T>> listToFilter)
Returns a LinkedList of the Class objects for all classes in listToFilter that are sub-classes of superClass.

Type Parameters:
S - the class that is used as filter
T - a common super-class of all classes in listToFilter
Parameters:
superclass - the additional class to use as a filter criterion
listToFilter - the list of classes
Returns:
the filtered list

getParameterSetFor

public static LinkedList<Class<? extends InstanceParameterSet>> getParameterSetFor(Class<? extends InstantiableFromParameterSet> clazz)
Returns a LinkedList of the classes of the InstanceParameterSets that can be used to instantiate the sub-class of InstantiableFromParameterSet that is given by clazz

Parameters:
clazz - the Class object of the sub-class of InstantiableFromParameterSet
Returns:
a LinkedList of Classes of the corresponding InstanceParameterSets

findSubclasses

public static <T> LinkedList<Class<? extends T>> findSubclasses(Class<T> clazz,
                                                                String startPackage)
                                                     throws ClassNotFoundException,
                                                            IOException
Returns all sub-classes of T including interfaces and abstract classes that are located in a package below startPackage.

Type Parameters:
T - The class to obtain the sub-classes for
Parameters:
clazz - the Class object for T
startPackage - the package under which to search
Returns:
the Class objects for the sub-classes
Throws:
ClassNotFoundException - if one of the classes is present in the file system or jar but cannot be loaded by the class loader
IOException - is thrown if the classes are searched for in a jar file, but that file could not be accessed or read

getSelectionParameter

public static <T> SelectionParameter getSelectionParameter(Class<? extends ParameterSet> clazz,
                                                           String startPackage,
                                                           String name,
                                                           String comment,
                                                           boolean required)
                                                throws Exception
This method creates an SelectionParameter that contains InstanceParameterSet for each possible class. The classes are specified by findInstantiableSubclasses(Class, String) and filterBySuperclass(Class, LinkedList).

Type Parameters:
T - The class to use the sub-classes in the SelectionParameter
Parameters:
clazz - the Class object for T
startPackage - the package under which to start the search
name - the name of the SelectionParameter
comment - the comment for the SelectionParameter
required - whether the SelectionParameter is required
Returns:
a SelectionParameter that contains InstanceParameterSet for each possible class.
Throws:
Exception - if the SelectionParameter could not be created properly
See Also:
findInstantiableSubclasses(Class, String), filterBySuperclass(Class, LinkedList), getInstanceParameterSets(Class, String)

getInstanceParameterSets

public static <T> LinkedList<InstanceParameterSet<? extends T>> getInstanceParameterSets(Class<T> clazz,
                                                                                         String startPackage)
                                                                              throws ClassNotFoundException,
                                                                                     IOException,
                                                                                     InstantiationException,
                                                                                     IllegalAccessException
This method returns a list of InstanceParameterSets that can be used to create a subclass of clazz. The classes are specified by findInstantiableSubclasses(Class, String) and filterBySuperclass(Class, LinkedList).

Type Parameters:
T - The class to use the sub-classes in the SelectionParameter
Parameters:
clazz - the Class object for T
startPackage - the package under which to start the search
Returns:
a LinkedList that contains InstanceParameterSet for each possible class.
Throws:
InstantiationException - if any InstanceParameterSet has no nullary constructor; or if the instantiation fails for some other reason
IllegalAccessException - if any InstanceParameterSet or its nullary constructor is not accessible
ClassNotFoundException - if one of the classes is present in the file system or jar but cannot be loaded by the class loader
IOException - if the classes are searched for in a jar file, but that file could not be accessed or read
See Also:
findInstantiableSubclasses(Class, String), filterBySuperclass(Class, LinkedList)