I have following code where my pointcut is based on annotation on interface. It does not work if object variable on caller method is defined as implementation class. But if I cast (same object) it to Interface then it works !!
package sample;
public interface MyInterface {
@Write //runtime annotation
public void methodA();
}
// In Write.java file
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface Write {
}
package sample;
public class MyImpl implements MyInterface {
public void methodA() {
System.out.println("MyImpl");
}
}
And two aspects defined as
@Aspect
public class MySimpleAspect {
@Around("execution(public * sample.MyImpl.*(..))")
public Object simpleAround(ProceedingJoinPoint point) throws Throwable {
System.out.println("in Simple Aspect");
return point.proceed();
}
}
@Aspect
public class MyComplexAspect {
@Around("call(@Write * sample.MyInterface+.*(..))") // doesn't get invoked if <includes> is part of pom
public Object complexAround(ProceedingJoinPoint point) throws Throwable {
System.out.println("in complex Aspect");
return point.proceed();
}
}
Test class
package sample;
import org.junit.Test;
public class MyTest {
@Test
public void testMyImpl() {
System.out.println("-- Using MyImpl.methodA() ---");
MyImpl my = new MyImpl(); //does not work
my.methodA();
System.out.println("\n-- Using myinterface.methodA() ---");
MyInterface myinterface = my; //works
myinterface.methodA();
}
}
Output
-------------------------------------------------------
T E S T S
-------------------------------------------------------
OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
Running sample.MyTest
-- Using MyImpl.methodA() ---
in Simple Aspect
MyImpl
-- Using myinterface.methodA() ---
in complex Aspect
in Simple Aspect
MyImpl
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec
UPDATE
Updated question with more code and output and removed pom/maven related stuff as it turns out to be pure aspectj problem and not maven
- AspectJ version 1.9.6
- AspectJ Maven Plugin 1.11
- JDK 1.8