Skip to content

Commit

Permalink
[WELD-1999] ClassNotFoundException thrown when deserializing a bean u…
Browse files Browse the repository at this point in the history
…sing InjectionPoint

FieldInjectionPoint.SerializationProxy uses BeanManager to load target
class instead of using default ResourceLoader
  • Loading branch information
spyrkob authored and jharting committed Aug 13, 2015
1 parent 118a135 commit 292a11f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
Expand Up @@ -246,7 +246,7 @@ protected void initType() {
* Initializes the injection points
*/
protected void initInjectableFields() {
injectableFields = Beans.getFieldInjectionPoints(this, annotatedItem);
injectableFields = Beans.getFieldInjectionPoints(this, annotatedItem, beanManager);
addInjectionPoints(Beans.mergeFieldInjectionPoints(injectableFields));
}

Expand Down
Expand Up @@ -106,7 +106,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) {
super(field, declaringBean, manager, services);
this.injectionPoint = FieldInjectionPoint.of(declaringBean, null, field);
this.injectionPoint = FieldInjectionPoint.of(declaringBean, null, field, manager);
}

@Override
Expand Down
Expand Up @@ -35,7 +35,6 @@
import javax.inject.Inject;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
import org.jboss.weld.Container;
import org.jboss.weld.bean.proxy.DecoratorProxy;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.exceptions.InvalidObjectException;
Expand All @@ -45,6 +44,7 @@
import org.jboss.weld.introspector.WeldField;
import org.jboss.weld.logging.messages.ReflectionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.util.AnnotatedTypes;
Expand All @@ -62,21 +62,22 @@ public class FieldInjectionPoint<T, X> extends ForwardingWeldField<T, X> impleme
private final WeldField<T, X> field;
private final boolean delegate;
private final boolean cacheable;
private final WeldManager beanManager;
private Bean<?> cachedBean;
private Type type;


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

protected FieldInjectionPoint(Bean<?> declaringBean, WeldClass<?> injectionTargetClass, WeldField<T, X> field) {
protected FieldInjectionPoint(Bean<?> declaringBean, WeldClass<?> injectionTargetClass, WeldField<T, X> field, WeldManager manager) {
assert (declaringBean != null || injectionTargetClass != null) : "both declaringBean and injectionTargetClass are null";
this.declaringBean = declaringBean;
this.injectionTargetClass = injectionTargetClass;
this.field = field;
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.beanManager = manager;
}

@Override
Expand Down Expand Up @@ -178,6 +179,10 @@ public Member getMember() {
return getJavaMember();
}

protected WeldManager getBeanManager() {
return beanManager;
}

// Serialization

private Object writeReplace() throws ObjectStreamException {
Expand All @@ -194,11 +199,13 @@ 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);
this.fieldName = injectionPoint.getName();
this.injectionTargetClassName = injectionPoint.getInjectionTargetClass().getName();
this.beanManager = injectionPoint.getBeanManager();
}

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

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

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

Expand Down
Expand Up @@ -74,7 +74,7 @@ public SimpleInjectionTarget(WeldClass<T> type, BeanManagerImpl beanManager) {
// unless someone calls produce()
}
this.constructor = constructor;
this.injectableFields = Beans.getFieldInjectionPoints(null, type);
this.injectableFields = Beans.getFieldInjectionPoints(null, type, beanManager);
this.injectionPoints.addAll(Beans.mergeFieldInjectionPoints(this.injectableFields));
this.initializerMethods = Beans.getInitializerMethods(null, type);
this.injectionPoints.addAll(Beans.getParameterInjectionPoints(null, initializerMethods));
Expand Down
30 changes: 15 additions & 15 deletions impl/src/main/java/org/jboss/weld/util/Beans.java
Expand Up @@ -209,15 +209,15 @@ 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, BeanManagerImpl beanManager) {
if (weldClass.isModified()) {
return getFieldInjectionPointsFromWeldFields(declaringBean, weldClass);
return getFieldInjectionPointsFromWeldFields(declaringBean, weldClass, beanManager);
} else {
return getFieldInjectionPointsFromDeclaredFields(declaringBean, weldClass);
return getFieldInjectionPointsFromDeclaredFields(declaringBean, weldClass, beanManager);
}
}

