}
interceptorWithLifecycleCallbacks.addAll(description.getClassInterceptors());
for (final InterceptorDescription interceptorDescription : description.getAllInterceptors()) {
final String interceptorClassName = interceptorDescription.getInterceptorClassName();
final ClassIndex interceptorClass;
try {
interceptorClass = classIndex.classIndex(interceptorClassName);
} catch (ClassNotFoundException e) {
throw MESSAGES.cannotLoadInterceptor(e, interceptorClassName);
}
//we store the interceptor instance under the class key
final Object contextKey = interceptorClass.getModuleClass();
final ClassReflectionIndex<?> interceptorIndex = deploymentReflectionIndex.getClassIndex(interceptorClass.getModuleClass());
final Constructor<?> constructor = interceptorIndex.getConstructor(EMPTY_CLASS_ARRAY);
if (constructor == null) {
throw MESSAGES.defaultConstructorNotFoundOnComponent(interceptorClassName, configuration.getComponentClass());
}
instantiators.addFirst(new ManagedReferenceInterceptorFactory(new ValueManagedReferenceFactory(new ConstructedValue(constructor, Collections.<Value<?>>emptyList())), contextKey));
destructors.addLast(new ManagedReferenceReleaseInterceptorFactory(contextKey));
final boolean interceptorHasLifecycleCallbacks = interceptorWithLifecycleCallbacks.contains(interceptorDescription);
new ClassDescriptionTraversal(interceptorClass.getModuleClass(), applicationClasses) {
@Override
public void handle(final Class<?> clazz, EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
mergeInjectionsForClass(clazz, classDescription, moduleDescription, description, configuration, context, injectors, contextKey, uninjectors, metadataComplete);
final InterceptorClassDescription interceptorConfig;
if (classDescription != null && !metadataComplete) {
interceptorConfig = InterceptorClassDescription.merge(classDescription.getInterceptorClassDescription(), moduleDescription.getInterceptorClassOverride(clazz.getName()));
} else {
interceptorConfig = InterceptorClassDescription.merge(null, moduleDescription.getInterceptorClassOverride(clazz.getName()));
}
// Only class level interceptors are processed for postconstruct/predestroy methods.
// Method level interceptors aren't supposed to be processed for postconstruct/predestroy lifecycle
// methods, as per interceptors spec
if (interceptorHasLifecycleCallbacks) {
final MethodIdentifier postConstructMethodIdentifier = interceptorConfig.getPostConstruct();
if (postConstructMethodIdentifier != null) {
final Method method = ClassReflectionIndexUtil.findRequiredMethod(deploymentReflectionIndex, clazz, postConstructMethodIdentifier);
if (isNotOverriden(clazz, method, interceptorClass.getModuleClass(), deploymentReflectionIndex)) {
InterceptorFactory interceptorFactory = new ManagedReferenceLifecycleMethodInterceptorFactory(contextKey, method, true, true);
List<InterceptorFactory> userPostConstruct = userPostConstructByInterceptorClass.get(interceptorClassName);
if (userPostConstruct == null) {
userPostConstructByInterceptorClass.put(interceptorClassName, userPostConstruct = new ArrayList<InterceptorFactory>());
}
userPostConstruct.add(interceptorFactory);
}
}
final MethodIdentifier preDestroyMethodIdentifier = interceptorConfig.getPreDestroy();
if (preDestroyMethodIdentifier != null) {
final Method method = ClassReflectionIndexUtil.findRequiredMethod(deploymentReflectionIndex, clazz, preDestroyMethodIdentifier);
if (isNotOverriden(clazz, method, interceptorClass.getModuleClass(), deploymentReflectionIndex)) {
InterceptorFactory interceptorFactory = new ManagedReferenceLifecycleMethodInterceptorFactory(contextKey, method, true, true);
List<InterceptorFactory> userPreDestroy = userPreDestroyByInterceptorClass.get(interceptorClassName);
if (userPreDestroy == null) {
userPreDestroyByInterceptorClass.put(interceptorClassName, userPreDestroy = new ArrayList<InterceptorFactory>());
}
userPreDestroy.add(interceptorFactory);
}
}
}
final MethodIdentifier aroundInvokeMethodIdentifier = interceptorConfig.getAroundInvoke();
if (aroundInvokeMethodIdentifier != null) {
final Method method = ClassReflectionIndexUtil.findRequiredMethod(deploymentReflectionIndex, clazz, aroundInvokeMethodIdentifier);
if (isNotOverriden(clazz, method, interceptorClass.getModuleClass(), deploymentReflectionIndex)) {
List<InterceptorFactory> interceptors;
if ((interceptors = userAroundInvokesByInterceptorClass.get(interceptorClassName)) == null) {
userAroundInvokesByInterceptorClass.put(interceptorClassName, interceptors = new ArrayList<InterceptorFactory>());
}
interceptors.add(new ManagedReferenceLifecycleMethodInterceptorFactory(contextKey, method, false));
}
}
if (description.isTimerServiceApplicable()) {
final MethodIdentifier aroundTimeoutMethodIdentifier = interceptorConfig.getAroundTimeout();
if (aroundTimeoutMethodIdentifier != null) {
final Method method = ClassReflectionIndexUtil.findRequiredMethod(deploymentReflectionIndex, clazz, aroundTimeoutMethodIdentifier);
if (isNotOverriden(clazz, method, interceptorClass.getModuleClass(), deploymentReflectionIndex)) {
List<InterceptorFactory> interceptors;
if ((interceptors = userAroundTimeoutsByInterceptorClass.get(interceptorClassName)) == null) {
userAroundTimeoutsByInterceptorClass.put(interceptorClassName, interceptors = new ArrayList<InterceptorFactory>());
}
interceptors.add(new ManagedReferenceLifecycleMethodInterceptorFactory(contextKey, method, false));
}
}
}
}
}.run();
}
final Deque<InterceptorFactory> userPostConstruct = new ArrayDeque<InterceptorFactory>();
final Deque<InterceptorFactory> userPreDestroy = new ArrayDeque<InterceptorFactory>();
//now add the lifecycle interceptors in the correct order
for (final InterceptorDescription interceptorClass : interceptorWithLifecycleCallbacks) {
if (userPostConstructByInterceptorClass.containsKey(interceptorClass.getInterceptorClassName())) {
userPostConstruct.addAll(userPostConstructByInterceptorClass.get(interceptorClass.getInterceptorClassName()));
}
if (userPreDestroyByInterceptorClass.containsKey(interceptorClass.getInterceptorClassName())) {
userPreDestroy.addAll(userPreDestroyByInterceptorClass.get(interceptorClass.getInterceptorClassName()));
}
}
new ClassDescriptionTraversal(configuration.getComponentClass(), applicationClasses) {