From 19f1b265931737a28760ccfe0200b4721f545989 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 28 Jun 2009 05:50:51 +0200 Subject: Rolled our own ServiceLoader, because its not part of java 1.5. --- src/lombok/core/SpiLoadUtil.java | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src/lombok/core') diff --git a/src/lombok/core/SpiLoadUtil.java b/src/lombok/core/SpiLoadUtil.java index bf4bddf4..b26dd747 100644 --- a/src/lombok/core/SpiLoadUtil.java +++ b/src/lombok/core/SpiLoadUtil.java @@ -1,12 +1,75 @@ package lombok.core; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.annotation.Annotation; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.net.URL; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Set; + +import lombok.Lombok; public class SpiLoadUtil { private SpiLoadUtil() {} + public static Iterator findServices(Class target) throws IOException { + return findServices(target, Thread.currentThread().getContextClassLoader()); + } + + public static Iterator findServices(final Class target, final ClassLoader loader) throws IOException { + Enumeration resources = loader.getResources("META-INF/services/" + target.getName()); + final Set entries = new LinkedHashSet(); + while ( resources.hasMoreElements() ) { + URL url = resources.nextElement(); + readServicesFromUrl(entries, url); + } + + final Iterator names = entries.iterator(); + return new Iterator() { + public boolean hasNext() { + return names.hasNext(); + } + + public C next() { + try { + return target.cast(Class.forName(names.next(), true, loader).newInstance()); + } catch ( Throwable t ) { + throw Lombok.sneakyThrow(t); + } + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + + private static void readServicesFromUrl(Collection list, URL url) throws IOException { + InputStream in = url.openStream(); + try { + if ( in == null ) return; + BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8")); + while ( true ) { + String line = r.readLine(); + if ( line == null ) break; + int idx = line.indexOf('#'); + if ( idx != -1 ) line = line.substring(0, idx); + line = line.trim(); + if ( line.length() == 0 ) continue; + list.add(line); + } + } finally { + try { in.close(); } catch ( Throwable ignore ) {} + } + } + @SuppressWarnings("unchecked") public static Class findAnnotationClass(Class c, Class base) { if ( c == Object.class || c == null ) return null; -- cgit