Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 3.8.5 (2019-03-22)

* [new] The `@Provide` annotation allows to register JSR-330 providers for producing injectable instances of a specific type.

# Version 3.8.4 (2019-03-12)

* [fix] Fix Jansi loading on unsupported platforms.
Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.8.4-SNAPSHOT</version>
<version>3.8.5-SNAPSHOT</version>
</parent>

<artifactId>seed-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed</artifactId>
<version>3.8.4-SNAPSHOT</version>
<version>3.8.5-SNAPSHOT</version>
</parent>

<artifactId>seed-core</artifactId>
Expand Down
38 changes: 38 additions & 0 deletions core/src/main/java/org/seedstack/seed/core/internal/Bindable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2013-2019, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.seedstack.seed.core.internal;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.inject.Binder;
import java.lang.annotation.Annotation;
import java.util.Optional;
import javax.inject.Qualifier;
import org.seedstack.shed.reflect.AnnotationPredicates;
import org.seedstack.shed.reflect.Annotations;

abstract class Bindable<T> {
final Class<? extends T> target;
final Annotation qualifier;

Bindable(Class<? extends T> target) {
this.target = checkNotNull(target, "Binding target should not be null");
this.qualifier = findQualifier(this.target).orElse(null);
}

abstract void apply(Binder binder);

private Optional<Annotation> findQualifier(Class<? extends T> target) {
return Annotations.on(target)
.traversingSuperclasses()
.traversingInterfaces()
.findAll()
.filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false))
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,47 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.seedstack.seed.core.internal;

import static com.google.common.base.Preconditions.checkNotNull;
package org.seedstack.seed.core.internal;

