6 import java.lang.annotation.*;
9 private final Class<? extends Annotation> an;
10 private final ClassLoader cl;
12 private Loader(Class<? extends Annotation> annotation, ClassLoader loader) {
17 public Iterable<String> names() {
18 return(new Iterable<String>() {
19 public Iterator<String> iterator() {
20 return(new Iterator<String>() {
21 private Enumeration<URL> rls;
22 private Iterator<String> cur = null;
24 private Iterator<String> parse(URL url) {
26 List<String> buf = new LinkedList<String>();
27 InputStream in = url.openStream();
29 BufferedReader r = new BufferedReader(new InputStreamReader(in, "utf-8"));
31 while((ln = r.readLine()) != null) {
37 return(buf.iterator());
41 } catch(IOException e) {
42 throw(new GlobAccessException(e));
46 public boolean hasNext() {
47 if((cur == null) || !cur.hasNext()) {
50 rls = cl.getResources("META-INF/glob/" + an.getName());
51 } catch(IOException e) {
52 throw(new GlobAccessException(e));
55 if(!rls.hasMoreElements())
57 URL u = rls.nextElement();
63 public String next() {
65 throw(new NoSuchElementException());
66 String ret = cur.next();
70 public void remove() {throw(new UnsupportedOperationException());}
76 public Iterable<Class<?>> classes() {
77 return(new Iterable<Class<?>>() {
78 public Iterator<Class<?>> iterator() {
79 return(new Iterator<Class<?>>() {
80 private final Iterator<String> names = names().iterator();
81 private Class<?> n = null;
83 public boolean hasNext() {
87 String nm = names.next();
91 } catch(ClassNotFoundException e) {
94 if(c.getAnnotation(an) == null)
101 public Class<?> next() {
103 throw(new NoSuchElementException());
109 public void remove() {throw(new UnsupportedOperationException());}
115 public <T> Iterable<T> instances(final Class<T> cast) {
116 return(new Iterable<T>() {
117 public Iterator<T> iterator() {
118 return(new Iterator<T>() {
119 private final Iterator<Class<?>> classes = classes().iterator();
122 public boolean hasNext() {
124 if(!classes.hasNext())
126 Class<?> cl = classes.next();
129 inst = cast.cast(cl.newInstance());
130 } catch(InstantiationException e) {
131 throw(new GlobInstantiationException(e));
132 } catch(IllegalAccessException e) {
133 throw(new GlobInstantiationException(e));
142 throw(new NoSuchElementException());
148 public void remove() {throw(new UnsupportedOperationException());}
154 public Iterable<?> instances() {
155 return(instances(Object.class));
158 public static Loader get(Class<? extends Annotation> annotation, ClassLoader loader) {
159 return(new Loader(annotation, loader));
162 public static Loader get(Class<? extends Annotation> annotation) {
163 return(get(annotation, annotation.getClassLoader()));