|
|
@@ -16,6 +16,7 @@ import net.ranides.assira.io.ZipReader;
|
|
|
import net.ranides.assira.io.uri.URIUtils;
|
|
|
import net.ranides.assira.text.StringUtils;
|
|
|
import net.ranides.assira.trace.ExceptionUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
@@ -26,6 +27,8 @@ import java.nio.charset.Charset;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.BiConsumer;
|
|
|
+import java.util.function.BiFunction;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.jar.JarEntry;
|
|
|
@@ -34,6 +37,8 @@ import java.util.jar.JarInputStream;
|
|
|
|
|
|
public final class PackageScanner {
|
|
|
|
|
|
+ private BiFunction<String, Throwable, Class<?>> handler;
|
|
|
+
|
|
|
private String root;
|
|
|
|
|
|
private ClassLoader loader;
|
|
|
@@ -61,12 +66,27 @@ public final class PackageScanner {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ public PackageScanner resolver(BiFunction<String, Throwable, Class<?>> handler) {
|
|
|
+ this.handler = handler;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PackageScanner logger(BiConsumer<String, Throwable> handler) {
|
|
|
+ this.handler = (name,error) -> { handler.accept(name, error); return null; };
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PackageScanner logger(Logger logger) {
|
|
|
+ this.handler = (name,error) -> { logger.error("Can't load class: " + name, error); return null; };
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
public CQuery<Class<?>> types() throws IOException {
|
|
|
return names().map(toClass(cloader())).filter(Objects::nonNull);
|
|
|
}
|
|
|
|
|
|
public CQuery<String> names() throws IOException {
|
|
|
- return files().filter(PackageScanner::isClass).map(PackageScanner::toClassName);
|
|
|
+ return files().filter(this::isClass).map(PackageScanner::toClassName);
|
|
|
}
|
|
|
|
|
|
public CQuery<String> files() throws IOException {
|
|
|
@@ -180,15 +200,18 @@ public final class PackageScanner {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static boolean isClass(String path) {
|
|
|
+ private boolean isClass(String path) {
|
|
|
return path.endsWith(".class");
|
|
|
}
|
|
|
|
|
|
- private static Function<String, Class<?>> toClass(ClassLoader loader) {
|
|
|
+ private Function<String, Class<?>> toClass(ClassLoader loader) {
|
|
|
return name -> {
|
|
|
try {
|
|
|
return Class.forName(name, false, loader);
|
|
|
} catch(ClassNotFoundException | NoClassDefFoundError cause) {
|
|
|
+ if(handler != null) {
|
|
|
+ return handler.apply(name, cause);
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
};
|