verinec.util
Class InterfaceUtility

java.lang.Object
  extended by verinec.util.InterfaceUtility

public class InterfaceUtility
extends Object

This utilily class provides methods to find Class resources that implement a given Interface.

You can specify an interface that the class must match. All folders and jars in the classpath are searched for classes, of which each is tested whether it implements the interface.
The interface can be just the name of the interface, or can also contain the package name. (If it is just the name, classes implementing any interface with that name are found, if the name occurs in more than one packages.)

Examples:
IMyInterface:

mypackage.IMyInterface: myproject.mypackage.IMyInterface:

To avoid unexpected results, specify the full classpath.

Author:
nanoTitan, Dominik Jungo

Constructor Summary
InterfaceUtility()
           
 
Method Summary
private static void addClassIfImplements(ArrayList resourceList, String classname, String iface)
          Test wether the class implements the interface and if it does, add it to the list if it is not yet in it.
static ArrayList getClassResources(String iface)
          Finds and returns URLs of all the system resources(classes) that implement a given interface.
private static ArrayList getClassResourcesFromArchive(ArrayList resourceList, JarFile jar, String iface)
          Finds all of the entries within the specified JAR file that implement the specified interface and returns the associated resource URLs.
private static ArrayList getClassResourcesFromDirectory(ArrayList resourceList, File dir, String iface)
          Finds all of the classes under the specified root directory within subdirectories that implement the specified interface and returns the associated resource URLs.
private static ArrayList getClassResourcesFromDirectory(ArrayList resourceList, File dir, String iface, String relpath)
          Implements the recursive search through directories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InterfaceUtility

public InterfaceUtility()
Method Detail

getClassResources

public static final ArrayList getClassResources(String iface)
                                         throws IOException
Finds and returns URLs of all the system resources(classes) that implement a given interface. System resources will be found in the optional extensions folder and off of the CLASSPATH.

Parameters:
iface - The implemented interface to look for.
Returns:
a Collection of resources that match the search criteria.
Throws:
IOException - If creation of jar file fails.

getClassResourcesFromArchive

private static final ArrayList getClassResourcesFromArchive(ArrayList resourceList,
                                                            JarFile jar,
                                                            String iface)
Finds all of the entries within the specified JAR file that implement the specified interface and returns the associated resource URLs.

Parameters:
resourceList - list of the already found classes
jar - The JAR file to search; if null an empty list will be returned.
iface - The interface classes must implement.
Returns:
a Collection of resources that match the search criteria.

getClassResourcesFromDirectory

private static final ArrayList getClassResourcesFromDirectory(ArrayList resourceList,
                                                              File dir,
                                                              String iface)
Finds all of the classes under the specified root directory within subdirectories that implement the specified interface and returns the associated resource URLs.

Parameters:
resourceList - list of the already found classes
dir - the directory to add (potentially); if dir is null or dir is not a directory then an empty Collection is returned
iface - the implemented interface to look for.
Returns:
a Collection of URLs of resources that match the specified criteria

getClassResourcesFromDirectory

private static final ArrayList getClassResourcesFromDirectory(ArrayList resourceList,
                                                              File dir,
                                                              String iface,
                                                              String relpath)
Implements the recursive search through directories.

Parameters:
resourceList - list of the already found classes
dir - The root directory of the search
iface - the implemented interface to look for.
relpath - Path so far opened from root path.
Returns:
A Collection of URLs of resources that match the specified criteria

addClassIfImplements

private static final void addClassIfImplements(ArrayList resourceList,
                                               String classname,
                                               String iface)
Test wether the class implements the interface and if it does, add it to the list if it is not yet in it.

Parameters:
resourceList - The list to add the class if it implements the interface.
classname - Name (with full class path) of a class to test.
iface - Interface name of interface the class must implement.

Copyright © 2005 Verinec, DIUF