private static List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPointsFromWeldFields(Bean<?> declaringBean, WeldClass<?> weldClass) {
private static List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPointsFromWeldFields(Bean<?> declaringBean, WeldClass<?> weldClass, BeanManagerImpl beanManager) {
Collection<WeldField<?, ?>> allFields = weldClass.getWeldFields(Inject.class);

List<Set<FieldInjectionPoint<?, ?>>> injectableFields = new ArrayList<Set<FieldInjectionPoint<?, ?>>>();
Expand All @@ -227,7 +227,7 @@ public static boolean isBeanProxyable(Bean<?> bean) {
for (WeldField<?, ?> field : allFields) {
Class<?> declaringClass = field.getJavaMember().getDeclaringClass();
if (declaringClass.equals(clazz)) {
addFieldInjectionPoint(declaringBean, weldClass, field, set);
addFieldInjectionPoint(declaringBean, weldClass, field, set, beanManager);
}
}
set.trimToSize();
Expand All @@ -238,13 +238,13 @@ public static boolean isBeanProxyable(Bean<?> bean) {
return injectableFields;
}

private static List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPointsFromDeclaredFields(Bean<?> declaringBean, WeldClass<?> weldClass) {
private static List<Set<FieldInjectionPoint<?, ?>>> getFieldInjectionPointsFromDeclaredFields(Bean<?> declaringBean, WeldClass<?> weldClass, BeanManagerImpl beanManager) {
List<Set<FieldInjectionPoint<?, ?>>> list = new ArrayList<Set<FieldInjectionPoint<?, ?>>>();
WeldClass<?> c = weldClass;
while (c != null && !c.getJavaClass().equals(Object.class)) {
ArraySet<FieldInjectionPoint<?, ?>> injectionPoints = new ArraySet<FieldInjectionPoint<?, ?>>();
for (WeldField<?, ?> field : c.getDeclaredWeldFields(Inject.class)) {
addFieldInjectionPoint(declaringBean, weldClass, field, injectionPoints);
addFieldInjectionPoint(declaringBean, weldClass, field, injectionPoints, beanManager);
}
injectionPoints.trimToSize();
list.add(0, injectionPoints);
Expand All @@ -254,10 +254,10 @@ public static boolean isBeanProxyable(Bean<?> bean) {
return list;
}

private static void addFieldInjectionPoint(Bean<?> declaringBean, WeldClass<?> weldClass, WeldField<?, ?> field, Set<FieldInjectionPoint<?, ?>> injectionPoints) {
private static void addFieldInjectionPoint(Bean<?> declaringBean, WeldClass<?> weldClass, WeldField<?, ?> field, Set<FieldInjectionPoint<?, ?>> injectionPoints, BeanManagerImpl beanManager) {
if (isInjectableField(field)) {
validateInjectableField(field);
injectionPoints.add(FieldInjectionPoint.of(declaringBean, weldClass, field));
injectionPoints.add(FieldInjectionPoint.of(declaringBean, weldClass, field, beanManager));
}
}

Expand Down Expand Up @@ -374,7 +374,7 @@ private static boolean isInterceptorMethod(WeldMethod<?, ?> annotatedMethod) {
public static Set<WeldInjectionPoint<?, ?>> getEjbInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(EjbInjectionServices.class)) {
Class<? extends Annotation> ejbAnnotationType = manager.getServices().get(EJBApiAbstraction.class).EJB_ANNOTATION_CLASS;
return getInjectionPoints(declaringBean, type, ejbAnnotationType);
return getInjectionPoints(declaringBean, type, ejbAnnotationType, manager);
} else {
return Collections.emptySet();
}
Expand All @@ -383,7 +383,7 @@ private static boolean isInterceptorMethod(WeldMethod<?, ?> annotatedMethod) {
public static Set<WeldInjectionPoint<?, ?>> getPersistenceContextInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(JpaInjectionServices.class)) {
Class<? extends Annotation> persistenceContextAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_CONTEXT_ANNOTATION_CLASS;
return getInjectionPoints(declaringBean, type, persistenceContextAnnotationType);
return getInjectionPoints(declaringBean, type, persistenceContextAnnotationType, manager);
} else {
return Collections.emptySet();
}
Expand All @@ -392,7 +392,7 @@ private static boolean isInterceptorMethod(WeldMethod<?, ?> annotatedMethod) {
public static Set<WeldInjectionPoint<?, ?>> getPersistenceUnitInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(JpaInjectionServices.class)) {
Class<? extends Annotation> persistenceUnitAnnotationType = manager.getServices().get(PersistenceApiAbstraction.class).PERSISTENCE_UNIT_ANNOTATION_CLASS;
return getInjectionPoints(declaringBean, type, persistenceUnitAnnotationType);
return getInjectionPoints(declaringBean, type, persistenceUnitAnnotationType, manager);
} else {
return Collections.emptySet();
}
Expand All @@ -401,16 +401,16 @@ private static boolean isInterceptorMethod(WeldMethod<?, ?> annotatedMethod) {
public static Set<WeldInjectionPoint<?, ?>> getResourceInjectionPoints(Bean<?> declaringBean, WeldClass<?> type, BeanManagerImpl manager) {
if (manager.getServices().contains(ResourceInjectionServices.class)) {
Class<? extends Annotation> resourceAnnotationType = manager.getServices().get(EJBApiAbstraction.class).RESOURCE_ANNOTATION_CLASS;
return getInjectionPoints(declaringBean, type, resourceAnnotationType);
return getInjectionPoints(declaringBean, type, resourceAnnotationType, manager);
} else {
return Collections.emptySet();
}
}

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

0 comments on commit 292a11f

Please sign in to comment.