xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

PrivateModule prevents interceptors to be applied #722

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:
When an interceptor is bound inside a private module the original class will 
not be enhanced if its bound and exposed via an interface. If the class is 
bound and exposed directly or if the interface is bound in a non private module 
the interceptors are applied correctly. This seems incorrect and an 
inconsistent behavior. 

Steps to reproduce:
1. run all 3 unit tests in attached InterceptorTest class
2. testPrivateModule fails, but the other 2 run fine

Original issue reported on code.google.com by wixner@gmail.com on 2 Aug 2012 at 12:44

Attachments:

GoogleCodeExporter commented 9 years ago
Probably related to issue702

Original comment by wixner@gmail.com on 2 Aug 2012 at 12:45

GoogleCodeExporter commented 9 years ago
upload new test without the @Ignore annotation

Original comment by wixner@gmail.com on 2 Aug 2012 at 1:04

Attachments:

GoogleCodeExporter commented 9 years ago
Somewhat unfortunately, this is working-as-designed.

Because of the way just-in-time-bindings work, in the HelloModulePrivate, Guice 
does not know that the statement:
 bind(HelloWord.class).to(HelloWorldImpl.class)
also means that 'HelloWorldImpl' is 'private'.  The statement is only saying, 
"Link HelloWorld to HelloWordImpl".  Because of that, HelloWordImpl is actually 
being bound in the parent module, and the interceptor in the private module 
can't see it.

This is why HelloModulePrivateNoInterface works -- you have a separate 
bind(HelloWorldImpl.class) line, telling Guice that HelloWorldImpl wants to be 
private.

If you add that bind statement (doesn't need to be a singleton as it is in 
HelloModulePrivateNoInterface) in the HelloModulePrivate, then things will 
start working.

Another fix would be to add 'binder().requireExplicitBindings()' to the parent 
Module, and that will tell Guice that you don't want just-in-time bindings at 
all, and everything should exist in the Module is was declared in.

Original comment by sberlin on 2 Aug 2012 at 1:27

GoogleCodeExporter commented 9 years ago
A bit surprising, but both your solutions work fine for me. 
Many thanks for the prompt and useful reply!

Original comment by wixner@gmail.com on 2 Aug 2012 at 2:02