Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WELD-1111 PART2 #194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -104,7 +104,7 @@ public static <X, T> EEResourceProducerField<X, T> of(WeldField<T, ? super X> fi


protected EEResourceProducerField(WeldField<T, ? super X> field, AbstractClassBean<X> declaringBean, BeanManagerImpl manager, ServiceRegistry services) { protected EEResourceProducerField(WeldField<T, ? super X> field, AbstractClassBean<X> declaringBean, BeanManagerImpl manager, ServiceRegistry services) {
super(field, declaringBean, manager, services); super(field, declaringBean, manager, services);
this.injectionPoint = FieldInjectionPoint.of(declaringBean, field); this.injectionPoint = FieldInjectionPoint.of(declaringBean, null, field);
} }


@Override @Override
Expand Down
Expand Up @@ -102,7 +102,7 @@ protected ObserverMethodImpl(final WeldMethod<T, ? super X> observer, final RIBe
this.observerMethod = MethodInjectionPoint.of(declaringBean, observer); this.observerMethod = MethodInjectionPoint.of(declaringBean, observer);


WeldParameter<?, ? super X> eventArgument = observerMethod.getAnnotatedParameters(Observes.class).get(0); WeldParameter<?, ? super X> eventArgument = observerMethod.getAnnotatedParameters(Observes.class).get(0);
this.eventType = TypeVariableResolver.resolveVariables(declaringBean, eventArgument.getBaseType()); this.eventType = TypeVariableResolver.resolveVariables(declaringBean.getBeanClass(), eventArgument.getBaseType());
this.id = ID_PREFIX + ID_SEPARATOR + /*manager.getId() + ID_SEPARATOR +*/ ObserverMethod.class.getSimpleName() + ID_SEPARATOR + declaringBean.getBeanClass().getName() + "." + observer.getSignature(); this.id = ID_PREFIX + ID_SEPARATOR + /*manager.getId() + ID_SEPARATOR +*/ ObserverMethod.class.getSimpleName() + ID_SEPARATOR + declaringBean.getBeanClass().getName() + "." + observer.getSignature();
this.bindings = new HashSet<Annotation>(eventArgument.getMetaAnnotations(Qualifier.class)); this.bindings = new HashSet<Annotation>(eventArgument.getMetaAnnotations(Qualifier.class));
this.reception = eventArgument.getAnnotation(Observes.class).notifyObserver(); this.reception = eventArgument.getAnnotation(Observes.class).notifyObserver();
Expand Down
Expand Up @@ -35,14 +35,18 @@
import javax.inject.Inject; import javax.inject.Inject;


import edu.umd.cs.findbugs.annotations.SuppressWarnings; import edu.umd.cs.findbugs.annotations.SuppressWarnings;
import org.jboss.weld.Container;
import org.jboss.weld.bean.proxy.DecoratorProxy; import org.jboss.weld.bean.proxy.DecoratorProxy;
import org.jboss.weld.exceptions.IllegalStateException; import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.exceptions.InvalidObjectException; import org.jboss.weld.exceptions.InvalidObjectException;
import org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy; import org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.introspector.ForwardingWeldField; import org.jboss.weld.introspector.ForwardingWeldField;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldField; import org.jboss.weld.introspector.WeldField;
import org.jboss.weld.logging.messages.ReflectionMessage; import org.jboss.weld.logging.messages.ReflectionMessage;
import org.jboss.weld.manager.BeanManagerImpl; import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.util.AnnotatedTypes; import org.jboss.weld.util.AnnotatedTypes;
import org.jboss.weld.util.reflection.Reflections; import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.TypeVariableResolver; import org.jboss.weld.util.reflection.TypeVariableResolver;
Expand All @@ -54,19 +58,22 @@ public class FieldInjectionPoint<T, X> extends ForwardingWeldField<T, X> impleme


@SuppressWarnings(value = "SE_BAD_FIELD", justification = "If the bean is not serializable, we won't ever try to serialize the injection point") @SuppressWarnings(value = "SE_BAD_FIELD", justification = "If the bean is not serializable, we won't ever try to serialize the injection point")
private final Bean<?> declaringBean; private final Bean<?> declaringBean;
private final WeldClass<?> injectionTargetClass; // used only when declaringBean is null
private final WeldField<T, X> field; private final WeldField<T, X> field;
private final boolean delegate; private final boolean delegate;
private final boolean cacheable; private final boolean cacheable;
private Bean<?> cachedBean; private Bean<?> cachedBean;
private Type type; private Type type;




public static <T, X> FieldInjectionPoint<T, X> of(Bean<?> declaringBean, WeldField<T, X> field) { public static <T, X> FieldInjectionPoint<T, X> of(Bean<?> declaringBean, WeldClass<?> injectionTargetClass, WeldField<T, X> field) {
return new FieldInjectionPoint<T, X>(declaringBean, field); return new FieldInjectionPoint<T, X>(declaringBean, injectionTargetClass, field);
} }


protected FieldInjectionPoint(Bean<?> declaringBean, WeldField<T, X> field) { protected FieldInjectionPoint(Bean<?> declaringBean, WeldClass<?> injectionTargetClass, WeldField<T, X> field) {
assert (declaringBean != null || injectionTargetClass != null) : "both declaringBean and injectionTargetClass are null";
this.declaringBean = declaringBean; this.declaringBean = declaringBean;
this.injectionTargetClass = injectionTargetClass;
this.field = field; this.field = field;
this.delegate = isAnnotationPresent(Inject.class) && isAnnotationPresent(Delegate.class) && declaringBean instanceof Decorator<?>; this.delegate = isAnnotationPresent(Inject.class) && isAnnotationPresent(Delegate.class) && declaringBean instanceof Decorator<?>;
this.cacheable = !delegate && !InjectionPoint.class.isAssignableFrom(field.getJavaMember().getType()) && !Instance.class.isAssignableFrom(field.getJavaMember().getType()); this.cacheable = !delegate && !InjectionPoint.class.isAssignableFrom(field.getJavaMember().getType()) && !Instance.class.isAssignableFrom(field.getJavaMember().getType());
Expand Down Expand Up @@ -97,6 +104,10 @@ public Bean<?> getBean() {
return declaringBean; return declaringBean;
} }


public WeldClass<?> getInjectionTargetClass() {
return injectionTargetClass;
}

public WeldField<T, X> getWeldField() { public WeldField<T, X> getWeldField() {
return field; return field;
} }
Expand Down Expand Up @@ -158,7 +169,7 @@ public boolean isDelegate() {


public Type getType() { public Type getType() {
if (type == null) { if (type == null) {
type = TypeVariableResolver.resolveVariables(getBean(), getBaseType()); type = TypeVariableResolver.resolveVariables(getBean() == null ? getInjectionTargetClass().getJavaClass() : getBean().getBeanClass(), getBaseType());
} }
return type; return type;
} }
Expand All @@ -182,19 +193,26 @@ private static class SerializationProxy<T> extends WeldInjectionPointSerializati
private static final long serialVersionUID = -3491482804822264969L; private static final long serialVersionUID = -3491482804822264969L;


private final String fieldName; private final String fieldName;
private final String injectionTargetClassName;


public SerializationProxy(FieldInjectionPoint<T, ?> injectionPoint) { public SerializationProxy(FieldInjectionPoint<T, ?> injectionPoint) {
super(injectionPoint); super(injectionPoint);
this.fieldName = injectionPoint.getName(); this.fieldName = injectionPoint.getName();
this.injectionTargetClassName = injectionPoint.getInjectionTargetClass().getName();
} }


private Object readResolve() { private Object readResolve() {
WeldField<T, ?> field = getWeldField(); WeldField<T, ?> field = getWeldField();
Bean<T> bean = getDeclaringBean(); if (field == null || (getDeclaringBean() == null && getDeclaringBeanId() != null)) {
if (field == null || (bean == null && getDeclaringBeanId() != null)) {
throw new IllegalStateException(ReflectionMessage.UNABLE_TO_GET_FIELD_ON_DESERIALIZATION, getDeclaringBeanId(), getDeclaringWeldClass(), fieldName); throw new IllegalStateException(ReflectionMessage.UNABLE_TO_GET_FIELD_ON_DESERIALIZATION, getDeclaringBeanId(), getDeclaringWeldClass(), fieldName);
} }
return FieldInjectionPoint.of(getDeclaringBean(), getWeldField());
return FieldInjectionPoint.of(getDeclaringBean(), getInjectionTargetClass(), getWeldField());
}

protected WeldClass<?> getInjectionTargetClass() {
Class<?> clazz = getService(ResourceLoader.class).classForName(injectionTargetClassName);
return getService(ClassTransformer.class).loadClass(clazz);
} }


protected WeldField<T, ?> getWeldField() { protected WeldField<T, ?> getWeldField() {
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.jboss.weld.injection; package org.jboss.weld.injection;


import org.jboss.weld.Container; import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.introspector.WeldAnnotated; import org.jboss.weld.introspector.WeldAnnotated;
import org.jboss.weld.introspector.WeldClass; import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.resources.ClassTransformer; import org.jboss.weld.resources.ClassTransformer;
Expand All @@ -37,22 +38,26 @@ abstract static class WeldInjectionPointSerializationProxy<T, S> implements Seri


public WeldInjectionPointSerializationProxy(WeldInjectionPoint<T, S> injectionPoint) { public WeldInjectionPointSerializationProxy(WeldInjectionPoint<T, S> injectionPoint) {
this.declaringBeanId = this.declaringBeanId =
injectionPoint.getBean() == null ? null : Container.instance().services().get(ContextualStore.class).putIfAbsent(injectionPoint.getBean()); injectionPoint.getBean() == null ? null : getService(ContextualStore.class).putIfAbsent(injectionPoint.getBean());
this.declaringClass = injectionPoint.getDeclaringType().getJavaClass(); this.declaringClass = injectionPoint.getDeclaringType().getJavaClass();
} }


protected Bean<T> getDeclaringBean() { protected Bean<T> getDeclaringBean() {
return declaringBeanId == null ? null : Container.instance().services().get(ContextualStore.class).<Bean<T>, T>getContextual(declaringBeanId); return declaringBeanId == null ? null : getService(ContextualStore.class).<Bean<T>, T>getContextual(declaringBeanId);
} }


protected WeldClass<?> getDeclaringWeldClass() { protected WeldClass<?> getDeclaringWeldClass() {
return Container.instance().services().get(ClassTransformer.class).loadClass(declaringClass); return getService(ClassTransformer.class).loadClass(declaringClass);
} }


protected String getDeclaringBeanId() { protected String getDeclaringBeanId() {
return declaringBeanId; return declaringBeanId;
} }


protected <E extends Service> E getService(Class<E> serviceClass) {
return Container.instance().services().get(serviceClass);
}

} }


WeldClass<?> getDeclaringType(); WeldClass<?> getDeclaringType();
Expand Down
45 changes: 19 additions & 26 deletions impl/src/main/java/org/jboss/weld/util/Beans.java
Expand Up @@ -188,21 +188,22 @@ public static boolean isBeanProxyable(Bean<?> bean) {


public static List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPoints(Bean<?> declaringBean, WeldClass<?> weldClass) { public static List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPoints(Bean<?> declaringBean, WeldClass<?> weldClass) {
List<Set<FieldInjectionPoint<?, ?>>> list = new ArrayList<Set<FieldInjectionPoint<?, ?>>>(); List<Set<FieldInjectionPoint<?, ?>>> list = new ArrayList<Set<FieldInjectionPoint<?, ?>>>();
while (weldClass != null && !weldClass.getJavaClass().equals(Object.class)) { WeldClass<?> c = weldClass;
list.add(0, getDeclaredFieldInjectionPoints(declaringBean, weldClass)); while (c != null && !c.getJavaClass().equals(Object.class)) {
weldClass = weldClass.getWeldSuperclass(); list.add(0, getDeclaredFieldInjectionPoints(declaringBean, weldClass, c));
c = c.getWeldSuperclass();
} }
return list; return list;
} }


private static Set<FieldInjectionPoint<?, ?>> getDeclaredFieldInjectionPoints(Bean<?> declaringBean, WeldClass<?> weldClass) { private static Set<FieldInjectionPoint<?, ?>> getDeclaredFieldInjectionPoints(Bean<?> declaringBean, WeldClass<?> injectionTargetClass, WeldClass<?> fieldDeclaringClass) {
ArraySet<FieldInjectionPoint<?, ?>> fields = new ArraySet<FieldInjectionPoint<?, ?>>(); ArraySet<FieldInjectionPoint<?, ?>> fields = new ArraySet<FieldInjectionPoint<?, ?>>();
for (WeldField<?, ?> field : weldClass.getDeclaredWeldFields(Inject.class)) { for (WeldField<?, ?> field : fieldDeclaringClass.getDeclaredWeldFields(Inject.class)) {
if (!field.isStatic() && !field.isAnnotationPresent(Produces.class)) { if (!field.isStatic() && !field.isAnnotationPresent(Produces.class)) {
if (field.isFinal()) { if (field.isFinal()) {
throw new DefinitionException(QUALIFIER_ON_FINAL_FIELD, field); throw new DefinitionException(QUALIFIER_ON_FINAL_FIELD, field);
} }
fields.add(FieldInjectionPoint.of(declaringBean, field)); fields.add(FieldInjectionPoint.of(declaringBean, injectionTargetClass, field));
} }
} }
fields.trimToSize(); fields.trimToSize();
Expand Down Expand Up @@ -299,37 +300,25 @@ public Set<Package> get() {
public static Set<WeldInjectionPoint<?, ?>> getEjbInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) { public static Set<WeldInjectionPoint<?, ?>> getEjbInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(EjbInjectionServices.class)) { if (manager.getServices().contains(EjbInjectionServices.class)) {
Class<? extends Annotation> ejbAnnotationType = manager.getServices().get(EJBApiAbstraction.class).EJB_ANNOTATION_CLASS; Class<? extends Annotation> ejbAnnotationType = manager.getServices().get(EJBApiAbstraction.class).EJB_ANNOTATION_CLASS;
ArraySet<WeldInjectionPoint<?, ?>> ejbInjectionPoints = new ArraySet<WeldInjectionPoint<?, ?>>(); return getInjectionPoints(declaringBean, type, ejbAnnotationType);
for (WeldField<?, ?> field : type.getWeldFields(ejbAnnotationType)) {
ejbInjectionPoints.add(FieldInjectionPoint.of(declaringBean, field));
}
return ejbInjectionPoints.trimToSize();
} else { } else {
return Collections.emptySet(); return Collections.emptySet();
} }
} }


public static Set<WeldInjectionPoint<?, ?>> getPersistenceContextInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) { public static Set<WeldInjectionPoint<?, ?>> getPersistenceContextInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(JpaInjectionServices.class)) { if (manager.getServices().contains(JpaInjectionServices.class)) {
ArraySet<WeldInjectionPoint<?, ?>> jpaInjectionPoints = new ArraySet<WeldInjectionPoint<?, ?>>();
Class<? extends Annotation> persistenceContextAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_CONTEXT_ANNOTATION_CLASS; Class<? extends Annotation> persistenceContextAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_CONTEXT_ANNOTATION_CLASS;
for (WeldField<?, ?> field : type.getWeldFields(persistenceContextAnnotationType)) { return getInjectionPoints(declaringBean, type, persistenceContextAnnotationType);
jpaInjectionPoints.add(FieldInjectionPoint.of(declaringBean, field));
}
return jpaInjectionPoints.trimToSize();
} else { } else {
return Collections.emptySet(); return Collections.emptySet();
} }
} }


public static Set<WeldInjectionPoint<?, ?>> getPersistenceUnitInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) { public static Set<WeldInjectionPoint<?, ?>> getPersistenceUnitInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(JpaInjectionServices.class)) { if (manager.getServices().contains(JpaInjectionServices.class)) {
ArraySet<WeldInjectionPoint<?, ?>> jpaInjectionPoints = new ArraySet<WeldInjectionPoint<?, ?>>();
Class<? extends Annotation> persistenceUnitAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_UNIT_ANNOTATION_CLASS; Class<? extends Annotation> persistenceUnitAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_UNIT_ANNOTATION_CLASS;
for (WeldField<?, ?> field : type.getWeldFields(persistenceUnitAnnotationType)) { return getInjectionPoints(declaringBean, type, persistenceUnitAnnotationType);
jpaInjectionPoints.add(FieldInjectionPoint.of(declaringBean, field));
}
return jpaInjectionPoints.trimToSize();
} else { } else {
return Collections.emptySet(); return Collections.emptySet();
} }
Expand All @@ -338,16 +327,20 @@ public Set<Package> get() {
public static Set<WeldInjectionPoint<?, ?>> getResourceInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) { public static Set<WeldInjectionPoint<?, ?>> getResourceInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(ResourceInjectionServices.class)) { if (manager.getServices().contains(ResourceInjectionServices.class)) {
Class<? extends Annotation> resourceAnnotationType = manager.getServices().get(EJBApiAbstraction.class).RESOURCE_ANNOTATION_CLASS; Class<? extends Annotation> resourceAnnotationType = manager.getServices().get(EJBApiAbstraction.class).RESOURCE_ANNOTATION_CLASS;
ArraySet<WeldInjectionPoint<?, ?>> resourceInjectionPoints = new ArraySet<WeldInjectionPoint<?, ?>>(); return getInjectionPoints(declaringBean, type, resourceAnnotationType);
for (WeldField<?, ?> field : type.getWeldFields(resourceAnnotationType)) {
resourceInjectionPoints.add(FieldInjectionPoint.of(declaringBean, field));
}
return resourceInjectionPoints.trimToSize();
} else { } else {
return Collections.emptySet(); return Collections.emptySet();
} }
} }


