Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept private methods inside CDI beans

I have a CDI bean with private method inside it. I have written an interceptor like following:

Interceptor's code:

@Interceptor
@TimeMeasure
public class TimeWatcher implements Serializable {

    @AroundInvoke
    public Object logMethodEntry(InvocationContext ctx) throws Exception {

        long t0 = System.nanoTime();
        try {
            return ctx.proceed();
        } finally {
            long dt = System.nanoTime() - t0;
            System.out.println("Method execution time: " + ctx.getMethod().getName() +  " - " + dt);
        }
    }
}

Annotation's code:

 import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.ElementType.TYPE;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;


    @InterceptorBinding
    @Target({TYPE, METHOD})
    @Retention(RUNTIME)
    public @interface TimeMeasure {

    }

Everything works fine only for public methods which are called externally, if I call method from inside CDI bean it doesn't work. I use JSF 2.0 MyFaces together with Omnifaces to accomplish @ViewScoped

Thank you in advance.

like image 698
Anatoly Avatar asked Nov 04 '14 09:11

Anatoly


1 Answers

This is by design. Internal calls can never be intercepted.

like image 148
Alexander Langer Avatar answered Oct 02 '22 14:10

Alexander Langer