Skip to content

Commit

Permalink
WELD-1503 Refactor metadata structures supporting bean interception -…
Browse files Browse the repository at this point in the history
… Part 2 InterceptionModel
  • Loading branch information
jharting committed Jan 27, 2014
1 parent 886d763 commit a05a732
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Set<InjectionPoint> getInjectionPoints() {
return getProducer().getInjectionPoints();
}

public InterceptionModel<?> getInterceptors() {
public InterceptionModel getInterceptors() {
if (isInterceptionCandidate()) {
return beanManager.getInterceptorModelRegistry().get(getAnnotated());
} else {
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/SessionBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void initializeAfterBeanDiscovery() {
}

protected void registerInterceptors() {
InterceptionModel<?> model = beanManager.getInterceptorModelRegistry().get(getAnnotated());
InterceptionModel model = beanManager.getInterceptorModelRegistry().get(getAnnotated());
if (model != null) {
getBeanManager().getServices().get(EjbServices.class).registerInterceptors(getEjbDescriptor(), new InterceptorBindingsAdapter(model));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import javax.enterprise.inject.spi.Interceptor;

import org.jboss.weld.ejb.spi.InterceptorBindings;
import org.jboss.weld.interceptor.spi.metadata.InterceptorFactory;
import org.jboss.weld.interceptor.spi.metadata.InterceptorClassMetadata;
import org.jboss.weld.interceptor.spi.metadata.InterceptorFactory;
import org.jboss.weld.interceptor.spi.model.InterceptionModel;
import org.jboss.weld.logging.BeanLogger;

Expand All @@ -37,9 +37,9 @@
*/
public class InterceptorBindingsAdapter implements InterceptorBindings {

private InterceptionModel<?> interceptionModel;
private InterceptionModel interceptionModel;

public InterceptorBindingsAdapter(InterceptionModel<?> interceptionModel) {
public InterceptorBindingsAdapter(InterceptionModel interceptionModel) {
if (interceptionModel == null) {
throw BeanLogger.LOG.interceptionModelNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public void registerCdiInterceptorsForMessageDrivenBeans() {
if (!getManager().getInterceptorModelRegistry().containsKey(type.slim())) {
InterceptionModelInitializer.of(getManager(), type, null).init();
}
InterceptionModel<?> model = getManager().getInterceptorModelRegistry().get(type.slim());
InterceptionModel model = getManager().getInterceptorModelRegistry().get(type.slim());
if (model != null) {
ejbServices.registerInterceptors(descriptor, new InterceptorBindingsAdapter(model));
}
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bootstrap/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private void validateCustomBean(Bean<?> bean, BeanManagerImpl beanManager) {
}

private void validateInterceptors(BeanManagerImpl beanManager, AbstractClassBean<?> classBean) {
InterceptionModel<?> interceptionModel = beanManager.getInterceptorModelRegistry().get(classBean.getAnnotated());
InterceptionModel interceptionModel = beanManager.getInterceptorModelRegistry().get(classBean.getAnnotated());
if (interceptionModel != null) {
Set<? extends InterceptorClassMetadata<?>> interceptors = interceptionModel.getAllInterceptors();
if (interceptors.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected void initializeInterceptionModel(EnhancedAnnotatedType<T> annotatedTyp
public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType) {
initializeInterceptionModel(annotatedType);

InterceptionModel<?> interceptionModel = null;
InterceptionModel interceptionModel = null;
if (isInterceptionCandidate()) {
interceptionModel = beanManager.getInterceptorModelRegistry().get(getType());
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType)
}
}

protected void setupConstructorInterceptionInstantiator(InterceptionModel<?> interceptionModel) {
protected void setupConstructorInterceptionInstantiator(InterceptionModel interceptionModel) {
if (interceptionModel != null && interceptionModel.hasExternalConstructorInterceptors()) {
setInstantiator(new ConstructorInterceptionInstantiator<T>(getInstantiator(), interceptionModel, getType()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
*/
public class ConstructorInterceptionInstantiator<T> extends ForwardingInstantiator<T> {

private final InterceptionModel<?> model;
private final InterceptionModel model;
private final SlimAnnotatedType<?> annotatedType;

public ConstructorInterceptionInstantiator(Instantiator<T> delegate, InterceptionModel<?> model, SlimAnnotatedType<?> type) {
public ConstructorInterceptionInstantiator(Instantiator<T> delegate, InterceptionModel model, SlimAnnotatedType<?> type) {
super(delegate);
this.model = model;
this.annotatedType = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import static org.jboss.weld.util.Interceptors.filterInterceptorBindings;
import static org.jboss.weld.util.Interceptors.flattenInterceptorBindings;
import static org.jboss.weld.util.Interceptors.mergeBeanInterceptorBindings;
import static org.jboss.weld.util.reflection.Reflections.cast;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -44,16 +42,18 @@
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.interceptor.builder.InterceptionModelBuilder;
import org.jboss.weld.interceptor.builder.InterceptorsApiAbstraction;
import org.jboss.weld.interceptor.reader.InterceptorMetadataReader;
import org.jboss.weld.interceptor.reader.TargetClassInterceptorMetadata;
import org.jboss.weld.interceptor.spi.metadata.InterceptorClassMetadata;
import org.jboss.weld.interceptor.spi.model.InterceptionModel;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.logging.ValidatorLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.serialization.spi.helpers.SerializableContextual;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Reflections;

import com.google.common.collect.Lists;

/**
* Initializes {@link InterceptionModel} for a {@link Bean} or a non-contextual component.
*
Expand All @@ -64,17 +64,12 @@
*/
public class InterceptionModelInitializer<T> {

private static final InterceptorClassMetadata<?>[] EMPTY_INTERCEPTOR_METADATA_ARRAY = new InterceptorClassMetadata[0];

private static <T> InterceptorClassMetadata<T>[] emptyInterceptorMetadataArray() {
return cast(EMPTY_INTERCEPTOR_METADATA_ARRAY);
}

public static <T> InterceptionModelInitializer<T> of(BeanManagerImpl manager, EnhancedAnnotatedType<T> annotatedType, Bean<?> bean) {
return new InterceptionModelInitializer<T>(manager, annotatedType, Beans.getBeanConstructorStrict(annotatedType), bean);
}

private final BeanManagerImpl manager;
private final InterceptorMetadataReader reader;
private final EnhancedAnnotatedType<T> annotatedType;
private final Set<Class<? extends Annotation>> stereotypes;
private final AnnotatedConstructor<T> constructor;
Expand All @@ -83,14 +78,15 @@ public static <T> InterceptionModelInitializer<T> of(BeanManagerImpl manager, En
private final EJBApiAbstraction ejbApi;

private List<AnnotatedMethod<?>> businessMethods;
private final InterceptionModelBuilder<?> builder;
private final InterceptionModelBuilder builder;
private boolean hasSerializationOrInvocationInterceptorMethods;

public InterceptionModelInitializer(BeanManagerImpl manager, EnhancedAnnotatedType<T> annotatedType, AnnotatedConstructor<T> constructor, Bean<?> bean) {
this.constructor = constructor;
this.manager = manager;
this.reader = manager.getInterceptorMetadataReader();
this.annotatedType = annotatedType;
this.builder = InterceptionModelBuilder.newBuilderFor(null); // TODO fixme
this.builder = new InterceptionModelBuilder();
if (bean == null) {
stereotypes = Collections.emptySet();
} else {
Expand All @@ -107,7 +103,7 @@ public void init() {
initEjbInterceptors();
initCdiInterceptors();

InterceptionModel<?> interceptionModel = builder.build();
InterceptionModel interceptionModel = builder.build();
if (interceptionModel.getAllInterceptors().size() > 0 || hasSerializationOrInvocationInterceptorMethods) {
if (annotatedType.isFinal()) {
throw BeanLogger.LOG.finalBeanClassWithInterceptorsNotAllowed(annotatedType.getJavaClass());
Expand All @@ -121,7 +117,7 @@ public void init() {

private void initTargetClassInterceptors() {
if (!Beans.isInterceptor(annotatedType)) {
TargetClassInterceptorMetadata interceptorClassMetadata = manager.getInterceptorMetadataReader().getTargetClassInterceptorMetadata(annotatedType);
TargetClassInterceptorMetadata interceptorClassMetadata = reader.getTargetClassInterceptorMetadata(annotatedType);
builder.setTargetClassInterceptorMetadata(interceptorClassMetadata);
hasSerializationOrInvocationInterceptorMethods = interceptorClassMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.AROUND_INVOKE)
|| interceptorClassMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.AROUND_TIMEOUT)
Expand All @@ -132,7 +128,6 @@ private void initTargetClassInterceptors() {
// target class
hasSerializationOrInvocationInterceptorMethods = false;
}
builder.setHasTargetClassInterceptors(hasSerializationOrInvocationInterceptorMethods);
}

private void initCdiInterceptors() {
Expand Down Expand Up @@ -163,7 +158,7 @@ private void initCdiLifecycleInterceptors(Map<Class<? extends Annotation>, Annot
private void initLifeCycleInterceptor(InterceptionType interceptionType, Map<Class<? extends Annotation>, Annotation> classBindingAnnotations) {
List<Interceptor<?>> resolvedInterceptors = manager.resolveInterceptors(interceptionType, classBindingAnnotations.values());
if (!resolvedInterceptors.isEmpty()) {
builder.intercept(interceptionType).with(toSerializableContextualArray(resolvedInterceptors));
builder.intercept(interceptionType, asInterceptorMetadata(resolvedInterceptors));
}
}

Expand Down Expand Up @@ -191,8 +186,8 @@ private void initInterceptor(InterceptionType interceptionType, AnnotatedMethod<
if (Reflections.isFinal(method.getJavaMember())) {
throw BeanLogger.LOG.finalInterceptedBeanMethodNotAllowed(method, methodBoundInterceptors.get(0).getBeanClass().getName());
}
Method javaMethod = Reflections.<AnnotatedMethod<T>>cast(method).getJavaMember();
builder.intercept(interceptionType, javaMethod).with(toSerializableContextualArray(methodBoundInterceptors));
Method javaMethod = method.getJavaMember();
builder.intercept(interceptionType, javaMethod, asInterceptorMetadata(methodBoundInterceptors));
}
}

Expand All @@ -207,7 +202,7 @@ private void initCdiConstructorInterceptors(Map<Class<? extends Annotation>, Ann
}
List<Interceptor<?>> constructorBoundInterceptors = manager.resolveInterceptors(InterceptionType.AROUND_CONSTRUCT, constructorBindings);
if (!constructorBoundInterceptors.isEmpty()) {
builder.intercept(InterceptionType.AROUND_CONSTRUCT).with(toSerializableContextualArray(constructorBoundInterceptors));
builder.intercept(InterceptionType.AROUND_CONSTRUCT, asInterceptorMetadata(constructorBoundInterceptors));
}
}

Expand Down Expand Up @@ -237,7 +232,7 @@ private void initClassDeclaredEjbInterceptors() {

if (classDeclaredInterceptors != null) {
for (Class<?> clazz : classDeclaredInterceptors) {
InterceptorClassMetadata<?> interceptor = manager.getInterceptorMetadataReader().getPlainInterceptorMetadata(clazz);
InterceptorClassMetadata<?> interceptor = reader.getPlainInterceptorMetadata(clazz);
for (InterceptionType interceptionType : InterceptionType.values()) {
if (excludeClassLevelAroundConstructInterceptors && interceptionType.equals(InterceptionType.AROUND_CONSTRUCT)) {
/*
Expand All @@ -246,7 +241,7 @@ private void initClassDeclaredEjbInterceptors() {
continue;
}
if (interceptor.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType))) {
builder.intercept(interceptionType).with(interceptor);
builder.intercept(interceptionType, Collections.<InterceptorClassMetadata<?>>singleton(interceptor));
}
}
}
Expand All @@ -260,7 +255,7 @@ public void initConstructorDeclaredEjbInterceptors() {
Class<?>[] constructorDeclaredInterceptors = interceptorsApi.extractInterceptorClasses(constructor);
if (constructorDeclaredInterceptors != null) {
for (Class<?> clazz : constructorDeclaredInterceptors) {
builder.intercept(InterceptionType.AROUND_CONSTRUCT).with(manager.getInterceptorMetadataReader().getPlainInterceptorMetadata(clazz));
builder.intercept(InterceptionType.AROUND_CONSTRUCT, Collections.<InterceptorClassMetadata<?>>singleton(reader.getPlainInterceptorMetadata(clazz)));
}
}
}
Expand All @@ -282,35 +277,22 @@ private void initMethodDeclaredEjbInterceptors(AnnotatedMethod<?> method) {
InterceptionType interceptionType = isTimeoutAnnotationPresentOn(method)
? InterceptionType.AROUND_TIMEOUT
: InterceptionType.AROUND_INVOKE;
InterceptorClassMetadata<?>[] interceptors = getMethodDeclaredInterceptorMetadatas(methodDeclaredInterceptors);
builder.intercept(interceptionType, javaMethod).with(interceptors);
builder.intercept(interceptionType, javaMethod, getMethodDeclaredInterceptorMetadatas(methodDeclaredInterceptors));
}
}

private InterceptorClassMetadata<?>[] getMethodDeclaredInterceptorMetadatas(Class<?>[] methodDeclaredInterceptors) {
List<InterceptorClassMetadata<?>> list = new ArrayList<InterceptorClassMetadata<?>>();
private List<InterceptorClassMetadata<?>> getMethodDeclaredInterceptorMetadatas(Class<?>[] methodDeclaredInterceptors) {
List<InterceptorClassMetadata<?>> list = Lists.newLinkedList();
for (Class<?> clazz : methodDeclaredInterceptors) {
list.add(manager.getInterceptorMetadataReader().getPlainInterceptorMetadata(clazz));
list.add(reader.getPlainInterceptorMetadata(clazz));
}
return list.toArray(new InterceptorClassMetadata[list.size()]);
return list;
}

private boolean isTimeoutAnnotationPresentOn(AnnotatedMethod<?> method) {
return method.isAnnotationPresent(ejbApi.TIMEOUT_ANNOTATION_CLASS);
}

private InterceptorClassMetadata<?>[] toSerializableContextualArray(List<Interceptor<?>> interceptors) {
List<InterceptorClassMetadata<?>> serializableContextuals = new ArrayList<InterceptorClassMetadata<?>>();
for (Interceptor<?> interceptor : interceptors) {
serializableContextuals.add(getInterceptorMetadata(interceptor));
}
return serializableContextuals.toArray(InterceptionModelInitializer.<SerializableContextual<?, ?>>emptyInterceptorMetadataArray());
}

private <X> InterceptorClassMetadata<X> getInterceptorMetadata(Interceptor<X> interceptor) {
return manager.getInterceptorMetadataReader().getCdiInterceptorMetadata(interceptor);
}

/**
* Merges bean interceptor bindings (including inherited ones) with method interceptor bindings. Method interceptor bindings
* override bean interceptor bindings. The bean binding map is not modified - a copy is used.
Expand All @@ -333,4 +315,8 @@ protected Map<Class<? extends Annotation>, Annotation> mergeMethodInterceptorBin
}
return mergedBeanBindings;
}

private List<InterceptorClassMetadata<?>> asInterceptorMetadata(List<Interceptor<?>> interceptors) {
return Lists.transform(interceptors, reader.getInterceptorToInterceptorMetadataFunction());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
*/
public class InterceptorApplyingInstantiator<T> extends ForwardingInstantiator<T> {

private final InterceptionModel<?> interceptionModel;
private final InterceptionModel interceptionModel;
private final SlimAnnotatedType<T> annotatedType;

public InterceptorApplyingInstantiator(Instantiator<T> delegate, InterceptionModel<?> model, SlimAnnotatedType<T> type) {
public InterceptorApplyingInstantiator(Instantiator<T> delegate, InterceptionModel model, SlimAnnotatedType<T> type) {
super(delegate);
this.interceptionModel = model;
this.annotatedType = type;
Expand Down
Loading

0 comments on commit a05a732

Please sign in to comment.