Search code examples
javacdi

CDI-Interceptor: Get param from intercepted Method


I have a interface

@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface LoggingInterceptorBinding {

}

and a class:

@LoggingInterceptorBinding
@Interceptor
public class LoggingInterceptor implements Serializable {

@AroundInvoke
public Object onMethodCall(InvocationContext context) throws Exception {
    try {
        System.out.println("Log before Method");
        return context.proceed();
    } finally {
        System.out.println("Log after Method");
    }

}

and a annotated method:

@LoggingInterceptorBinding
public void sayHello(String name)

Is it possible to get the parameter "name" from sayHello in the interceptors "onMethodCalls"-method?


Solution

  • The InvocationContext interface has a getParameters() method that

    Returns the parameter values that will be passed to the method of the target class. If setParameters() has been called, getParameters() returns the values to which the parameters have been set.