Skip to content

Commit

Permalink
[WELD-1999] Moving the BeanManager to WeldInjectionPointSerialization…
Browse files Browse the repository at this point in the history
…Proxy

Applying the fix to Constructor, Parameter and Method InjectionPoint
classes.
  • Loading branch information
spyrkob authored and jharting committed Aug 13, 2015
1 parent 292a11f commit a453100
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 60 deletions.
8 changes: 4 additions & 4 deletions impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
Expand Up @@ -254,8 +254,8 @@ protected void initInjectableFields() {
* Initializes the initializer methods
*/
protected void initInitializerMethods() {
initializerMethods = Beans.getInitializerMethods(this, getWeldAnnotated());
addInjectionPoints(Beans.getParameterInjectionPoints(this, initializerMethods));
initializerMethods = Beans.getInitializerMethods(this, getWeldAnnotated(), beanManager);
addInjectionPoints(Beans.getParameterInjectionPoints(this, initializerMethods, beanManager));
}

@Override
Expand Down Expand Up @@ -439,8 +439,8 @@ protected void checkConstructor() {
* Initializes the constructor
*/
protected void initConstructor() {
this.constructor = Beans.getBeanConstructor(this, getWeldAnnotated());
addInjectionPoints(Beans.getParameterInjectionPoints(this, constructor));
this.constructor = Beans.getBeanConstructor(this, getWeldAnnotated(), beanManager);
addInjectionPoints(Beans.getParameterInjectionPoints(this, constructor, beanManager));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions impl/src/main/java/org/jboss/weld/bean/DisposalMethod.java
Expand Up @@ -56,12 +56,12 @@ public static <X, T> DisposalMethod<X, T> of(BeanManagerImpl manager, WeldMethod

protected DisposalMethod(BeanManagerImpl beanManager, WeldMethod<T, ? super X> disposalMethod, AbstractClassBean<X> declaringBean, ServiceRegistry services) {
super(new StringBuilder().append(DisposalMethod.class.getSimpleName()).append(BEAN_ID_SEPARATOR).append(declaringBean.getWeldAnnotated().getName()).append(disposalMethod.getSignature().toString()).toString(), declaringBean, beanManager, services);
this.disposalMethodInjectionPoint = MethodInjectionPoint.of(this, disposalMethod);
this.disposalMethodInjectionPoint = MethodInjectionPoint.of(this, disposalMethod, beanManager);
initQualifiers();
initType();
initTypes();
initStereotypes();
addInjectionPoints(Beans.getParameterInjectionPoints(this, disposalMethodInjectionPoint));
addInjectionPoints(Beans.getParameterInjectionPoints(this, disposalMethodInjectionPoint, beanManager));
}

private void initDisposesParameter() {
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Expand Up @@ -339,7 +339,7 @@ protected T createInstance(CreationalContext<T> ctx) {
if (!isSubclassed()) {
return getConstructor().newInstance(beanManager, ctx);
} else {
ProxyClassConstructorInjectionPointWrapper<T> constructorInjectionPointWrapper = new ProxyClassConstructorInjectionPointWrapper<T>(this, constructorForEnhancedSubclass, getConstructor());
ProxyClassConstructorInjectionPointWrapper<T> constructorInjectionPointWrapper = new ProxyClassConstructorInjectionPointWrapper<T>(this, constructorForEnhancedSubclass, getConstructor(), beanManager);
return constructorInjectionPointWrapper.newInstance(beanManager, ctx);
}
}
Expand Down
4 changes: 2 additions & 2 deletions impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
Expand Up @@ -75,7 +75,7 @@ public static <X, T> ProducerMethod<X, T> of(WeldMethod<T, ? super X> method, Ab

protected ProducerMethod(WeldMethod<T, ? super X> method, AbstractClassBean<X> declaringBean, BeanManagerImpl beanManager, ServiceRegistry services) {
super(new StringBuilder().append(ProducerMethod.class.getSimpleName()).append(BEAN_ID_SEPARATOR).append(declaringBean.getWeldAnnotated().getName()).append(".").append(method.getSignature().toString()).toString(), declaringBean, beanManager, services);
this.method = MethodInjectionPoint.of(this, method);
this.method = MethodInjectionPoint.of(this, method, beanManager);
initType();
initTypes();
initQualifiers();
Expand Down Expand Up @@ -150,7 +150,7 @@ public String toString() {
*/
protected void initProducerMethodInjectableParameters() {
for (WeldParameter<?, ?> parameter : method.getWeldParameters()) {
addInjectionPoint(ParameterInjectionPoint.of(this, parameter));
addInjectionPoint(ParameterInjectionPoint.of(this, parameter, beanManager));
}
}

Expand Down
Expand Up @@ -99,7 +99,7 @@ public class ObserverMethodImpl<T, X> implements ObserverMethod<T> {
protected ObserverMethodImpl(final WeldMethod<T, ? super X> observer, final RIBean<X> declaringBean, final BeanManagerImpl manager) {
this.beanManager = manager;
this.declaringBean = declaringBean;
this.observerMethod = MethodInjectionPoint.of(declaringBean, observer);
this.observerMethod = MethodInjectionPoint.of(declaringBean, observer, beanManager);

WeldParameter<?, ? super X> eventArgument = observerMethod.getAnnotatedParameters(Observes.class).get(0);
this.eventType = TypeVariableResolver.resolveVariables(declaringBean.getBeanClass(), eventArgument.getBaseType());
Expand All @@ -110,7 +110,7 @@ protected ObserverMethodImpl(final WeldMethod<T, ? super X> observer, final RIBe

this.injectionPoints = new HashSet<WeldInjectionPoint<?, ?>>();
this.newInjectionPoints = new HashSet<WeldInjectionPoint<?, ?>>();
for (WeldInjectionPoint<?, ?> injectionPoint : Beans.getParameterInjectionPoints(null, observerMethod)) {
for (WeldInjectionPoint<?, ?> injectionPoint : Beans.getParameterInjectionPoints(null, observerMethod, beanManager)) {
if (injectionPoint.isAnnotationPresent(Observes.class) == false) {
if (injectionPoint.isAnnotationPresent(New.class)) {
this.newInjectionPoints.add(injectionPoint);
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.jboss.weld.introspector.WeldParameter;
import org.jboss.weld.logging.messages.ReflectionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.util.AnnotatedTypes;

import javax.enterprise.context.spi.CreationalContext;
Expand Down Expand Up @@ -57,9 +58,11 @@ private abstract static class ForwardingParameterInjectionPointList<T, X> extend

protected abstract Bean<X> declaringBean();

protected abstract WeldManager beanManager();

@Override
public ParameterInjectionPoint<T, X> get(int index) {
return ParameterInjectionPoint.of(declaringBean(), delegate().get(index));
return ParameterInjectionPoint.of(declaringBean(), delegate().get(index), beanManager());
}

@Override
Expand All @@ -71,15 +74,19 @@ public int size() {

@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<T> declaringBean;

private final WeldConstructor<T> constructor;

public static <T> ConstructorInjectionPoint<T> of(Bean<T> declaringBean, WeldConstructor<T> constructor) {
return new ConstructorInjectionPoint<T>(declaringBean, constructor);
private final WeldManager beanManager;

public static <T> ConstructorInjectionPoint<T> of(Bean<T> declaringBean, WeldConstructor<T> constructor, WeldManager beanManager) {
return new ConstructorInjectionPoint<T>(declaringBean, constructor, beanManager);
}

protected ConstructorInjectionPoint(Bean<T> declaringBean, WeldConstructor<T> constructor) {
protected ConstructorInjectionPoint(Bean<T> declaringBean, WeldConstructor<T> constructor, WeldManager beanManager) {
this.declaringBean = declaringBean;
this.constructor = constructor;
this.beanManager = beanManager;
}

@Override
Expand Down Expand Up @@ -107,6 +114,10 @@ public Bean<?> getBean() {
return declaringBean;
}

protected WeldManager getBeanManager() {
return beanManager;
}

@Override
public Set<Annotation> getQualifiers() {
return delegate().getQualifiers();
Expand Down Expand Up @@ -142,6 +153,10 @@ protected List<? extends WeldParameter<?, T>> delegate() {
return delegate;
}

@Override
protected WeldManager beanManager() {
return beanManager;
}
};
}

Expand Down Expand Up @@ -211,7 +226,7 @@ private static class SerializationProxy<T> extends WeldInjectionPointSerializati
private final ConstructorSignature signature;

public SerializationProxy(ConstructorInjectionPoint<T> injectionPoint) {
super(injectionPoint);
super(injectionPoint, injectionPoint.getBeanManager());
this.signature = injectionPoint.getSignature();
}

Expand All @@ -221,7 +236,7 @@ private Object readResolve() {
if (constructor == null || (bean == null && getDeclaringBeanId() != null)) {
throw new IllegalStateException(ReflectionMessage.UNABLE_TO_GET_CONSTRUCTOR_ON_DESERIALIZATION, getDeclaringBeanId(), getDeclaringWeldClass(), signature);
}
return ConstructorInjectionPoint.of(getDeclaringBean(), getWeldConstructor());
return ConstructorInjectionPoint.of(getDeclaringBean(), getWeldConstructor(), getBeanManager());
}

protected WeldConstructor<T> getWeldConstructor() {
Expand Down
Expand Up @@ -199,13 +199,11 @@ private static class SerializationProxy<T> extends WeldInjectionPointSerializati

private final String fieldName;
private final String injectionTargetClassName;
private final WeldManager beanManager;

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

private Object readResolve() {
Expand All @@ -214,11 +212,11 @@ private Object readResolve() {
throw new IllegalStateException(ReflectionMessage.UNABLE_TO_GET_FIELD_ON_DESERIALIZATION, getDeclaringBeanId(), getDeclaringWeldClass(), fieldName);
}

return FieldInjectionPoint.of(getDeclaringBean(), getInjectionTargetClass(), getWeldField(), beanManager);
return FieldInjectionPoint.of(getDeclaringBean(), getInjectionTargetClass(), getWeldField(), getBeanManager());
}

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

Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.jboss.weld.introspector.WeldParameter;
import org.jboss.weld.logging.messages.ReflectionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.util.AnnotatedTypes;

import javax.enterprise.context.spi.CreationalContext;
Expand Down Expand Up @@ -52,9 +53,11 @@ private abstract static class ForwardingParameterInjectionPointList<T, X> extend

protected abstract Bean<X> declaringBean();

protected abstract WeldManager beanManager();

@Override
public ParameterInjectionPoint<T, X> get(int index) {
return ParameterInjectionPoint.of(declaringBean(), delegate().get(index));
return ParameterInjectionPoint.of(declaringBean(), delegate().get(index), null);
}

@Override
Expand All @@ -66,14 +69,16 @@ public int size() {

private final Bean<?> declaringBean;
private final WeldMethod<T, X> method;
private final WeldManager beanManager;

public static <T, X> MethodInjectionPoint<T, X> of(Bean<?> declaringBean, WeldMethod<T, X> method) {
return new MethodInjectionPoint<T, X>(declaringBean, method);
public static <T, X> MethodInjectionPoint<T, X> of(Bean<?> declaringBean, WeldMethod<T, X> method, WeldManager beanManager) {
return new MethodInjectionPoint<T, X>(declaringBean, method, beanManager);
}

protected MethodInjectionPoint(Bean<?> declaringBean, WeldMethod<T, X> method) {
protected MethodInjectionPoint(Bean<?> declaringBean, WeldMethod<T, X> method, WeldManager beanManager) {
this.declaringBean = declaringBean;
this.method = method;
this.beanManager = beanManager;
}

@Override
Expand Down Expand Up @@ -101,6 +106,10 @@ public Bean<?> getBean() {
return declaringBean;
}

protected WeldManager getBeanManager() {
return beanManager;
}

@Override
public Set<Annotation> getQualifiers() {
return delegate().getQualifiers();
Expand Down Expand Up @@ -181,6 +190,10 @@ protected List<? extends WeldParameter<?, X>> delegate() {
return delegate;
}

@Override
protected WeldManager beanManager() {
return beanManager;
}
};
}

Expand Down Expand Up @@ -259,7 +272,7 @@ private static class SerializationProxy<T> extends WeldInjectionPointSerializati
private final MethodSignature signature;

public SerializationProxy(MethodInjectionPoint<T, ?> injectionPoint) {
super(injectionPoint);
super(injectionPoint, injectionPoint.getBeanManager());
this.signature = injectionPoint.getSignature();
}

Expand All @@ -269,7 +282,7 @@ private Object readResolve() {
if (method == null || (bean == null && getDeclaringBeanId() != null)) {
throw new IllegalStateException(ReflectionMessage.UNABLE_TO_GET_METHOD_ON_DESERIALIZATION, getDeclaringBeanId(), getDeclaringWeldClass(), signature);
}
return MethodInjectionPoint.of(getDeclaringBean(), getWeldMethod());
return MethodInjectionPoint.of(getDeclaringBean(), getWeldMethod(), getBeanManager());
}

protected WeldMethod<T, ?> getWeldMethod() {
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.jboss.weld.introspector.WeldParameter;
import org.jboss.weld.logging.messages.ReflectionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.util.reflection.Reflections;

import javax.decorator.Delegate;
Expand All @@ -53,22 +54,24 @@

public class ParameterInjectionPoint<T, X> extends ForwardingWeldParameter<T, X> implements WeldInjectionPoint<T, Object>, Serializable {

public static <T, X> ParameterInjectionPoint<T, X> of(Bean<?> declaringBean, WeldParameter<T, X> parameter) {
return new ParameterInjectionPoint<T, X>(declaringBean, parameter);
public static <T, X> ParameterInjectionPoint<T, X> of(Bean<?> declaringBean, WeldParameter<T, X> parameter, WeldManager beanManager) {
return new ParameterInjectionPoint<T, X>(declaringBean, parameter, beanManager);
}

@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 WeldParameter<T, X> parameter;
private final boolean delegate;
private final boolean cacheable;
private final WeldManager beanManager;
private Bean<?> cachedBean;

private ParameterInjectionPoint(Bean<?> declaringBean, WeldParameter<T, X> parameter) {
private ParameterInjectionPoint(Bean<?> declaringBean, WeldParameter<T, X> parameter, WeldManager beanManager) {
this.declaringBean = declaringBean;
this.parameter = parameter;
this.delegate = isAnnotationPresent(Delegate.class) && declaringBean instanceof Decorator<?>;
this.cacheable = !delegate && !InjectionPoint.class.isAssignableFrom(parameter.getJavaClass()) && !Instance.class.isAssignableFrom(parameter.getJavaClass());
this.beanManager = beanManager;
}

@Override
Expand Down Expand Up @@ -153,6 +156,10 @@ private void readObject(ObjectInputStream stream) throws InvalidObjectException
throw new InvalidObjectException(PROXY_REQUIRED);
}

private WeldManager getBeanManager() {
return beanManager;
}

private static class SerializationProxy<T> extends WeldInjectionPointSerializationProxy<T, Object> {

private static final long serialVersionUID = -3491482804822264969L;
Expand All @@ -162,7 +169,7 @@ private static class SerializationProxy<T> extends WeldInjectionPointSerializati
private final ConstructorSignature constructorSignature;

public SerializationProxy(ParameterInjectionPoint<T, ?> injectionPoint) {
super(injectionPoint);
super(injectionPoint, injectionPoint.getBeanManager());
this.parameterPosition = injectionPoint.getPosition();
if (injectionPoint.delegate().getDeclaringWeldCallable() instanceof WeldMethod<?, ?>) {
this.methodSignature = ((WeldMethod<?, ?>) injectionPoint.delegate().getDeclaringWeldCallable()).getSignature();
Expand All @@ -181,7 +188,7 @@ private Object readResolve() {
if (parameter == null || (bean == null && getDeclaringBeanId() != null)) {
throw new IllegalStateException(ReflectionMessage.UNABLE_TO_GET_PARAMETER_ON_DESERIALIZATION, getDeclaringBeanId(), getDeclaringWeldClass(), methodSignature, parameterPosition);
}
return ParameterInjectionPoint.of(getDeclaringBean(), getWeldParameter());
return ParameterInjectionPoint.of(getDeclaringBean(), getWeldParameter(), getBeanManager());
}

protected WeldParameter<T, ?> getWeldParameter() {
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.jboss.weld.bean.proxy.TargetBeanInstance;
import org.jboss.weld.introspector.WeldConstructor;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.WeldManager;

/**
* A wrapper on a {@link ConstructorInjectionPoint}, to be used if a proxy subclass is instantiated instead of the
Expand All @@ -50,8 +51,8 @@ public class ProxyClassConstructorInjectionPointWrapper<T> extends ConstructorIn
private boolean decorator;
private final Bean<?> bean;

public ProxyClassConstructorInjectionPointWrapper(Bean<T> declaringBean, WeldConstructor<T> weldConstructor, ConstructorInjectionPoint<T> originalConstructorInjectionPoint) {
super(declaringBean, weldConstructor);
public ProxyClassConstructorInjectionPointWrapper(Bean<T> declaringBean, WeldConstructor<T> weldConstructor, ConstructorInjectionPoint<T> originalConstructorInjectionPoint, WeldManager beanManager) {
super(declaringBean, weldConstructor, beanManager);
this.decorator = (declaringBean instanceof javax.enterprise.inject.spi.Decorator);
this.originalConstructorInjectionPoint = originalConstructorInjectionPoint;
this.bean = declaringBean;
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.introspector.WeldAnnotated;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.serialization.spi.ContextualStore;

Expand All @@ -35,11 +36,13 @@ abstract static class WeldInjectionPointSerializationProxy<T, S> implements Seri

private final String declaringBeanId;
private final Class<?> declaringClass;
private final WeldManager beanManager;

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

protected Bean<T> getDeclaringBean() {
Expand All @@ -54,8 +57,12 @@ protected String getDeclaringBeanId() {
return declaringBeanId;
}

protected WeldManager getBeanManager() {
return beanManager;
}

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

}
Expand Down

0 comments on commit a453100

Please sign in to comment.