Could you maybe create a Foo subclass?

class SubFoo extends Foo {
  public SubFoo() {
    super(10);
  }
}
…
bind(Foo.class).to(SubFoo.class);
bindInterceptor(…)…

On Friday, November 4, 2016 at 3:48:42 AM UTC+1, lei wang wrote:
>
> I want to add an interceptor to a 3rd party class. Since it does not have 
> a no-arg constructor, I'd have to use a provider to create an instance. But 
> it seems that provider returns a raw object (no proxy), and I can't 
> intercept it. Do i miss something? How can I add an interceptor to a class 
> without no-arg constructor ? Here is a snippet..
>
> class Foo {
>     public Foo(int value) {...}
>
>     public void exec() {...}
> }
>
> class Bar {
>      public void bar() {...}
> }
>
> class FooBarModule extends AbstractModule {
>     protected void configure() {
>         bind(Foo.class).toProvider(FooProvider.class);
>         bind(Bar.class);
>
>         
>  
> bindInterceptor(Matchers.subclassesOf(Foo.class).or(Matchers.subclassesOf(Bar.class)),
>  
> Matchers.any(), new FooBarInterceptor());
>     }
> }
>
>
> class FooMain {
>     public static void main(String[] args) {
>        Injector injector = Guice.createInjector(new FooModule());
>        Foo fooObj = injector.getInstance(Foo.class);
>
>        fooObj.exec();           // FooBarInterceptor is NOT triggered, 
> because fooObj is a raw object.
>
>        Bar barObj = injector.getInstance(Bar.class);
>        barObj.bar();             // FooBarInterceptor is triggeredas 
> expected. barObj is a Guice proxied object
> }
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/7ccae02c-4361-454b-9add-fef01684dd2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to