Slight tweak to SpiLoadUtil: It now returns iterables instead of iterators.

This commit is contained in:
Reinier Zwitserloot committed 2009-09-23 07:43:43 +02:00
1 parent ec38e1a51b
commit 21ea5eeca8
2 files changed
+38 -44

No files matched your search

+21 -17
View File
@@ -61,7 +61,7 @@ public class SpiLoadUtil {
*
* @param target class to find implementations for.
*/
public static <C> Iterator<C> findServices(Class<C> target) throws IOException {
public static <C> Iterable<C> findServices(Class<C> target) throws IOException {
return findServices(target, Thread.currentThread().getContextClassLoader());
}
@@ -75,7 +75,7 @@ public class SpiLoadUtil {
* @param loader The classloader object to use to both the spi discovery files, as well as the loader to use
* to make the returned instances.
*/
public static <C> Iterator<C> findServices(final Class<C> target, final ClassLoader loader) throws IOException {
public static <C> Iterable<C> findServices(final Class<C> target, final ClassLoader loader) throws IOException {
Enumeration<URL> resources = loader.getResources("META-INF/services/" + target.getName());
final Set<String> entries = new LinkedHashSet<String>();
while ( resources.hasMoreElements() ) {
@@ -84,21 +84,25 @@ public class SpiLoadUtil {
}
final Iterator<String> names = entries.iterator();
return new Iterator<C>() {
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();
return new Iterable<C> () {
@Override public Iterator<C> iterator() {
return new Iterator<C>() {
@Override public boolean hasNext() {
return names.hasNext();
}
@Override public C next() {
try {
return target.cast(Class.forName(names.next(), true, loader).newInstance());
} catch ( Throwable t ) {
throw Lombok.sneakyThrow(t);
}
}
@Override public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
+17 -27
View File
@@ -23,11 +23,11 @@ package lombok.eclipse;
import static lombok.eclipse.Eclipse.toQualifiedName;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import lombok.Lombok;
@@ -96,44 +96,34 @@ public class HandlerLibrary {
/** Uses SPI Discovery to find implementations of {@link EclipseAnnotationHandler}. */
@SuppressWarnings("unchecked") private static void loadAnnotationHandlers(HandlerLibrary lib) {
Iterator<EclipseAnnotationHandler> it;
try {
it = SpiLoadUtil.findServices(EclipseAnnotationHandler.class);
} catch ( Throwable t ) {
throw Lombok.sneakyThrow(t);
}
while ( it.hasNext() ) {
try {
EclipseAnnotationHandler<?> handler = it.next();
Class<? extends Annotation> annotationClass =
SpiLoadUtil.findAnnotationClass(handler.getClass(), EclipseAnnotationHandler.class);
AnnotationHandlerContainer<?> container = new AnnotationHandlerContainer(handler, annotationClass);
if ( lib.annotationHandlers.put(container.annotationClass.getName(), container) != null ) {
Eclipse.error(null, "Duplicate handlers for annotation type: " + container.annotationClass.getName());
for (EclipseAnnotationHandler<?> handler : SpiLoadUtil.findServices(EclipseAnnotationHandler.class)) {
try {
Class<? extends Annotation> annotationClass =
SpiLoadUtil.findAnnotationClass(handler.getClass(), EclipseAnnotationHandler.class);
AnnotationHandlerContainer<?> container = new AnnotationHandlerContainer(handler, annotationClass);
if (lib.annotationHandlers.put(container.annotationClass.getName(), container) != null) {
Eclipse.error(null, "Duplicate handlers for annotation type: " + container.annotationClass.getName());
}
lib.typeLibrary.addType(container.annotationClass.getName());
} catch (Throwable t) {
Eclipse.error(null, "Can't load Lombok annotation handler for Eclipse: ", t);
}
lib.typeLibrary.addType(container.annotationClass.getName());
} catch ( Throwable t ) {
Eclipse.error(null, "Can't load Lombok annotation handler for Eclipse: ", t);
}
} catch ( IOException e ) {
Lombok.sneakyThrow(e);
}
}
/** Uses SPI Discovery to find implementations of {@link EclipseASTVisitor}. */
private static void loadVisitorHandlers(HandlerLibrary lib) {
Iterator<EclipseASTVisitor> it;
try {
it = SpiLoadUtil.findServices(EclipseASTVisitor.class);
for (EclipseASTVisitor visitor : SpiLoadUtil.findServices(EclipseASTVisitor.class)) {
lib.visitorHandlers.add(visitor);
}
} catch ( Throwable t ) {
throw Lombok.sneakyThrow(t);
}
while ( it.hasNext() ) {
try {
lib.visitorHandlers.add(it.next());
} catch ( Throwable t ) {
Eclipse.error(null, "Can't load Lombok visitor handler for Eclipse: ", t);
}
}
}
/**