How to bind method interceptor to provider?
Asked Answered
A

2

13

Is there way to bind a method interceptor to a provider rather than an instance?

e.g. I use the code below to bind interceptors how would I bind INTERCEPTOR to a provider and then to the annotation?

bindInterceptor(
    Matchers.any(), Matchers.annotatedWith(ANNOTATION.class), new INTERCEPTOR());
Abampere answered 6/12, 2011 at 20:13 Comment(2)
What are you trying to intercept? the get() method or maybe some setter method?Shamus
Well any method ideally, we have a performance monitoring system that intercepts method calls and times their execution etc. I want to lazily initialize the interceptor.Abampere
S
21

Guice does not allow AOP on instances not built by Guice: Guice AOP Limitations

"Instances must be created by Guice by an @Inject-annotated or no-argument constructor"

This means that instances created with a provider will not be candidates for AOP.

On the flip side, as long as your Provider is instantiated by Guice under the conditions mentioned, your Provider may be a candidate for AOP.

Here's an example that demonstrates this:

AOP Annotation:

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface AOPExample {}

Provider:

public class ExampleProvider implements Provider<Example> {

    @AOPExample
    public Example get() {
        System.out.println("Building...");
        return new Example();
    }
}

Target Example:

public class Example {

    @AOPExample
    public void tryMe() {
        System.out.println("example working...");
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Module:

public class ExampleModule extends AbstractModule {
    @Override
    protected void configure() {
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), new LoggingAOP());

        bind(Example.class).toProvider(ExampleProvider.class);
    }
}

Test Code:

public class Test {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new TestModule());

        ExampleProvider exampleProvider = injector.getInstance(ExampleProvider.class);
        Example example = exampleProvider.get();

        example.tryMe();

        Example directExample = injector.getInstance(Example.class);

        directExample.tryMe();

    }
}

Test Output:

start
Building...
end took: 3
example working...
start
Building...
end took: 0
example working...

Notice that the "example working..." is not surrounded by the timer code. The Provider.get ("Building...") is however.

If your question is: can the interceptor (new INTERCEPTOR()) be provided through a Guice Provider, the answer is no. The closest you may get to this functionality is calling the requestInjection() in the module configure method. This will inject your Interceptor with the appropriate code. From your interceptor you may be able to use Providers to avoid any sort of overhead that is causing you slowness during startup.

Here's what I mean:

Module:

public class TestModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(String.class).toInstance("One");
        bind(String.class).annotatedWith(Names.named("two")).toInstance("Two");

        LoggingAOP loggingAOP = new LoggingAOP();

        bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), loggingAOP);

        requestInjection(loggingAOP);

        bind(Example.class).toProvider(ExampleProvider.class);
    }
}

Interceptor:

public class LoggingAOP implements MethodInterceptor {

    @Inject
    private Provider<SomethingThatTakesALongTimeToInit> provider;

    public Object invoke(MethodInvocation invocation) throws Throwable {
        provider.get()...
        System.out.println("start");
        long start = System.currentTimeMillis();
        Object value =  invocation.proceed();
        System.out.println("end took: " + (System.currentTimeMillis() - start));
        return value;
    }
}

Hope this answers your question.

Shamus answered 7/2, 2012 at 20:59 Comment(2)
Great "Hello World" example to Guice AOP.Vigorous
Thanks @ripper234. By the way we have similar (nearly identical) facilities in Transfuse, a project I've been working on: androidtransfuse.org/documentation.html#method_interceptorsShamus
O
0

The question, as I read it, is, how does one bind the interceptor type itself to a provider, rather than having to instantiate the interceptor at configuration time.

I don't think there's an easy way to do that, but one could write an interceptor that itself accepts a Provider for an implementation type. An example of this is shown in the Guice AOP documentation:

public class NotOnWeekendsModule extends AbstractModule {
    protected void configure() {
    bindInterceptor(any(),
                    annotatedWith(NotOnWeekends.class),
                    new WeekendBlocker(getProvider(Calendar.class)));
    }
}
Orourke answered 26/10, 2018 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.