com.itemis.maven.plugins.cdi.internal.beans.CdiProducerBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdi-plugin-utils Show documentation
Show all versions of cdi-plugin-utils Show documentation
Provides an abstract Mojo that enables CDI-based dependency injection for Maven Plugins.
package com.itemis.maven.plugins.cdi.internal.beans;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Qualifier;
import org.jboss.weld.literal.DefaultLiteral;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
public class CdiProducerBean implements Bean {
private Method method;
private Object hostInstance;
private BeanManager beanManager;
private Set qualifiers;
private Set types;
private Class> instanceClass;
public CdiProducerBean(Method method, Object hostInstance, BeanManager beanManager, Type type, Class> instanceClass,
Set qualifiers) {
this.method = method;
this.hostInstance = hostInstance;
this.beanManager = beanManager;
this.instanceClass = instanceClass;
this.qualifiers = qualifiers;
this.types = calcBeanTypes(type);
}
private Set calcBeanTypes(Type implTpye) {
Set beanTypes = Sets.newHashSet();
if (implTpye instanceof ParameterizedType) {
beanTypes.add((ParameterizedType) implTpye);
} else {
Typed typedAnnotation = ((Class>) implTpye).getAnnotation(Typed.class);
if (typedAnnotation != null) {
for (Class> cls : typedAnnotation.value()) {
beanTypes.add(cls);
}
} else {
beanTypes.addAll(getTypeClasses((Class>) implTpye));
}
}
return beanTypes;
}
private Set> getTypeClasses(Class> cls) {
if (cls == null) {
return Collections.emptySet();
}
Set> classes = Sets.newHashSet();
classes.add(cls);
classes.addAll(getTypeClasses(cls.getSuperclass()));
for (Class> iface : cls.getInterfaces()) {
classes.addAll(getTypeClasses(iface));
}
return classes;
}
@SuppressWarnings("unchecked")
@Override
public T create(CreationalContext creationalContext) {
Object[] params = new Object[0];
Class>[] parameterTypes = this.method.getParameterTypes();
Annotation[][] parameterAnnotations = this.method.getParameterAnnotations();
params = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
Set qualifiers = getCdiQualifiers(parameterAnnotations[i]);
Class> paramType = parameterTypes[i];
Set> beans = this.beanManager.getBeans(paramType, qualifiers.toArray(new Annotation[qualifiers.size()]));
if (beans.size() == 1) {
Bean> bean = Iterables.get(beans, 0);
Object reference = this.beanManager.getReference(bean, paramType,
this.beanManager.createCreationalContext(bean));
params[i] = reference;
} else {
// FIXME handle -> ambiguous results
}
}
T instance = null;
try {
this.method.setAccessible(true);
instance = (T) this.method.invoke(this.hostInstance, params);
} catch (Throwable t) {
t.printStackTrace();
}
return instance;
}
@Override
public void destroy(T instance, CreationalContext creationalContext) {
}
@Override
public Set getTypes() {
return this.types;
}
@Override
public Set getQualifiers() {
return this.qualifiers;
}
@Override
public Class extends Annotation> getScope() {
return Dependent.class;
}
@Override
public String getName() {
return null;
}
@Override
public Set> getStereotypes() {
return Collections.emptySet();
}
@Override
public boolean isAlternative() {
return false;
}
@Override
public Class> getBeanClass() {
return this.instanceClass;
}
@Override
public Set getInjectionPoints() {
return Collections.emptySet();
}
@Override
public boolean isNullable() {
return true;
}
private Set getCdiQualifiers(Annotation[] annotattions) {
Set qualifiers = Sets.newHashSet();
for (Annotation annotation : annotattions) {
if (annotation.annotationType().isAnnotationPresent(Qualifier.class)) {
qualifiers.add(annotation);
}
}
if (qualifiers.isEmpty()) {
qualifiers.add(DefaultLiteral.INSTANCE);
}
return qualifiers;
}
}