Skip to content

Commit f3c07e7

Browse files
authored
Merge pull request #233 from adrienlauer/kernel-predicates
Update to kernel without specifications
2 parents d65c9b2 + 07c4190 commit f3c07e7

File tree

33 files changed

+298
-245
lines changed

33 files changed

+298
-245
lines changed

cli/src/main/java/org/seedstack/seed/cli/internal/CommandLineHandlerSpecification.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.cli.internal;
910

1011
import java.lang.reflect.Modifier;
11-
import org.kametic.specifications.AbstractSpecification;
12+
import java.util.function.Predicate;
1213
import org.seedstack.seed.cli.CommandLineHandler;
1314
import org.seedstack.shed.reflect.ClassPredicates;
1415

15-
class CommandLineHandlerSpecification extends AbstractSpecification<Class<?>> {
16+
class CommandLineHandlerSpecification implements Predicate<Class<?>> {
1617
static CommandLineHandlerSpecification INSTANCE = new CommandLineHandlerSpecification();
1718

1819
private CommandLineHandlerSpecification() {
1920
// no instantiation allowed
2021
}
2122

2223
@Override
23-
public boolean isSatisfiedBy(Class<?> candidate) {
24+
public boolean test(Class<?> candidate) {
2425
return ClassPredicates.classIsAssignableFrom(CommandLineHandler.class)
2526
.and(ClassPredicates.classIsInterface().negate())
2627
.and(ClassPredicates.classModifierIs(Modifier.ABSTRACT).negate())

cli/src/main/java/org/seedstack/seed/cli/internal/CommandLinePlugin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.cli.internal;
910

1011
import io.nuun.kernel.api.plugin.InitState;
@@ -43,7 +44,7 @@ public void setup(SeedRuntime seedRuntime) {
4344

4445
@Override
4546
public Collection<ClasspathScanRequest> classpathScanRequests() {
46-
return classpathScanRequestBuilder().specification(CommandLineHandlerSpecification.INSTANCE).build();
47+
return classpathScanRequestBuilder().predicate(CommandLineHandlerSpecification.INSTANCE).build();
4748
}
4849

4950
@Override
@@ -54,7 +55,7 @@ public InitState initialize(InitContext initContext) {
5455
return InitState.INITIALIZED;
5556
}
5657

57-
Collection<Class<?>> cliHandlerCandidates = initContext.scannedTypesBySpecification()
58+
Collection<Class<?>> cliHandlerCandidates = initContext.scannedTypesByPredicate()
5859
.get(CommandLineHandlerSpecification.INSTANCE);
5960
for (Class<?> candidate : cliHandlerCandidates) {
6061
CliCommand cliCommand = candidate.getAnnotation(CliCommand.class);

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<version>${project.version}</version>
3232
</dependency>
3333
<dependency>
34-
<groupId>io.nuun</groupId>
34+
<groupId>io.nuun.kernel</groupId>
3535
<artifactId>kernel-core</artifactId>
3636
</dependency>
3737
<dependency>

core/src/main/java/org/seedstack/seed/core/internal/CorePlugin.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.core.internal;
910

1011
import static org.seedstack.shed.misc.PriorityUtils.sortByPriority;
@@ -21,9 +22,7 @@
2122
import java.util.Set;
2223
import java.util.stream.Collectors;
2324
import javax.inject.Provider;
24-
import org.kametic.specifications.Specification;
2525
import org.seedstack.seed.SeedInterceptor;
26-
import org.seedstack.seed.core.internal.utils.SpecificationBuilder;
2726
import org.seedstack.shed.misc.PriorityUtils;
2827
import org.seedstack.shed.reflect.Classes;
2928
import org.slf4j.Logger;
@@ -39,12 +38,6 @@ public class CorePlugin extends AbstractSeedPlugin {
3938
static final String AUTODETECT_BINDINGS_KERNEL_PARAM = "seedstack.autodetectBindings";
4039
static final String AUTODETECT_INTERCEPTORS_KERNEL_PARAM = "seedstack.autodetectInterceptors";
4140
private static final String SEEDSTACK_PACKAGE = "org.seedstack";
42-
private static final Specification<Class<?>> installSpecification = new SpecificationBuilder<>(
43-
InstallResolver.INSTANCE).build();
44-
private static final Specification<Class<?>> bindSpecification = new SpecificationBuilder<>(
45-
BindResolver.INSTANCE).build();
46-
private static final Specification<Class<?>> providerSpecification = new SpecificationBuilder<>(
47-
ProvideResolver.INSTANCE).build();
4841
private final Set<Class<? extends Module>> modules = new HashSet<>();
4942
private final Set<Class<? extends Module>> overridingModules = new HashSet<>();
5043
private final List<SeedInterceptor> methodInterceptors = new ArrayList<>();
@@ -64,9 +57,9 @@ public String pluginPackageRoot() {
6457
@Override
6558
public Collection<ClasspathScanRequest> classpathScanRequests() {
6659
return classpathScanRequestBuilder()
67-
.specification(installSpecification)
68-
.specification(bindSpecification)
69-
.specification(providerSpecification)
60+
.predicate(InstallResolver.INSTANCE)
61+
.predicate(BindResolver.INSTANCE)
62+
.predicate(ProvideResolver.INSTANCE)
7063
.subtypeOf(SeedInterceptor.class)
7164
.build();
7265
}
@@ -91,7 +84,7 @@ public InitState initialize(InitContext initContext) {
9184

9285
@SuppressWarnings("unchecked")
9386
private void detectModules(InitContext initContext) {
94-
initContext.scannedTypesBySpecification().get(installSpecification)
87+
initContext.scannedTypesByPredicate().get(InstallResolver.INSTANCE)
9588
.stream()
9689
.filter(Module.class::isAssignableFrom)
9790
.forEach(candidate -> InstallResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
@@ -107,7 +100,7 @@ private void detectModules(InitContext initContext) {
107100

108101
@SuppressWarnings("unchecked")
109102
private void detectBindings(InitContext initContext) {
110-
initContext.scannedTypesBySpecification().get(bindSpecification)
103+
initContext.scannedTypesByPredicate().get(BindResolver.INSTANCE)
111104
.forEach(candidate -> BindResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
112105
if (annotation.override()) {
113106
overridingBindings.add(new BindingDefinition<>(
@@ -127,7 +120,7 @@ private void detectBindings(InitContext initContext) {
127120

128121
@SuppressWarnings("unchecked")
129122
private void detectProviders(InitContext initContext) {
130-
initContext.scannedTypesBySpecification().get(providerSpecification)
123+
initContext.scannedTypesByPredicate().get(ProvideResolver.INSTANCE)
131124
.forEach(candidate -> ProvideResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
132125
if (annotation.override()) {
133126
overridingBindings.add(new ProviderDefinition<>((Class<Provider<Object>>) candidate));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright © 2013-2020, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
package org.seedstack.seed.core.internal.el;
10+
11+
import static org.seedstack.shed.reflect.ClassPredicates.classImplements;
12+
import static org.seedstack.shed.reflect.ClassPredicates.classModifierIs;
13+
14+
import java.lang.reflect.Modifier;
15+
import java.util.function.Predicate;
16+
import org.seedstack.seed.el.spi.ELHandler;
17+
18+
class ELHandlerPredicate implements Predicate<Class<?>> {
19+
static ELHandlerPredicate INSTANCE = new ELHandlerPredicate();
20+
21+
private ELHandlerPredicate() {
22+
// no instantiation allowed
23+
}
24+
25+
@Override
26+
public boolean test(Class<?> candidate) {
27+
return classImplements(ELHandler.class)
28+
.and(classModifierIs(Modifier.ABSTRACT).negate())
29+
.test(candidate);
30+
}
31+
}

core/src/main/java/org/seedstack/seed/core/internal/el/ELInterceptor.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.core.internal.el;
910

1011
import com.google.inject.Injector;
@@ -21,21 +22,14 @@
2122
import org.seedstack.seed.el.spi.ELHandler;
2223

2324
class ELInterceptor implements MethodInterceptor {
24-
25-
private Class<? extends Annotation> annotationClass;
26-
27-
private ELBinder.ExecutionPolicy policy;
28-
29-
// Get a map of annotation handler
25+
private final Class<? extends Annotation> annotationClass;
26+
private final ELBinder.ExecutionPolicy policy;
3027
@Inject
31-
private Map<Class<? extends Annotation>, Class<ELHandler>> elMap;
32-
28+
private Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap;
3329
@Inject
3430
private ELService elService;
35-
3631
@Inject
3732
private ELContextBuilder elContextBuilder;
38-
3933
@Inject
4034
private Injector injector;
4135

@@ -46,8 +40,8 @@ class ELInterceptor implements MethodInterceptor {
4640

4741
@Override
4842
public Object invoke(MethodInvocation invocation) throws Throwable {
49-
Class<ELHandler> handlerClass = elMap.get(this.annotationClass);
50-
ELHandler ELHandler = injector.getInstance(handlerClass);
43+
Class<ELHandler<?>> handlerClass = elMap.get(this.annotationClass);
44+
ELHandler<?> ELHandler = injector.getInstance(handlerClass);
5145

5246
// The policy defines if the EL is evaluated before the method, after or both.
5347

core/src/main/java/org/seedstack/seed/core/internal/el/ELModule.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.core.internal.el;
910

1011
import com.google.common.collect.ImmutableMap;
@@ -19,9 +20,9 @@
1920

2021
class ELModule extends AbstractModule {
2122
private final ExpressionFactory expressionFactory;
22-
private final Map<Class<? extends Annotation>, Class<ELHandler>> elMap;
23+
private final Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap;
2324

24-
ELModule(ExpressionFactory expressionFactory, Map<Class<? extends Annotation>, Class<ELHandler>> elMap) {
25+
ELModule(ExpressionFactory expressionFactory, Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap) {
2526
this.expressionFactory = expressionFactory;
2627
this.elMap = elMap;
2728
}
@@ -32,7 +33,7 @@ protected void configure() {
3233
bind(ELService.class).to(ELServiceInternal.class);
3334
bind(ELContextBuilder.class).to(ELContextBuilderImpl.class);
3435

35-
for (Class<ELHandler> elHandlerClass : elMap.values()) {
36+
for (Class<ELHandler<?>> elHandlerClass : elMap.values()) {
3637
bind(elHandlerClass);
3738
}
3839

@@ -41,6 +42,6 @@ protected void configure() {
4142
}
4243

4344
private static class AnnotationHandlersTypeLiteral
44-
extends TypeLiteral<Map<Class<? extends Annotation>, Class<ELHandler>>> {
45+
extends TypeLiteral<Map<Class<? extends Annotation>, Class<ELHandler<?>>>> {
4546
}
4647
}

core/src/main/java/org/seedstack/seed/core/internal/el/ELPlugin.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.core.internal.el;
910

1011
import io.nuun.kernel.api.plugin.InitState;
@@ -19,7 +20,6 @@
1920
import javax.el.ELContext;
2021
import javax.el.ExpressionFactory;
2122
import net.jodah.typetools.TypeResolver;
22-
import org.kametic.specifications.Specification;
2323
import org.seedstack.seed.SeedException;
2424
import org.seedstack.seed.core.internal.AbstractSeedPlugin;
2525
import org.seedstack.seed.el.spi.ELHandler;
@@ -32,7 +32,6 @@ public class ELPlugin extends AbstractSeedPlugin {
3232
static final Class<? extends ELContext> JUEL_CONTEXT_CLASS;
3333
private static final Object EXPRESSION_FACTORY;
3434
private static final Logger LOGGER = LoggerFactory.getLogger(ELPlugin.class);
35-
private final Specification<Class<?>> specificationELHandlers = classImplements(ELHandler.class);
3635
private ELModule elModule;
3736

3837
static {
@@ -106,36 +105,31 @@ public String name() {
106105

107106
@Override
108107
public Collection<ClasspathScanRequest> classpathScanRequests() {
109-
return classpathScanRequestBuilder().specification(specificationELHandlers).build();
108+
return classpathScanRequestBuilder().predicate(ELHandlerPredicate.INSTANCE).build();
110109
}
111110

112111
@SuppressWarnings("unchecked")
113112
@Override
114113
public InitState initialize(InitContext initContext) {
115114
if (isEnabled()) {
116-
Map<Class<? extends Annotation>, Class<ELHandler>> elMap = new HashMap<>();
117-
118-
// Scan all the ExpressionLanguageHandler
119-
Map<Specification, Collection<Class<?>>> scannedTypesBySpecification = initContext
120-
.scannedTypesBySpecification();
121-
Collection<Class<?>> elHandlerClasses = scannedTypesBySpecification.get(specificationELHandlers);
115+
Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap = new HashMap<>();
122116

123-
// Look for their type parameters
124-
for (Class<?> elHandlerClass : elHandlerClasses) {
117+
// Scan all the ExpressionLanguageHandler and look for their type parameters
118+
for (Class<?> elHandlerClass : initContext.scannedTypesByPredicate().get(ELHandlerPredicate.INSTANCE)) {
125119
Class<Annotation> typeParameterClass = (Class<Annotation>) TypeResolver.resolveRawArguments(
126-
ELHandler.class, (Class<ELHandler>) elHandlerClass)[0];
120+
ELHandler.class, (Class<ELHandler<?>>) elHandlerClass)[0];
127121
// transform this type parameters in a map of annotation, ExpressionHandler
128122
if (elMap.get(typeParameterClass) != null) {
129123
throw SeedException.createNew(ExpressionLanguageErrorCode.EL_ANNOTATION_IS_ALREADY_BIND)
130124
.put("annotation", typeParameterClass.getSimpleName())
131125
.put("handler", elHandlerClass);
132126
}
133-
elMap.put(typeParameterClass, (Class<ELHandler>) elHandlerClass);
127+
elMap.put(typeParameterClass, (Class<ELHandler<?>>) elHandlerClass);
134128
}
135129

136130
elModule = new ELModule((ExpressionFactory) EXPRESSION_FACTORY, elMap);
137131
} else {
138-
LOGGER.debug("Java EL is not present in the classpath, EL support disabled");
132+
LOGGER.info("Java EL is not present in the classpath, EL support disabled");
139133
}
140134

141135
return InitState.INITIALIZED;

core/src/main/java/org/seedstack/seed/core/internal/lifecycle/LifecycleExtension.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8+
89
package org.seedstack.seed.core.internal.lifecycle;
910

1011
import io.nuun.kernel.spi.KernelExtension;
@@ -26,22 +27,23 @@ public void starting(Collection<LifecycleManager> lifecycleManagers) {
2627
// this phase is not supported by LifecycleManager
2728
}
2829

30+
@Override
31+
public void injected(Collection<LifecycleManager> lifecycleManagers) {
32+
lifecycleManagers.forEach(LifecycleManager::starting);
33+
}
34+
2935
@Override
3036
public void started(Collection<LifecycleManager> lifecycleManagers) {
31-
for (LifecycleManager lifecycleManager : lifecycleManagers) {
32-
lifecycleManager.started();
33-
}
37+
lifecycleManagers.forEach(LifecycleManager::started);
3438
}
3539

3640
@Override
3741
public void stopping(Collection<LifecycleManager> lifecycleManagers) {
38-
for (LifecycleManager lifecycleManager : lifecycleManagers) {
39-
lifecycleManager.stopping();
40-
}
42+
lifecycleManagers.forEach(LifecycleManager::stopping);
4143
}
4244

4345
@Override
4446
public void stopped(Collection<LifecycleManager> lifecycleManagers) {
45-
// this phase is not supported by LifecycleManager
47+
lifecycleManagers.forEach(LifecycleManager::stopped);
4648
}
4749
}

core/src/main/java/org/seedstack/seed/core/internal/lifecycle/LifecycleManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
import java.lang.reflect.Method;
1212

1313
interface LifecycleManager {
14+
void starting();
15+
1416
void started();
1517

1618
void stopping();
1719

20+
void stopped();
21+
1822
void registerPreDestroy(Object o, Method m);
1923

2024
void registerAutoCloseable(AutoCloseable autoCloseable);

0 commit comments

Comments
 (0)