Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] @AfterReturning not called when theJoinPoint and "returning" specified

Hi all,

I've bumped into an issue that I'm hoping someone here can resolve - I'm new to AspectJ so perhaps I'm missing the boat.  In any case,  on an @AfterReturning joinpoint I need to get the method's return value and the thisJoinPoint.  The problem I've encountered is if I add the JoinPoint argument as the 1st arg and specify a returning value the join is never invoked when the returning value is anything other than a String (and likely other java.* objects - I haven't done a comprehensive test).  Other combinations work and the same syntax works when the returning value is a String (and as I noted likely other java.* types). I've pasted a reproducible sample below.

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class BustedAspectJTest {

    @Pointcut("call(public Foo.X *.findByPrimaryKey(String,boolean)) && args(pk,..)")
    public void facadeFindByPrimaryKeyMethodCapture(String pk) {
    }

    // this join does not work - attempts to capture both  thisJoinPoint and the returning value
    @AfterReturning(pointcut = "facadeFindByPrimaryKeyMethodCapture(pk)", returning = "dto")
    public void afterAdvicefacadeFindByPrimaryKeyMethodCapture(JoinPoint thisJoinPoint, String pk, Foo.X dto) {
        System.out.println("I'M NOT WORKING WITH NON-PRIMITIVE RETURN TYPES - facadeFindByPrimaryKeyMethodCapture - after with returning value [" + dto + "] and thisJoinPoint ->" + thisJoinPoint);
    }

    @Before("facadeFindByPrimaryKeyMethodCapture(pk)")
    public void beforeAdvicefacadeFindByPrimaryKeyMethodCapture(JoinPoint thisJoinPoint, String pk) throws Exception {
        System.out.println("facadeFindByPrimaryKeyMethodCapture before");
    }

    @AfterReturning(pointcut = "facadeFindByPrimaryKeyMethodCapture(pk)", returning = "dto")
    public void afterAdvicefacadeFindByPrimaryKeyMethodCapture(String pk, Foo.X dto) {
        System.out.println("WORKS WITH RETURN VALUE BUT WITHOUT JOINPOINT - facadeFindByPrimaryKeyMethodCapture - after with returning value [" + dto + "] and thisJoinPoint ->");
    }

    @AfterReturning(pointcut = "facadeFindByPrimaryKeyMethodCapture(pk)")
    public void afterAdvicefacadeFindByPrimaryKeyMethodCapture(JoinPoint thisJoinPoint, String pk) {
        System.out.println("WORKS WITH JOINPOINT BUT WITHOUT RETURN VALUE");
    }


    // following joins working in all cases
    @Pointcut("call(public String *.findByPrimaryKeyPrimitiveReturnType(String,boolean)) && args(pk,..)")
    public void facadeFindByPrimaryKeyStringReturnTypeMethodCapture(String pk) {
    }

    @Before("facadeFindByPrimaryKeyStringReturnTypeMethodCapture(pk)")
    public void beforeFacadeFindByPrimaryKeyStringReturnTypeMethodCapture(JoinPoint thisJoinPoint, String pk) throws Exception {
        System.out.println("--- All joins working below  --");
        System.out.println("facadeFindByPrimaryKeyStringReturnTypeMethodCapture before");
    }

    @AfterReturning(pointcut = "facadeFindByPrimaryKeyStringReturnTypeMethodCapture(pk)", returning = "dto")
    public void facadeFindByPrimaryKeyStringReturnTypeMethodCapture(JoinPoint thisJoinPoint, String pk, String dto) {
        System.out.println("WORKS WITH JOINPOINT AND STRING RETURN VALUE- facadeFindByPrimaryKeyStringReturnTypeMethodCapture after with returning value [" + dto + "] and thisJoinPoint ->" + thisJoinPoint);
    }
}
*****************************************************************
And here's the tester:
*****************************************************************

public class Foo {

    public Foo.X findByPrimaryKey(String x, boolean cascade) {
        return new X();
    }

    public String findByPrimaryKeyPrimitiveReturnType(String x, boolean cascade) {
        return "returned";
    }

    public static void main(String[] args) {
        Foo instance = new Foo();
        instance.findByPrimaryKey("x",true);
        instance.findByPrimaryKeyPrimitiveReturnType("y",true);
    }

    public class X {
      public String toString() {
          return this.getClass().getName();
      }
    }
}



Thanks,
Trey

Back to the top