import com.google.inject.Binder;
import com.google.inject.binder.AnnotatedBindingBuilder;
import java.lang.annotation.Annotation;
import java.util.Optional;
import javax.inject.Qualifier;
import org.seedstack.seed.SeedException;
import org.seedstack.shed.reflect.AnnotationPredicates;
import org.seedstack.shed.reflect.Annotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BindingDefinition<E> {
class BindingDefinition<T> extends Bindable<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(BindingDefinition.class);
private final Class<E> key;
private final Class<? extends E> target;
private final Annotation qualifier;

public BindingDefinition(Class<? extends E> target) {
this(target, null);
}
private final Class<T> from;

public BindingDefinition(Class<? extends E> target, Class<E> fromKey) {
this.target = checkNotNull(target, "Binding target should not be null");
this.key = fromKey;
this.qualifier = findQualifier(this.target).orElse(null);
BindingDefinition(Class<? extends T> target, Class<T> from) {
super(target);
this.from = from;
}

public void apply(Binder binder) {
if (key != null) {
AnnotatedBindingBuilder<?> bind = binder.bind(key);
if (!key.isAssignableFrom(target)) {
if (from != null) {
AnnotatedBindingBuilder<T> bind = binder.bind(from);
if (!from.isAssignableFrom(target)) {
throw SeedException.createNew(CoreErrorCode.INVALID_BINDING)
.put("key", key)
.put("key", from)
.put("target", target);
}
if (qualifier != null) {
LOGGER.trace("Binding {} annotated with {} to {}", key.getName(), qualifier, target.getName());
bind.annotatedWith(qualifier).to(getExtendingClass(target));
LOGGER.debug("Binding {} annotated with {} to {}", from.getName(), qualifier, target.getName());
bind.annotatedWith(qualifier).to(target);
} else {
LOGGER.trace("Binding {} to {}", key.getName(), target.getName());
bind.to(getExtendingClass(target));
LOGGER.debug("Binding {} to {}", from.getName(), target.getName());
bind.to(target);
}
} else {
LOGGER.trace("Binding {} to itself", target.getName());
binder.bind(target);
if (qualifier != null) {
LOGGER.debug("Binding {} annotated with {} to itself", target.getName(), qualifier);
binder.bind(target).annotatedWith(qualifier);
} else {
LOGGER.debug("Binding {} to itself", target.getName());
binder.bind(target);
}
}
}

private Optional<Annotation> findQualifier(Class<? extends E> target) {
return Annotations.on(target)
.traversingSuperclasses()
.traversingInterfaces()
.findAll()
.filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false))
.findFirst();
}

@SuppressWarnings("unchecked")
private <C extends Class<?>> C getExtendingClass(Class<?> aClass) {
return (C) aClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal;

import com.google.inject.AbstractModule;
Expand All @@ -17,10 +18,10 @@
class CoreModule extends AbstractModule {
private final Logger LOGGER = LoggerFactory.getLogger(CoreModule.class);
private final Collection<? extends Module> modules;
private final Set<BindingDefinition> bindings;
private final Set<Bindable> bindings;
private final boolean overriding;

CoreModule(Collection<? extends Module> modules, Set<BindingDefinition> bindings, boolean overriding) {
CoreModule(Collection<? extends Module> modules, Set<Bindable> bindings, boolean overriding) {
this.modules = modules;
this.bindings = bindings;
this.overriding = overriding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal;

import com.google.common.base.Strings;
Expand All @@ -16,6 +17,7 @@
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Provider;
import org.kametic.specifications.Specification;
import org.seedstack.seed.core.internal.utils.SpecificationBuilder;
import org.seedstack.shed.reflect.Classes;
Expand All @@ -32,10 +34,12 @@ public class CorePlugin extends AbstractSeedPlugin {
InstallResolver.INSTANCE).build();
private static final Specification<Class<?>> bindSpecification = new SpecificationBuilder<>(
BindResolver.INSTANCE).build();
private static final Specification<Class<?>> providerSpecification = new SpecificationBuilder<>(
ProvideResolver.INSTANCE).build();
private final Set<Class<? extends Module>> modules = new HashSet<>();
private final Set<Class<? extends Module>> overridingModules = new HashSet<>();
private final Set<BindingDefinition> bindings = new HashSet<>();
private final Set<BindingDefinition> overridingBindings = new HashSet<>();
private final Set<Bindable> bindings = new HashSet<>();
private final Set<Bindable> overridingBindings = new HashSet<>();

@Override
public String name() {
Expand All @@ -52,10 +56,10 @@ public Collection<ClasspathScanRequest> classpathScanRequests() {
return classpathScanRequestBuilder()
.specification(installSpecification)
.specification(bindSpecification)
.specification(providerSpecification)
.build();
}

@SuppressWarnings("unchecked")
@Override
public InitState initialize(InitContext initContext) {
String autodetectModules = initContext.kernelParam(AUTODETECT_MODULES_KERNEL_PARAM);
Expand All @@ -65,6 +69,7 @@ public InitState initialize(InitContext initContext) {
String autodetectBindings = initContext.kernelParam(AUTODETECT_BINDINGS_KERNEL_PARAM);
if (Strings.isNullOrEmpty(autodetectBindings) || Boolean.parseBoolean(autodetectBindings)) {
detectBindings(initContext);
detectProviders(initContext);
}
return InitState.INITIALIZED;
}
Expand Down Expand Up @@ -101,6 +106,18 @@ private void detectBindings(InitContext initContext) {
}));
}

@SuppressWarnings("unchecked")
private void detectProviders(InitContext initContext) {
initContext.scannedTypesBySpecification().get(providerSpecification)
.forEach(candidate -> ProvideResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
if (annotation.override()) {
overridingBindings.add(new ProviderDefinition<>((Class<Provider<Object>>) candidate));
} else {
bindings.add(new ProviderDefinition<>((Class<Provider<Object>>) candidate));
}
}));
}

@Override
public Object nativeUnitModule() {
return new CoreModule(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright © 2013-2019, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal;

import org.seedstack.seed.Provide;
import org.seedstack.shed.reflect.StandardAnnotationResolver;

class ProvideResolver extends StandardAnnotationResolver<Class<?>, Provide> {
static ProvideResolver INSTANCE = new ProvideResolver();

private ProvideResolver() {
// no external instantiation allowed
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright © 2013-2019, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.seedstack.seed.core.internal;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.inject.Binder;
import com.google.inject.TypeLiteral;
import com.google.inject.binder.AnnotatedBindingBuilder;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import javax.inject.Provider;
import net.jodah.typetools.TypeResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ProviderDefinition<T, P extends Provider<T>> extends Bindable<P> {
private static final Logger LOGGER = LoggerFactory.getLogger(ProviderDefinition.class);
private final Type from;

ProviderDefinition(Class<P> provider) {
super(provider);
Type type = checkNotNull(TypeResolver.resolveGenericType(Provider.class, provider),
"Unable to resolve generic type of provider " + provider.getName());
if (type instanceof ParameterizedType) {
from = ((ParameterizedType) type).getActualTypeArguments()[0];
} else {
throw new IllegalArgumentException("A generic type is required for provider " + provider.getName());
}
}

@SuppressWarnings("unchecked")
public void apply(Binder binder) {
AnnotatedBindingBuilder<T> bind = binder.bind((TypeLiteral<T>) TypeLiteral.get(from));
if (qualifier != null) {
LOGGER.debug("Binding {} annotated with {} to provider {}",
from.getTypeName(),
qualifier,
target.getName());
bind.annotatedWith(qualifier).toProvider(target);
} else {
LOGGER.debug("Binding {} to provider {}", from.getTypeName(), target.getName());
bind.toProvider(target);
}
}
}
74 changes: 74 additions & 0 deletions core/src/test/java/custom/CustomApplicationProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright © 2013-2019, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package custom;

import java.io.File;
import java.util.Map;
import javax.inject.Provider;
import org.seedstack.coffig.Coffig;
import org.seedstack.seed.Application;
import org.seedstack.seed.ClassConfiguration;
import org.seedstack.seed.Provide;

@Provide(override = true)
public class CustomApplicationProvider implements Provider<Application> {
@Override
public Application get() {
return new Application() {
@Override
public String getName() {
return "custom";
}

@Override
public String getId() {
return null;
}

@Override
public String getVersion() {
return null;
}

@Override
public File getStorageLocation(String context) {
return null;
}

@Override
public boolean isStorageEnabled() {
return false;
}

@Override
public Coffig getConfiguration() {
return null;
}

@Override
public <T> ClassConfiguration<T> getConfiguration(Class<T> someClass) {
return null;
}

@Override
public String substituteWithConfiguration(String value) {
return null;
}

@Override
public Map<String, String> getKernelParameters() {
return null;
}

@Override
public String[] getArguments() {
return new String[0];
}
};
}
}
Loading