>> Does it weave aspects at load time or at compilation time?
I’m fairly certain it does this at runtime.
>> How is your project set up so that AOP knows what classes are subject to weaving?
Guice provides a bindInterceptor(…) method to bind any classes annotated with annotation X with MethodInterceptor Y. So my test example is directly from the Guice AOP docs (https://code.google.com/p/google-guice/wiki/AOP).
public class AopModule extends AbstractModule {
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(NotOnWeekends.class),
new WeekendBlocker());
}
}
Unfortunately it’s no longer the weekend, so we can modify WeekendBlocker class from the Guice-AOP docs to always throw an exception:
public class WeekendBlocker implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
throw new IllegalStateException(invocation.getMethod().getName() + " not allowed on weekends!");
}
}
Here’s the main method which executes the test:
public class AopApp {
public static void main(String args) {
Injector injector = Guice.createInjector(new AopModule());
// Simple simple = injector.getInstance(Simple.class); // java
KotlinSimple simple = injector.getInstance(KotlinSimple.class);
simple.hello();
}
}
And KotlinSimple source:
public class KotlinSimple {
NotOnWeekends
fun hello() {
println(“Hello Kotlin”)
}
}
Guice-Persist @Transactional annotations are bound similarly (though this is done for you by including JpaPersistModule):
// class-level @Transacational
bindInterceptor(annotatedWith(Transactional.class), any(), getTransactionInterceptor());
// method-level @Transacational
bindInterceptor(any(), annotatedWith(Transactional.class), getTransactionInterceptor());
See here: https://code.google.com/p/google-guice/source/browse/extensions/persist/src/com/google/inject/persist/PersistModule.java