private static boolean isValidBusinessMethodInterceptorMethod(InterceptionType interceptionType, Method method) { if (!Object.class.equals(method.getReturnType())) { throw ValidatorLogger.LOG.interceptorMethodDoesNotReturnObject(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), OBJECT_CLASS_NAME, Formats.formatAsStackTraceElement(method)); } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveExactlyOneParameter(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Formats.formatAsStackTraceElement(method)); } if (!InvocationContext.class.isAssignableFrom(parameterTypes[0])) { throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveCorrectTypeOfParameter(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), InvocationContext.class.getName(), Formats.formatAsStackTraceElement(method)); } return true; }
@Override public List<Interceptor<?>> getLifecycleInterceptors(InterceptionType interceptionType) { if (interceptionType == null) { throw BeanLogger.LOG.interceptionTypeNull(); } org.jboss.weld.interceptor.spi.model.InterceptionType internalInterceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType.name()); if (!internalInterceptionType.isLifecycleCallback()) { throw BeanLogger.LOG.interceptionTypeNotLifecycle(interceptionType.name()); } if (internalInterceptionType.equals(org.jboss.weld.interceptor.spi.model.InterceptionType.AROUND_CONSTRUCT)) { return extractCdiInterceptors(interceptionModel.getConstructorInvocationInterceptors()); } return extractCdiInterceptors(interceptionModel.getInterceptors(internalInterceptionType, null)); }
@Override @SuppressWarnings("unchecked") public InterceptorInvocation getInterceptorInvocation(Object interceptorInstance, InterceptionType interceptionType) { return new CustomInterceptorInvocation<T>(factory.getInterceptor(), (T) interceptorInstance, javax.enterprise.inject.spi.InterceptionType.valueOf(interceptionType.name())); }
@Override public List<Interceptor<?>> getMethodInterceptors(InterceptionType interceptionType, Method method) { if (interceptionType == null) { throw BeanLogger.LOG.interceptionTypeNull(); } if (method == null) { throw BeanLogger.LOG.methodNull(); } org.jboss.weld.interceptor.spi.model.InterceptionType internalInterceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType.name()); if (internalInterceptionType.isLifecycleCallback()) { throw BeanLogger.LOG.interceptionTypeLifecycle(interceptionType.name()); } return extractCdiInterceptors(interceptionModel.getInterceptors(internalInterceptionType, method)); }
throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveVoidReturnType( method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Void.TYPE.getName(), Formats.formatAsStackTraceElement(method)); ValidatorLogger.LOG.interceptorMethodDoesNotHaveZeroParameters( method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName()); throw ValidatorLogger.LOG.interceptorMethodDeclaresMultipleParameters( method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Formats.formatAsStackTraceElement(method)); ValidatorLogger.LOG.interceptorMethodShouldNotThrowCheckedExceptions( method.getName(), method.getDeclaringClass().getName(), exceptionType.getName(), Formats.formatAsStackTraceElement(method));
private static boolean isValidInterceptorClassLifecycleInterceptorMethod(InterceptionType interceptionType, Method method) { if (!Object.class.equals(method.getReturnType()) && !Void.TYPE.equals(method.getReturnType())) { throw ValidatorLogger.LOG.interceptorMethodDoesNotReturnObjectOrVoid(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Void.TYPE.getName(), OBJECT_CLASS_NAME, Formats.formatAsStackTraceElement(method)); } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 0) { return false; } else if (parameterTypes.length == 1) { if (InvocationContext.class.isAssignableFrom(parameterTypes[0])) { return true; } throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveCorrectTypeOfParameter(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), InvocationContext.class.getName(), Formats.formatAsStackTraceElement(method)); } else { throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveExactlyOneParameter(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Formats.formatAsStackTraceElement(method)); } }
private void initClassDeclaredEjbInterceptors() { Class<?>[] classDeclaredInterceptors = interceptorsApi.extractInterceptorClasses(annotatedType); boolean excludeClassLevelAroundConstructInterceptors = constructor.isAnnotationPresent(ExcludeClassInterceptors.class); if (classDeclaredInterceptors != null) { for (Class<?> clazz : classDeclaredInterceptors) { InterceptorClassMetadata<?> interceptor = reader.getPlainInterceptorMetadata(clazz); for (InterceptionType interceptionType : InterceptionType.values()) { if (excludeClassLevelAroundConstructInterceptors && interceptionType.equals(InterceptionType.AROUND_CONSTRUCT)) { /* * @ExcludeClassInterceptors suppresses @AroundConstruct interceptors defined on class level */ continue; } if (interceptor.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType))) { builder.interceptGlobal(interceptionType, null, Collections.<InterceptorClassMetadata<?>>singleton(interceptor), null); } } } } }
@Override public boolean intercepts(InterceptionType type) { return interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type.name())); }
@Override public Object intercept(InterceptionType type, T instance, final InvocationContext ctx) { final org.jboss.weld.interceptor.spi.model.InterceptionType interceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type .name()); final List<InterceptorMethodInvocation> methodInvocations = interceptorMetadata.getInterceptorInvocation(instance, interceptionType) .getInterceptorMethodInvocations(); Set<Annotation> interceptorBindings = null; if (ctx instanceof org.jboss.weld.interceptor.WeldInvocationContext) { interceptorBindings = Reflections.<org.jboss.weld.interceptor.WeldInvocationContext> cast(ctx).getInterceptorBindings(); } try { /* * Calling Interceptor.intercept() may result in multiple interceptor method invocations (provided the interceptor class interceptor methods on * superclasses). This requires cooperation with InvocationContext. * * We use a wrapper InvocationContext for the purpose of executing the chain of interceptor methods of this interceptor. */ return new WeldInvocationContextImpl(ctx, methodInvocations, interceptorBindings, null).proceed(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new WeldException(e); } }
public void interceptMethod(javax.enterprise.inject.spi.InterceptionType interceptionType, Method method, Collection<InterceptorClassMetadata<?>> interceptors, Set<Annotation> interceptorBindings) { checkModelNotBuilt(); InterceptionType weldInterceptionType = InterceptionType.valueOf(interceptionType); if (weldInterceptionType.isLifecycleCallback()) { throw new IllegalArgumentException("Illegal interception type: " + interceptionType); } if (null == methodBoundInterceptors.get(weldInterceptionType)) { methodBoundInterceptors.put(weldInterceptionType, new HashMap<>()); } List<InterceptorClassMetadata<?>> interceptorsList = methodBoundInterceptors.get(weldInterceptionType).get(method); if (interceptorsList == null) { interceptorsList = new ArrayList<>(); methodBoundInterceptors.get(weldInterceptionType).put(method, interceptorsList); } interceptorsList.addAll(interceptors); intercept(weldInterceptionType, interceptors); if (interceptorBindings != null) { // WELD-1742 Associate method interceptor bindings memberInterceptorBindings.put(method, interceptorBindings); } }
@Override public List<InterceptorClassMetadata<?>> getInterceptors(InterceptionType interceptionType, Method method) { if (InterceptionType.AROUND_CONSTRUCT.equals(interceptionType)) { throw new IllegalStateException("Cannot use getInterceptors() for @AroundConstruct interceptor lookup. Use getConstructorInvocationInterceptors() instead."); if (interceptionType.isLifecycleCallback() && method != null) { throw new IllegalArgumentException("On a lifecycle callback, the associated method must be null"); if (!interceptionType.isLifecycleCallback() && method == null) { throw new IllegalArgumentException("Around-invoke and around-timeout interceptors are defined for a given method"); if (interceptionType.isLifecycleCallback()) { if (globalInterceptors.containsKey(interceptionType)) { return globalInterceptors.get(interceptionType);
@Override public boolean expectsInvocationContext() { return !targetClass || !interceptionType.isLifecycleCallback(); }
static boolean isAnnotationClassExpected(InterceptionType interceptionType) { if (InterceptionType.POST_ACTIVATE.equals(interceptionType) || InterceptionType.PRE_PASSIVATE.equals(interceptionType)) { Environment environment = detectEnvironment(); if (environment != null && (Environments.SE.equals(environment) || Environments.SERVLET.equals(environment))) { return false; } } return true; }
public void interceptGlobal(javax.enterprise.inject.spi.InterceptionType interceptionType, Constructor<?> constructor, Collection<InterceptorClassMetadata<?>> interceptors, Set<Annotation> interceptorBindings) { checkModelNotBuilt(); InterceptionType weldInterceptionType = InterceptionType.valueOf(interceptionType); List<InterceptorClassMetadata<?>> interceptorsList = globalInterceptors.get(weldInterceptionType); if (interceptorsList == null) { interceptorsList = new ArrayList<>(); globalInterceptors.put(weldInterceptionType, interceptorsList); } interceptorsList.addAll(interceptors); intercept(weldInterceptionType, interceptors); if (constructor != null) { // WELD-1742 Associate constructor interceptor bindings memberInterceptorBindings.put(constructor, interceptorBindings); } }
@Override public List<Interceptor<?>> getMethodInterceptors(InterceptionType interceptionType, Method method) { if (interceptionType == null) { throw BeanLogger.LOG.interceptionTypeNull(); } if (method == null) { throw BeanLogger.LOG.methodNull(); } org.jboss.weld.interceptor.spi.model.InterceptionType internalInterceptionType = org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType.name()); if (internalInterceptionType.isLifecycleCallback()) { throw BeanLogger.LOG.interceptionTypeLifecycle(interceptionType.name()); } return extractCdiInterceptors(interceptionModel.getInterceptors(internalInterceptionType, method)); }
throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveVoidReturnType( method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Void.TYPE.getName(), Formats.formatAsStackTraceElement(method)); ValidatorLogger.LOG.interceptorMethodDoesNotHaveZeroParameters( method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName()); throw ValidatorLogger.LOG.interceptorMethodDeclaresMultipleParameters( method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Formats.formatAsStackTraceElement(method)); ValidatorLogger.LOG.interceptorMethodShouldNotThrowCheckedExceptions( method.getName(), method.getDeclaringClass().getName(), exceptionType.getName(), Formats.formatAsStackTraceElement(method));
private static boolean isValidInterceptorClassLifecycleInterceptorMethod(InterceptionType interceptionType, Method method) { if (!Object.class.equals(method.getReturnType()) && !Void.TYPE.equals(method.getReturnType())) { throw ValidatorLogger.LOG.interceptorMethodDoesNotReturnObjectOrVoid(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Void.TYPE.getName(), OBJECT_CLASS_NAME, Formats.formatAsStackTraceElement(method)); } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 0) { return false; } else if (parameterTypes.length == 1) { if (InvocationContext.class.isAssignableFrom(parameterTypes[0])) { return true; } throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveCorrectTypeOfParameter(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), InvocationContext.class.getName(), Formats.formatAsStackTraceElement(method)); } else { throw ValidatorLogger.LOG.interceptorMethodDoesNotHaveExactlyOneParameter(method.getName(), method.getDeclaringClass().getName(), interceptionType.annotationClassName(), Formats.formatAsStackTraceElement(method)); } }
private void initClassDeclaredEjbInterceptors() { Class<?>[] classDeclaredInterceptors = interceptorsApi.extractInterceptorClasses(annotatedType); boolean excludeClassLevelAroundConstructInterceptors = constructor.isAnnotationPresent(ExcludeClassInterceptors.class); if (classDeclaredInterceptors != null) { for (Class<?> clazz : classDeclaredInterceptors) { InterceptorClassMetadata<?> interceptor = reader.getPlainInterceptorMetadata(clazz); for (InterceptionType interceptionType : InterceptionType.values()) { if (excludeClassLevelAroundConstructInterceptors && interceptionType.equals(InterceptionType.AROUND_CONSTRUCT)) { /* * @ExcludeClassInterceptors suppresses @AroundConstruct interceptors defined on class level */ continue; } if (interceptor.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(interceptionType))) { builder.interceptGlobal(interceptionType, null, Collections.<InterceptorClassMetadata<?>>singleton(interceptor), null); } } } } }
@Override public boolean intercepts(InterceptionType type) { return interceptorMetadata.isEligible(org.jboss.weld.interceptor.spi.model.InterceptionType.valueOf(type.name())); }