Search code examples
javaaopjavaagents

Get the name of the response object through AOP


Using AOP I know using java 8 and a pointcut it is now possible to get the names of a methods method parameters through AOP.

My question is, is it also possible to get the name of the object being returned?

Update - Adding code example:

I'm trying to log the name of the object being returned from a method

    /**
     * Log the return value of all methods in the package service.impl
     * @param pjp
     * @param returnValue
     */
    @AfterReturning(pointcut = "execution(public * com.bla.core.service.impl.*.*(..))", returning = "returnValue")
    public void debugAfter(JoinPoint pjp, Object returnValue) {
        if (logger.isDebugEnabled()) {
            logger.debug(getAuditEndMessage(pjp, returnValue));
        }
    }


    private String getAuditEndMessage(JoinPoint joinPoint, Object returnValue) {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getSimpleName();

        //Assuming every object has the toString method overriden
        //In reality there is more logic than this
        String returnValueStr = String.valueOf(returnValue);

        String returnValueObjectName = //Trying to find some way of finding this value

        //Ex: "[End][CategoryServiceImpl][getCategory]Ouput: [category=[id=1][name=Test Category][description=Test Description]]
        String returnStr = "[End][" + className + "][" + methodName + "]Ouput: [" + returnValueObjectName + "=" + returnValueStr + "]"

        return returnStr;
    }

So if I had a method:

    public ExampleCategory getCategory(int categoryId){
        ExampleCategory category = exampleCategoryDao.read(categoryId);
        return category;
    }

I want the output string to contain the name of the object being returned:

[End][CategoryServiceImpl][getCategory]Ouput: [category=[id=1][name=Test Category][description=Test Description]]

I can get the names of the input parameters with the new java 8 functionality, but I don't know how to do this with the return value.

Update 2:

After typing this all up, I realized that if this is possible, there could also be weird cases like:

    public ExampleCategory getCategory(int categoryId){
        return exampleCategoryDao.read(categoryId);
    }

I could just print the returning class name in those cases. I would still like to know if it is possible to do what I'm asking above.


Solution

  • As I already said in that answer, AspectJ has no knowledge about local variables, only about non-static or static member variables. So the answer to your question is no.

    Even if that was possible, I do not see any added value in knowing local variable names. There you are far beyond the concept of AOP (implementing cross-cutting concerns) and rather within the realm of a debugger. Besides, the very concept of local variables (and even the term itself!) is about their locality. Other than methods they are not part of the contract of a class but completely internal to its implementation.

    By the way, the advice name @AfterReturning is a hint as to its purpose: get some info and do something after a method has returned its result. You want to know some internal method state just before it returns the result.