Skip to content

Commit

Permalink
WELD-1144 Superclass of processed AnnotationType is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Aug 6, 2012
1 parent a0839a5 commit 30e561c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 51 deletions.
Expand Up @@ -22,6 +22,7 @@

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -40,6 +41,7 @@
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedParameter;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.annotated.slim.unbacked.UnbackedAnnotatedType;
import org.jboss.weld.bootstrap.events.ProcessInjectionPointImpl;
import org.jboss.weld.ejb.EJBApiAbstraction;
import org.jboss.weld.exceptions.DefinitionException;
Expand Down Expand Up @@ -170,16 +172,30 @@ public <T, X> MethodInjectionPoint<T, X> createMethodInjectionPoint(EnhancedAnno
*/
public List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPoints(Bean<?> declaringBean, EnhancedAnnotatedType<?> type, BeanManagerImpl manager) {
List<Set<FieldInjectionPoint<?, ?>>> injectableFieldsList = new ArrayList<Set<FieldInjectionPoint<?, ?>>>();
EnhancedAnnotatedType<?> t = type;
while (t != null && !t.getJavaClass().equals(Object.class)) {
ArraySet<FieldInjectionPoint<?, ?>> fields = new ArraySet<FieldInjectionPoint<?, ?>>();
for (EnhancedAnnotatedField<?, ?> annotatedField : t.getDeclaredEnhancedFields(Inject.class)) {
if (!annotatedField.isStatic()) {
addFieldInjectionPoint(annotatedField, fields, declaringBean, type.getJavaClass(), manager);

if (type.slim() instanceof UnbackedAnnotatedType<?>) {
// external AnnotatedTypes require special treatment
Collection<EnhancedAnnotatedField<?, ?>> allFields = type.getEnhancedFields(Inject.class);

for (Class<?> clazz = type.getJavaClass(); clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) {
ArraySet<FieldInjectionPoint<?, ?>> fields = new ArraySet<FieldInjectionPoint<?, ?>>();
for (EnhancedAnnotatedField<?, ?> field : allFields) {
if (!field.isStatic() && field.getJavaMember().getDeclaringClass().equals(clazz)) {
addFieldInjectionPoint(field, fields, declaringBean, type.getJavaClass(), manager);
}
}
injectableFieldsList.add(0, immutableSet(fields));
}
} else {
for (EnhancedAnnotatedType<?> t = type; t != null && !t.getJavaClass().equals(Object.class); t = t.getEnhancedSuperclass()) {
ArraySet<FieldInjectionPoint<?, ?>> fields = new ArraySet<FieldInjectionPoint<?, ?>>();
for (EnhancedAnnotatedField<?, ?> annotatedField : t.getDeclaredEnhancedFields(Inject.class)) {
if (!annotatedField.isStatic()) {
addFieldInjectionPoint(annotatedField, fields, declaringBean, t.getJavaClass(), manager);
}
}
injectableFieldsList.add(0, immutableSet(fields));
}
injectableFieldsList.add(0, immutableSet(fields));
t = t.getEnhancedSuperclass();
}
return immutableList(injectableFieldsList);
}
Expand Down
65 changes: 39 additions & 26 deletions impl/src/main/java/org/jboss/weld/util/Beans.java
Expand Up @@ -90,6 +90,7 @@
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.annotated.enhanced.MethodSignature;
import org.jboss.weld.annotated.slim.unbacked.UnbackedAnnotatedType;
import org.jboss.weld.bean.AbstractProducerBean;
import org.jboss.weld.bean.DecoratorImpl;
import org.jboss.weld.bean.InterceptorImpl;
Expand Down Expand Up @@ -120,6 +121,7 @@
import org.jboss.weld.resources.ClassLoaderResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.util.collections.ArraySet;
import org.jboss.weld.util.collections.HashSetSupplier;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
Expand Down Expand Up @@ -287,43 +289,54 @@ public static List<AnnotatedMethod<?>> getInterceptableMethods(AnnotatedType<?>
return annotatedMethods;
}

public static List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods(Bean<?> declaringBean, EnhancedAnnotatedType<?> type, BeanManagerImpl manager) {
public static <T> List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods(Bean<?> declaringBean, EnhancedAnnotatedType<T> type, BeanManagerImpl manager) {
List<Set<MethodInjectionPoint<?, ?>>> initializerMethodsList = new ArrayList<Set<MethodInjectionPoint<?, ?>>>();
// Keep track of all seen methods so we can ignore overridden methods
Multimap<MethodSignature, Package> seenMethods = Multimaps.newSetMultimap(new HashMap<MethodSignature, Collection<Package>>(), new Supplier<Set<Package>>() {
Multimap<MethodSignature, Package> seenMethods = Multimaps.newSetMultimap(new HashMap<MethodSignature, Collection<Package>>(), HashSetSupplier.<Package>instance());

public Set<Package> get() {
return new HashSet<Package>();
}
if (type.slim() instanceof UnbackedAnnotatedType<?>) {
// external AnnotatedTypes require special treatment
Collection<EnhancedAnnotatedMethod<?, ? super T>> allMethods = type.getEnhancedMethods();

});
EnhancedAnnotatedType<?> t = type;
while (t != null && !t.getJavaClass().equals(Object.class)) {
ArraySet<MethodInjectionPoint<?, ?>> initializerMethods = new ArraySet<MethodInjectionPoint<?, ?>>();
for (EnhancedAnnotatedMethod<?, ?> method : t.getDeclaredEnhancedMethods()) {
if (method.isAnnotationPresent(Inject.class) && !method.isStatic()) {
if (method.getAnnotation(Produces.class) != null) {
throw new DefinitionException(INITIALIZER_CANNOT_BE_PRODUCER, method, type);
} else if (method.getEnhancedParameters(Disposes.class).size() > 0) {
throw new DefinitionException(INITIALIZER_CANNOT_BE_DISPOSAL_METHOD, method, type);
} else if (method.getEnhancedParameters(Observes.class).size() > 0) {
throw new DefinitionException(INVALID_INITIALIZER, method);
} else if (method.isGeneric()) {
throw new DefinitionException(INITIALIZER_METHOD_IS_GENERIC, method, type);
} else {
if (!isOverridden(method, seenMethods)) {
initializerMethods.add(InjectionPointFactory.instance().createMethodInjectionPoint(method, declaringBean, type.getJavaClass(), false, manager));
}
for (Class<?> clazz = type.getJavaClass(); clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) {
ArraySet<MethodInjectionPoint<?, ?>> initializerMethods = new ArraySet<MethodInjectionPoint<?, ?>>();
for (EnhancedAnnotatedMethod<?, ?> method : allMethods) {
if (method.getJavaMember().getDeclaringClass().equals(clazz)) {
processPossibleInitializerMethod(type, method, initializerMethods, seenMethods, declaringBean, manager);
}
}
seenMethods.put(method.getSignature(), method.getPackage());
initializerMethodsList.add(0, immutableSet(initializerMethods));
}
} else {
for (EnhancedAnnotatedType<?> t = type; t != null && !t.getJavaClass().equals(Object.class); t = t.getEnhancedSuperclass()) {
ArraySet<MethodInjectionPoint<?, ?>> initializerMethods = new ArraySet<MethodInjectionPoint<?, ?>>();
for (EnhancedAnnotatedMethod<?, ?> method : t.getDeclaredEnhancedMethods()) {
processPossibleInitializerMethod(type, method, initializerMethods, seenMethods, declaringBean, manager);
}
initializerMethodsList.add(0, immutableSet(initializerMethods));
}
initializerMethodsList.add(0, immutableSet(initializerMethods));
t = t.getEnhancedSuperclass();
}
return immutableList(initializerMethodsList);
}

private static void processPossibleInitializerMethod(EnhancedAnnotatedType<?> type, EnhancedAnnotatedMethod<?, ?> method, Set<MethodInjectionPoint<?, ?>> initializerMethods, Multimap<MethodSignature, Package> seenMethods, Bean<?> declaringBean, BeanManagerImpl manager) {
if (method.isAnnotationPresent(Inject.class)) {
if (method.getAnnotation(Produces.class) != null) {
throw new DefinitionException(INITIALIZER_CANNOT_BE_PRODUCER, method, type);
} else if (method.getEnhancedParameters(Disposes.class).size() > 0) {
throw new DefinitionException(INITIALIZER_CANNOT_BE_DISPOSAL_METHOD, method, type);
} else if (method.getEnhancedParameters(Observes.class).size() > 0) {
throw new DefinitionException(INVALID_INITIALIZER, method);
} else if (method.isGeneric()) {
throw new DefinitionException(INITIALIZER_METHOD_IS_GENERIC, method, type);
}
if (!method.isStatic() && !isOverridden(method, seenMethods)) {
initializerMethods.add(InjectionPointFactory.instance().createMethodInjectionPoint(method, declaringBean, type.getJavaClass(), false, manager));
}
}
seenMethods.put(method.getSignature(), method.getPackage());
}

private static boolean isOverridden(EnhancedAnnotatedMethod<?, ?> method, Multimap<MethodSignature, Package> seenMethods) {
if (method.isPrivate()) {
return false;
Expand Down
@@ -1,12 +1,10 @@
package org.jboss.weld.tests.annotatedType.weld1144;

import javax.ejb.Stateless;
import javax.inject.Inject;

/**
* Session Bean implementation class CdiTest
*/
@Stateless
public class CdiTest1 {

@Inject
Expand Down
@@ -1,9 +1,7 @@
package org.jboss.weld.tests.annotatedType.weld1144;

import javax.ejb.Stateless;
import javax.inject.Inject;

@Stateless
public class CdiTest2 extends CdiTest1 {

@Inject
Expand Down
@@ -1,8 +1,6 @@
package org.jboss.weld.tests.annotatedType.weld1144;

import javax.ejb.Stateless;

@Stateless
public class SomeInjectedBean {

}
@@ -1,5 +1,7 @@
package org.jboss.weld.tests.annotatedType.weld1144;

import static junit.framework.Assert.assertNotNull;

import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.Extension;
import javax.inject.Inject;
Expand All @@ -9,12 +11,9 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertNotNull;

/**
* @author Richard Kennard
* @author Marko Luksa
Expand All @@ -23,7 +22,7 @@
public class Weld1144Test {

@Deployment
public static Archive getDeployment() {
public static Archive<?> getDeployment() {
return ShrinkWrap.create(BeanArchive.class)
.addPackage(Weld1144Test.class.getPackage())
.addAsServiceProvider(Extension.class, CdiExtension.class);
Expand All @@ -33,21 +32,14 @@ public static Archive getDeployment() {
private Instance<CdiTest2> test;

@Test
@Ignore
public void testChildClassFieldIsInjected() {
CdiTest2 cdiTest2 = test.get();
assertNotNull(cdiTest2.getSomeInjectedBean2());
}

@Test
@Ignore
public void testSuperclassFieldIsInjected() {
CdiTest2 cdiTest2 = test.get();
assertNotNull(cdiTest2.getSomeInjectedBean1());
}

@Test
public void testDummy() {
// remove it
}
}

0 comments on commit 30e561c

Please sign in to comment.