Question

Is it possible to extract the value of a method variable once/while it is intercepted? I don;t wish to intercept the parameters but an attribute value within the method? e.g.

Business Logic: 

@MyInterceptor
void myMethod(Object o){

 ArrayList myList= null;
 myList= dao.getRecords(o.getId) //intercept the result of this dao call


//I only want to call doWork after I have 'validated' contents of myList in interceptor

 doWork(myList)


}


The Interceptor:  

@Interceptor
@MyInterceptor
MyInterceptor{

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

 //retrieve the contents of myList above and perform validation
 //if it passes validation call ctx.proceed else return error

}

}

Thanks

Was it helpful?

Solution

I am afraid you can't really do this with interceptors because they have no access to method internal variables (just look at InvocationContext javadocs). So your only chance to do this would to make myList a beans property and then do this in your interceptor.

@AroundInvoke{
public Object invoke(InvocationContext ctx) throws Exception {
    if(ctx.getTarget() instanceof BeanWithListProperty) {
        Object toProceed = ctx.proceed(); 
        BeanWithListProperty bean = (BeanWithListProperty) ctx.getTarget();
        List list = bean.getMyList();
        return toProceed;
    }
    return ctx.proceed();
}

Other option would be to use Decorators which will lead to slightly more readable and efficient code.

However I don't like these solutions much, seems to me that your code is really not well designed, what are you trying to achive?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top