Tavian,

Sorry, the example should be:

    DuplicateDetectionWrapper duplicateDetection = new 
DuplicateDetectionWrapper();

    install(duplicateDetection.wrap(new Module1()));
    install(duplicateDetection.wrap(new Module1())); // Fine
    install(Elements.getModule(Elements.getElements(duplicateDetection.wrap(new 
Module1()))));

See full example here: 
https://github.com/atteo/moonshine/blob/master/container/src/test/java/org/atteo/moonshine/services/internal/DuplicateDetectionWrapperTest.java

So actually DuplicateDetectionWrapper rewrites the modules so only the 
first install() of given submodule is left and the rest are ignored. 
Therefore it needs to be applied on modules before they are rewritten.


The reason I've implemented this is that in my library I rewrite every 
module the user provides. The error happened when the user provided two 
different modules inherited from ServletModule class. 
ServletModule<https://code.google.com/p/google-guice/source/browse/extensions/servlet/src/com/google/inject/servlet/ServletModule.java>
 depends 
on module de-duplication because it installs it's internal module:
install(new InternalServletModule());

which has equals and hashCode implemented to make it a singleton similarly 
as in your example.

Sławek



On Tuesday, November 5, 2013 5:10:07 PM UTC+1, Tavian Barnes wrote:
>
> Thanks, Sławek.  Sadly that doesn't help here because the module returned 
> from Elements.getModule() doesn't compare equal to a new Module1().
>
> What's the need for that in the first place, by the way?  Guice already 
> performs module de-duplication.
>
> On Tuesday, November 5, 2013 10:15:36 AM UTC-5, Sławek Piotrowski wrote:
>>
>> I've encountered similar problem while developing the Moonshine framework.
>> I've solved this by writing a module wrapper which detects install() 
>> calls with duplicated modules.
>> See:
>>
>> https://github.com/atteo/moonshine/blob/master/container/src/main/java/org/atteo/moonshine/services/internal/DuplicateDetectionWrapper.java
>>
>> In your case the usage would be:
>>     DuplicateDetectionWrapper duplicateDetection = new 
>> DuplicateDetectionWrapper();
>>
>>     install(duplicateDetection.wrap(new Module1()));
>>     install(duplicateDetection.wrap(new Module1())); // Fine
>>     
>> install(duplicateDetection.wrap(Elements.getModule(Elements.getElements(new 
>> Module1()))));
>>
>> Regards,
>> Sławek
>>
>> On Monday, November 4, 2013 6:50:36 PM UTC+1, Tavian Barnes wrote:
>>>
>>> Since Elements.getElements() creates a flat list of bindings, 
>>> constructions that rely on module de-duplication in install() don't work 
>>> perfectly with module re-writing.  For example:
>>>
>>> interface Interface {
>>> }
>>>
>>> static class Implementation implements Interface {
>>> }
>>>
>>> class Module1 extends AbstractModule {
>>>   @Override
>>>   public void configure() {
>>>     bind(Interface.class).toInstance(new Implementation());
>>>   }
>>>
>>>   @Override
>>>   public boolean equals(Object obj) {
>>>     return obj instanceof Module1;
>>>   }
>>>
>>>   @Override
>>>   public int hashCode() {
>>>     return 0;
>>>   }
>>> }
>>>
>>> class Module2 extends AbstractModule {
>>>   @Override
>>>   public void configure() {
>>>     install(new Module1());
>>>     install(new Module1()); // Fine
>>>     install(Elements.getModule(Elements.getElements(new Module1()))); // 
>>> A binding to Interface was already configured...
>>>   }
>>> }
>>>
>>> This is especially noticeable with things like Multibinder/MapBinder 
>>> which rely on module de-duplication.
>>>
>>> My idea to fix this is to add an Element implementation 
>>> "InstalledModule" or something that would hold the bindings from installed 
>>> modules, and replay them only if the module wasn't already installed in the 
>>> target binder.
>>>
>>> That's obviously a pretty big change though.  Is there a better way I 
>>> could approach this?  Is a "fix" even desired?
>>>
>>

-- 
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 http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to