Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the values of the parameters passed to the method while handling exception with Spring AOP

In my aspect class's method I want to get the values of the parameters and the name of the parameters. Names are still ok if I don't get but I need to get the values of the parameters passed is it possible? (There is not issue with ptCut expression , the method is getting called i checked with sysouts)

My Aspect method is something like this :

    public void excpetionHappened(Exception e) {
    // Log the exception
    // log the name of the method name/signature which caused the exception
    // log the value of the input parameters to the method
    // wrap and throw new exctn

}

Thanks in advance.

like image 922
java_enthu Avatar asked Mar 20 '12 08:03

java_enthu


1 Answers

You can use a around advice.

This makes it possible to access the parameter while handling the exception.

public aspect ExceptionReporterAspect {

    /** The name of the used logger. */
    public final static String LOGGER_NAME = "AspectJExceptionLogger";

    /** Logger used to log messages. */
    private static final Log LOGGER = LogFactory.getLog(LOGGER_NAME);

    pointcut stringRequestHandler() : 
        execution (@RequestMapping Object the.package..*(..));

    Object around(): objectRequestHandler(){
        try {
            return proceed();
        } catch (Exception ex){
            Signature sig = thisJoinPointStaticPart.getSignature();
            Object[] args = thisJoinPoint.getArgs();

            String location = sig.getDeclaringTypeName() + '.' + sig.getName() + ", args=" + Arrays.toString(args);
            LOGGER.warn("(AOP detected) exception within " + location, ex);

               throw(ex)
        }
    }   
}
like image 126
Ralph Avatar answered Sep 20 '22 05:09

Ralph