Search in sources :

Example 1 with AroundConstruct

use of javax.interceptor.AroundConstruct in project javaee7-samples by javaee-samples.

the class MyInterceptor method onConstruct.

@AroundConstruct
public Object onConstruct(InvocationContext context) throws Exception {
    // null before the InvocationContext.proceed() returns
    Object target = context.getTarget();
    isNull(target);
    // null in case of AroundConstruct
    Method method = context.getMethod();
    isNull(method);
    // NOT null in case of AroundConstruct
    Constructor ctor = context.getConstructor();
    isNotNull(ctor);
    // perform the constructor injection
    Object result = context.proceed();
    isNull(result);
    // NOT null after the InvocationContext.proceed() completes
    target = context.getTarget();
    isNotNull(target);
    // a constructor should have been called
    GreetingBean bean = (GreetingBean) target;
    isBoolean(bean.isConstructed(), true);
    isBoolean(bean.isInitialized(), false);
    // constructor injection should have been done
    isNotNull(bean.getParam());
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) AroundConstruct(javax.interceptor.AroundConstruct)

Example 2 with AroundConstruct

use of javax.interceptor.AroundConstruct in project Payara by payara.

the class MetricsInterceptor method constructorInvocation.

@AroundConstruct
private Object constructorInvocation(InvocationContext context) throws Exception {
    Object target;
    MetricsService metricsService = Globals.getDefaultBaseServiceLocator().getService(MetricsService.class);
    if (metricsService.isMetricsEnabled()) {
        Class<?> beanClass = bean.getBeanClass();
        registerMetrics(beanClass, context.getConstructor(), context.getTarget());
        target = context.proceed();
        Class<?> type = beanClass;
        do {
            for (Method method : type.getDeclaredMethods()) {
                if (!method.isSynthetic() && !Modifier.isPrivate(method.getModifiers())) {
                    registerMetrics(beanClass, method, context.getTarget());
                }
            }
            type = type.getSuperclass();
        } while (!Object.class.equals(type));
    } else {
        target = context.proceed();
    }
    return target;
}
Also used : MetricsService(fish.payara.microprofile.metrics.MetricsService) Method(java.lang.reflect.Method) AroundConstruct(javax.interceptor.AroundConstruct)

Example 3 with AroundConstruct

use of javax.interceptor.AroundConstruct in project Payara by payara.

the class SystemInterceptorProxy method setDelegate.

public void setDelegate(Object d) {
    Class delegateClass = d.getClass();
    try {
        for (Method m : delegateClass.getDeclaredMethods()) {
            if (m.getAnnotation(PostConstruct.class) != null) {
                postConstruct = m;
                prepareMethod(m);
            } else if (m.getAnnotation(PreDestroy.class) != null) {
                preDestroy = m;
                prepareMethod(m);
            } else if (m.getAnnotation(AroundInvoke.class) != null) {
                aroundInvoke = m;
                prepareMethod(m);
            } else if (m.getAnnotation(AroundTimeout.class) != null) {
                aroundTimeout = m;
                prepareMethod(m);
            } else if (m.getAnnotation(AroundConstruct.class) != null) {
                aroundConstruct = m;
                prepareMethod(m);
            }
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    delegate = d;
}
Also used : AroundConstruct(javax.interceptor.AroundConstruct) Method(java.lang.reflect.Method) PostConstruct(javax.annotation.PostConstruct) AroundInvoke(javax.interceptor.AroundInvoke) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Method (java.lang.reflect.Method)3 AroundConstruct (javax.interceptor.AroundConstruct)3 MetricsService (fish.payara.microprofile.metrics.MetricsService)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PostConstruct (javax.annotation.PostConstruct)1 AroundInvoke (javax.interceptor.AroundInvoke)1