From: Fredrik Tolf Date: Tue, 12 Mar 2013 21:39:00 +0000 (+0100) Subject: Added ability to implicitly cast instances from the Loader. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=70e5fe9d71697e7b33b0ed1a95efa29ad4ed7b62;p=jglob.git Added ability to implicitly cast instances from the Loader. --- diff --git a/src/dolda/jglob/Loader.java b/src/dolda/jglob/Loader.java index 528c3fb..82a47b5 100644 --- a/src/dolda/jglob/Loader.java +++ b/src/dolda/jglob/Loader.java @@ -8,7 +8,6 @@ import java.lang.annotation.*; public class Loader { private final Class an; private final ClassLoader cl; - private final Map, Object> instances = new HashMap, Object>(); private Loader(Class annotation, ClassLoader loader) { this.an = annotation; @@ -113,21 +112,21 @@ public class Loader { }); } - public Iterable instances() { - return(new Iterable() { - public Iterator iterator() { - return(new Iterator() { + public Iterable instances(final Class cast) { + return(new Iterable() { + public Iterator iterator() { + return(new Iterator() { private final Iterator> classes = classes().iterator(); - private Object n = null; + private T n = null; public boolean hasNext() { while(n == null) { if(!classes.hasNext()) return(false); Class cl = classes.next(); - Object inst; + T inst; try { - inst = cl.newInstance(); + inst = cast.cast(cl.newInstance()); } catch(InstantiationException e) { throw(new GlobInstantiationException(e)); } catch(IllegalAccessException e) { @@ -138,10 +137,10 @@ public class Loader { return(true); } - public Object next() { + public T next() { if(!hasNext()) throw(new NoSuchElementException()); - Object r = n; + T r = n; n = null; return(r); } @@ -152,6 +151,10 @@ public class Loader { }); } + public Iterable instances() { + return(instances(Object.class)); + } + public static Loader get(Class annotation, ClassLoader loader) { return(new Loader(annotation, loader)); }