private static Set<WeldInjectionPoint<?, ?>> getInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, Class<? extends Annotation> annotationType) {
ArraySet<WeldInjectionPoint<?, ?>> set = new ArraySet<WeldInjectionPoint<?, ?>>();
for (WeldField<?, ?> field : type.getWeldFields(annotationType)) {
set.add(FieldInjectionPoint.of(declaringBean, type, field));
}
return set.trimToSize();
}

public static List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods(Bean<?> declaringBean, WeldClass<?> type) { public static List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods(Bean<?> declaringBean, WeldClass<?> type) {
List<Set<MethodInjectionPoint<?, ?>>> initializerMethodsList = new ArrayList<Set<MethodInjectionPoint<?, ?>>>(); List<Set<MethodInjectionPoint<?, ?>>> initializerMethodsList = new ArrayList<Set<MethodInjectionPoint<?, ?>>>();
// Keep track of all seen methods so we can ignore overridden methods // Keep track of all seen methods so we can ignore overridden methods
Expand Down
@@ -1,6 +1,5 @@
package org.jboss.weld.util.reflection; package org.jboss.weld.util.reflection;


import javax.enterprise.inject.spi.Bean;
import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
Expand All @@ -22,16 +21,8 @@ public TypeVariableResolver(Class beanClass) {
this.beanClass = beanClass; this.beanClass = beanClass;
} }


public static Type resolveVariables(Bean bean, Type type) { public static Type resolveVariables(Class beanClass, Type type) {
if (bean == null) { return new TypeVariableResolver(beanClass).resolveVariablesInType(type);
// bean is null when we're dealing with an InjectionTarget created through BeanManager.createInjectionTarget()
// we can't resolve variables since we're missing critical info, thus we simply return the original type for now
return type;
}
if (bean.getBeanClass() == null) {
throw new IllegalArgumentException("Bean " + bean + " has null beanClass!");
}
return new TypeVariableResolver(bean.getBeanClass()).resolveVariablesInType(type);
} }


public Type resolveVariablesInType(Type type) { public Type resolveVariablesInType(Type type) {
Expand Down