I'm using Guice and AspectJ, and I am trying to do some AOP to measure the execution time of certain methods.
I have this annotation which will be used to annotate all the methods I need to measure:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface MyStopWatch {
}
I have this method interceptor:
public class MyInterceptor implements org.aopalliance.intercept.MethodInterceptor {
private final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
final org.apache.commons.lang3.time.StopWatch stopWatch = org.apache.commons.lang3.time.StopWatch.createStarted();
final Object returnedObject = invocation.proceed();
stopWatch.stop();
logger.info("[STOPWATCH] Method: {} - Time: {}.", invocation.getMethod().getName(), stopWatch.getTime());
return returnedObject;
}
}
I have this interface
public interface MySuperClass {
@MyStopWatch
default void test() {
System.out.println("Hello, world");
}
}
Then I have this subclass which inherits from MySuperClass:
public class MySubClass implements MySuperClass {
}
And finally, I have this bind:
public class MyAOPModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(
Matchers.any(),
Matchers.annotatedWith(MyStopWatch.class),
new MyInterceptor()
);
}
}
I initialize my Guice module like this:
public static void main(String[] args) throws Throwable {
Injector injector = createInjector(new MyAOPModule());
injector.getInstance(MySubClass.class).test();
}
My problem is that there is nothing being logged, as if the subclass's execution of the test() method was not annotated.
Any way I can go around this?