
io.vertx.up.uca.di.DiGuiceModule Maven / Gradle / Ivy
The newest version!
package io.vertx.up.uca.di;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import io.horizon.uca.log.Annal;
import io.vertx.up.util.Ut;
import javax.inject.Named;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.Set;
/**
* @author Lang
*/
public abstract class DiGuiceModule extends AbstractModule {
protected String bindConstructor(final Class clazz) {
// Standalone, Non-Constructor
if (Ut.isDefaultConstructor(clazz)) {
final Constructor constructor = Ut.constructor(clazz);
this.bind(clazz).toConstructor(constructor).asEagerSingleton();
this.logger().info("[ DI ] Constructor Bind: `{0}`", clazz);
return null;
} else {
return clazz.getName();
}
}
@SuppressWarnings("all")
protected Set bindInterface(final Class interfaceCls, final Set> implSet) {
if (!implSet.isEmpty()) {
final Set clazzSet = new HashSet<>();
if (1 == implSet.size()) {
final Class clazz = implSet.iterator().next();
this.bind(interfaceCls).to(clazz).asEagerSingleton();
this.logger().info("[ DI ] Interface Bind: `{0}`, interfaceCls = `{1}`", clazz, interfaceCls);
// clazzSet.add(clazz.getName());
} else {
// interface with multi classed injection
implSet.forEach(implCls -> {
if (implCls.isAnnotationPresent(Named.class)) {
final Annotation annotation = implCls.getAnnotation(Named.class);
final String name = Ut.invoke(annotation, "value");
this.logger().info("[ DI ] Interface Bind: `{0}`, interfaceCls = `{1}`, name = {2}",
implCls, interfaceCls, name);
this.bind(interfaceCls).annotatedWith(Names.named(name))
.to(implCls);
} else {
clazzSet.add(implCls.getName());
}
});
}
return clazzSet;
} else {
return null;
}
}
protected Annal logger() {
return Annal.get(this.getClass());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy