Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions modules/jooby-scanner/src/main/java/org/jooby/scanner/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public class Scanner implements Jooby.Module {
private List<String> packages;

@SuppressWarnings("rawtypes")
private Set<Class> serviceTypes = new LinkedHashSet<>();
private final Set<Class> serviceTypes = new LinkedHashSet<>();

/**
* Creates a new {@link Scanner} and uses the provided scan spec or packages.
Expand Down Expand Up @@ -404,7 +404,7 @@ public void configure(final Env env, final Config conf, final Binder binder) {
Router routes = env.router();

ClassLoader loader = getClass().getClassLoader();
Throwing.Function<String, Class> loadClass = name -> loader.loadClass(name);
Throwing.Function<String, Class> loadClass = loader::loadClass;

// bind once as singleton + post/pre callbacks
Set<Object> bindings = new HashSet<>();
Expand All @@ -418,9 +418,7 @@ public void configure(final Env env, final Config conf, final Binder binder) {
ScanResult result = scanner.scan(conf.getInt("runtime.processors") + 1);

Predicate<String> inPackage = name -> packages.stream()
.filter(name::startsWith)
.findFirst()
.isPresent();
.anyMatch(name::startsWith);

/** Controllers: */
result.getNamesOfClassesWithAnnotation(Path.class)
Expand All @@ -443,41 +441,35 @@ public void configure(final Env env, final Config conf, final Binder binder) {
/** Annotated with: */
serviceTypes.stream()
.filter(A)
.forEach(a -> {
result.getNamesOfClassesWithAnnotation(a)
.stream()
.filter(once)
.map(loadClass)
.filter(C)
.forEach(bind);
});
.forEach(a -> result.getNamesOfClassesWithAnnotation(a)
.stream()
.filter(once)
.map(loadClass)
.filter(C)
.forEach(bind));

/** Implements: */
serviceTypes.stream()
.filter(I)
.filter(type -> type != Jooby.Module.class && type != Module.class && type != Service.class)
.forEach(i -> {
result.getNamesOfClassesImplementing(i)
.stream()
.filter(inPackage)
.filter(once)
.map(loadClass)
.filter(C)
.forEach(bind);
});
.forEach(i -> result.getNamesOfClassesImplementing(i)
.stream()
.filter(inPackage)
.filter(once)
.map(loadClass)
.filter(C)
.forEach(bind));

/** SubclassOf: */
serviceTypes.stream()
.filter(S)
.forEach(k -> {
result.getNamesOfSubclassesOf(k)
.stream()
.filter(inPackage)
.filter(once)
.map(loadClass)
.filter(C)
.forEach(bind);
});
.forEach(k -> result.getNamesOfSubclassesOf(k)
.stream()
.filter(inPackage)
.filter(once)
.map(loadClass)
.filter(C)
.forEach(bind));

/** Guice modules: */
if (serviceTypes.contains(Module.class)) {
Expand Down Expand Up @@ -528,7 +520,7 @@ public Scanner scan(final Class<?> type) {
}

private static <T> T newObject(final Class<T> klass) {
return throwingSupplier(() -> klass.newInstance()).get();
return throwingSupplier(klass::newInstance).get();
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -542,7 +534,7 @@ private static void guavaServices(final Env env, final Binder binder,
serviceTypes.forEach(guavaService);
// lazy service manager
AtomicReference<ServiceManager> sm = new AtomicReference<>();
Provider<ServiceManager> smProvider = () -> sm.get();
Provider<ServiceManager> smProvider = sm::get;
binder.bind(ServiceManager.class).toProvider(smProvider);
// ask Guice for services, create ServiceManager and start services
env.onStart(r -> {
Expand All @@ -553,9 +545,7 @@ private static void guavaServices(final Env env, final Binder binder,
sm.get().startAsync().awaitHealthy();
});
// stop services
env.onStop(() -> {
sm.get().stopAsync().awaitStopped();
});
env.onStop(() -> sm.get().stopAsync().awaitStopped());
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand Down