|
@@ -10,6 +10,7 @@ import net.ranides.assira.text.StringTraits;
|
|
|
import net.ranides.assira.text.StringUtils;
|
|
import net.ranides.assira.text.StringUtils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
@@ -32,7 +33,7 @@ public class ServiceScanner<S> {
|
|
|
private ServiceScanner(Class<S> service) {
|
|
private ServiceScanner(Class<S> service) {
|
|
|
this.service = service;
|
|
this.service = service;
|
|
|
this.loader = ClassLoader.getSystemClassLoader();
|
|
this.loader = ClassLoader.getSystemClassLoader();
|
|
|
- this.resolver = name -> Optional.of(service.cast(Class.forName(name, false, loader).newInstance()));
|
|
|
|
|
|
|
+ this.resolver = this::resolvePublic;
|
|
|
this.listener = error -> { };
|
|
this.listener = error -> { };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,6 +46,10 @@ public class ServiceScanner<S> {
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public ServiceScanner<S> resolvePrivate() {
|
|
|
|
|
+ return resolver(this::resolvePrivate);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public ServiceScanner<S> resolver(ServiceResolver<S> handler) {
|
|
public ServiceScanner<S> resolver(ServiceResolver<S> handler) {
|
|
|
ServiceResolver<S> prev = this.resolver;
|
|
ServiceResolver<S> prev = this.resolver;
|
|
|
this.resolver = name -> {
|
|
this.resolver = name -> {
|
|
@@ -119,13 +124,16 @@ public class ServiceScanner<S> {
|
|
|
return Optional.of(name);
|
|
return Optional.of(name);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private Optional<S> makeInstance(Class<? extends S> type) {
|
|
|
|
|
- try {
|
|
|
|
|
- return Optional.of(type.newInstance());
|
|
|
|
|
- } catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
|
- listener.accept(e);
|
|
|
|
|
- return Optional.empty();
|
|
|
|
|
|
|
+ private Optional<S> resolvePublic(String name) throws ReflectiveOperationException {
|
|
|
|
|
+ return Optional.of(service.cast(Class.forName(name, false, loader).newInstance()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Optional<S> resolvePrivate(String name) throws ReflectiveOperationException {
|
|
|
|
|
+ Constructor<?> ctor = Class.forName(name, false, loader).getConstructor();
|
|
|
|
|
+ if(!MemberTraits.isPublic(ctor)) {
|
|
|
|
|
+ ctor.setAccessible(true);
|
|
|
}
|
|
}
|
|
|
|
|
+ return Optional.of(service.cast(ctor.newInstance()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private Optional<S> makeInstance(String name) {
|
|
private Optional<S> makeInstance(String name) {
|