use of javax.enterprise.inject.Typed in project deltaspike by apache.
the class BeanBuilder method readFromType.
/**
* <p>
* Read the {@link AnnotatedType}, creating a bean from the class and it's
* annotations.
* </p>
* <p/>
* <p>
* By default the bean lifecycle will wrap the result of calling
* {@link BeanManager#createInjectionTarget(AnnotatedType)}.
* </p>
* <p/>
* <p>
* {@link BeanBuilder} does <em>not</em> support reading members of the class
* to create producers or observer methods.
* </p>
*
* @param type the type to read
*/
public BeanBuilder<T> readFromType(AnnotatedType<T> type) {
this.beanClass = type.getJavaClass();
if (beanLifecycle == null) {
setDefaultBeanLifecycle(type);
}
this.qualifiers = new HashSet<Annotation>();
this.stereotypes = new HashSet<Class<? extends Annotation>>();
this.types = new HashSet<Type>();
for (Annotation annotation : type.getAnnotations()) {
if (beanManager.isQualifier(annotation.annotationType())) {
this.qualifiers.add(annotation);
} else if (beanManager.isScope(annotation.annotationType())) {
this.scope = annotation.annotationType();
} else if (beanManager.isStereotype(annotation.annotationType())) {
this.stereotypes.add(annotation.annotationType());
}
if (annotation instanceof Named) {
this.name = ((Named) annotation).value();
if (name == null || name.length() == 0) {
name = createDefaultBeanName(type);
}
}
if (annotation instanceof Alternative) {
this.alternative = true;
}
}
if (type.isAnnotationPresent(Typed.class)) {
Typed typed = type.getAnnotation(Typed.class);
this.types.addAll(Arrays.asList(typed.value()));
} else {
for (Class<?> c = type.getJavaClass(); c != Object.class && c != null; c = c.getSuperclass()) {
this.types.add(c);
}
Collections.addAll(this.types, type.getJavaClass().getInterfaces());
this.types.add(Object.class);
}
if (qualifiers.isEmpty()) {
qualifiers.add(new DefaultLiteral());
}
qualifiers.add(new AnyLiteral());
this.id = ImmutableBeanWrapper.class.getName() + ":" + Annotateds.createTypeId(type);
return this;
}
use of javax.enterprise.inject.Typed in project deltaspike by apache.
the class MockExtension method onProcessInjectionTarget.
public <X> void onProcessInjectionTarget(@Observes ProcessInjectionTarget<X> processInjectionTarget, BeanManager beanManager) {
if (!isActivated) {
return;
}
for (MockFilter mockFilter : mockFilters) {
if (!mockFilter.isMockedImplementationSupported(beanManager, processInjectionTarget.getAnnotatedType())) {
return;
}
}
List<Annotation> qualifiers = new ArrayList<Annotation>();
for (Annotation annotation : processInjectionTarget.getAnnotatedType().getAnnotations()) {
if (beanManager.isQualifier(annotation.annotationType())) {
qualifiers.add(annotation);
}
}
Typed typed = processInjectionTarget.getAnnotatedType().getAnnotation(Typed.class);
List<Type> foundTypes = new ArrayList<Type>();
if (typed != null) {
Collections.addAll(foundTypes, typed.value());
} else {
foundTypes.addAll(extractTypes(processInjectionTarget.getAnnotatedType().getJavaClass()));
}
if (foundTypes.isEmpty()) {
return;
}
final InjectionTarget<X> originalInjectionTarget = processInjectionTarget.getInjectionTarget();
processInjectionTarget.setInjectionTarget(new MockAwareInjectionTargetWrapper<X>(beanManager, originalInjectionTarget, foundTypes, qualifiers));
}
use of javax.enterprise.inject.Typed in project deltaspike by apache.
the class AbstractMockManager method addMock.
@Override
public void addMock(Object mockInstance, Annotation... qualifiers) {
//check if this method gets used without changing the default-config
if (!TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS && !TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS) {
throw new IllegalStateException("The support for mocked CDI-Beans is disabled " + "due to a reduced portability across different CDI-implementations. " + "Please set '" + TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS_KEY + "' and/or '" + TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS_KEY + "' to 'true' " + "(in 'META-INF/apache-deltaspike.properties') on your test-classpath.");
}
Class<?> mockClass = mockInstance.getClass();
Class<?> beanClass = mockClass.getSuperclass();
if (beanClass == null) {
beanClass = mockClass;
}
if (Object.class.equals(beanClass)) {
throw new IllegalArgumentException(mockInstance.getClass().getName() + " isn't a supported approach for mocking -> please extend from the original class.");
}
TypedMock typedMock = mockClass.getAnnotation(TypedMock.class);
if (typedMock == null) {
typedMock = beanClass.getAnnotation(TypedMock.class);
}
Class[] specifiedTypes = null;
if (typedMock != null) {
specifiedTypes = typedMock.value();
} else {
Typed typed = mockClass.getAnnotation(Typed.class);
if (typed == null || typed.value().length == 0) {
typed = beanClass.getAnnotation(Typed.class);
}
if (typed != null && typed.value().length > 0) {
specifiedTypes = typed.value();
}
}
if (specifiedTypes != null) {
for (Class typedClass : specifiedTypes) {
this.registeredMocks.put(new BeanCacheKey(typedClass, qualifiers), mockInstance);
}
} else {
this.registeredMocks.put(new BeanCacheKey(beanClass, qualifiers), mockInstance);
}
}
use of javax.enterprise.inject.Typed in project deltaspike by apache.
the class MockExtension method onProcessProducer.
public <X, T> void onProcessProducer(@Observes ProcessProducer<X, T> processProducer, BeanManager beanManager) {
if (!isActivated) {
return;
}
for (MockFilter mockFilter : mockFilters) {
if (!mockFilter.isMockedImplementationSupported(beanManager, processProducer.getAnnotatedMember())) {
return;
}
}
final Producer<T> originalProducer = processProducer.getProducer();
AnnotatedMember<X> annotatedMember = processProducer.getAnnotatedMember();
List<Annotation> qualifiers = new ArrayList<Annotation>();
for (Annotation annotation : annotatedMember.getAnnotations()) {
if (beanManager.isQualifier(annotation.annotationType())) {
qualifiers.add(annotation);
}
}
Typed typed = annotatedMember.getAnnotation(Typed.class);
List<Type> foundTypes = new ArrayList<Type>();
if (typed != null) {
Collections.addAll(foundTypes, typed.value());
} else if (annotatedMember.getBaseType() instanceof Class) {
foundTypes.addAll(extractTypes((Class) annotatedMember.getBaseType()));
}
if (foundTypes.isEmpty()) {
return;
}
processProducer.setProducer(new MockAwareProducerWrapper<T>(beanManager, originalProducer, foundTypes, qualifiers));
}
Aggregations