org.jdesktop.wonderland.common.utils
Class ScannedClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by org.jdesktop.wonderland.common.utils.ScannedClassLoader

public class ScannedClassLoader
extends java.net.URLClassLoader

A classloader that scans its contents for annotations. It can then be queried by annotation type for all classes implementing the given annotation.

Author:
jkaplan

Constructor Summary
protected ScannedClassLoader()
          Create a scannned classloader that scans the contents of the system classloader.
  ScannedClassLoader(java.net.URL[] urls)
          Create a scanned classloader that scans the given urls, using the default parent classloader.
  ScannedClassLoader(java.net.URL[] urls, java.lang.ClassLoader parent)
          Create a scanned classloader that scans the given urls.
 
Method Summary
protected  void addURL(java.net.URL url)
           
protected  void createDB(java.net.URL[] urls)
          Create the annotation database from the given URLs.
<T> java.util.Iterator<T>
getAll(java.lang.Class<? extends java.lang.annotation.Annotation> annot, java.lang.Class<T> clazz)
          Get instances of all classes that implement the given interface, defined either by service provider or by annoation.
 java.util.Set<java.lang.String> getClasses(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
          Get the name of all classes that are annotated with the given annotation.
<T> java.util.Iterator<T>
getInstances(java.lang.Class<? extends java.lang.annotation.Annotation> annot, java.lang.Class<T> clazz)
          Get instances of every class marked with the given annotation.
static ScannedClassLoader getSystemScannedClassLoader()
          Get a singleton instance for the System class loader
 
Methods inherited from class java.net.URLClassLoader
definePackage, findClass, findResource, findResources, getPermissions, getURLs, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScannedClassLoader

protected ScannedClassLoader()
Create a scannned classloader that scans the contents of the system classloader. Use getSystemScannedClassLoader() instead.


ScannedClassLoader

public ScannedClassLoader(java.net.URL[] urls)
Create a scanned classloader that scans the given urls, using the default parent classloader.

Parameters:
urls - the urls to scan

ScannedClassLoader

public ScannedClassLoader(java.net.URL[] urls,
                          java.lang.ClassLoader parent)
Create a scanned classloader that scans the given urls.

Parameters:
urls - the urls to scan
parent - the parent classlaoder to delegate to
Method Detail

getSystemScannedClassLoader

public static ScannedClassLoader getSystemScannedClassLoader()
Get a singleton instance for the System class loader

Returns:
a singleton scanned version of the System class loader

getClasses

public java.util.Set<java.lang.String> getClasses(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Get the name of all classes that are annotated with the given annotation.

Parameters:
annotation - the annotation to search for

getInstances

public <T> java.util.Iterator<T> getInstances(java.lang.Class<? extends java.lang.annotation.Annotation> annot,
                                              java.lang.Class<T> clazz)
Get instances of every class marked with the given annotation. The returned iterator will instantiate the object during the call to hasNext() or next(). If there is an error, a ClassScanningError will be thrown by the call to hasNext() or next(). If an error is thrown, the iterator will attempt to recover so subsequent calls don't fail, but the recovery can't be guaranteed.

All instantiations will be done using the default, no argument constructor.

This iterator does not support removal.

Parameters:
annot - the annotation to search for
clazz - the class of the return type
Returns:
an iterator of instantiated instances of the given type

getAll

public <T> java.util.Iterator<T> getAll(java.lang.Class<? extends java.lang.annotation.Annotation> annot,
                                        java.lang.Class<T> clazz)
Get instances of all classes that implement the given interface, defined either by service provider or by annoation.

Parameters:
annot - the annotation to search for
clazz - the class of the return type
Returns:
an iterator of instantiated instances of the given type

addURL

protected void addURL(java.net.URL url)
Overrides:
addURL in class java.net.URLClassLoader

createDB

protected void createDB(java.net.URL[] urls)
Create the annotation database from the given URLs.

Parameters:
urls - the urls to use


Open Wonderland - http://openwonderland.org