On 18 April 2012 17:47, Mirko Raner <[email protected]> wrote:
> Hi all,
>
> the Guice documentation for the Binder class states that "Guice cannot
> currently bind or inject a generic type, such as Set<E>; all type parameters
> must be fully specified."
> Is there any remedy for this limitation in sight?
>
> What I'm trying to do is the following:
> A common source of unwanted coupling is introduced by using specific
> implementations of collection types or other generic types, for example
>
>     Map<String, Integer> elements = new HashMap<String, Integer>();
>
> which ties the code to a specific implementation (HashMap, in this case).
> Let's say that depending on my deployment scenario I might want to use GNU
> Trove classes like THashMap instead.
> However, rather than hard-coding the implementing class in my code, I would
> like to write
>
>     @Inject Map<String, Integer> elements;
>
> and have Guice take care of the rest. I obviously don't want to create
> bindings specifically for maps from String to Integer, but for maps in
> general.
> There might also be additional annotations to specify implementation
> preferences, for example
>
>     @Inject @Identity Map<String, Integer> elements; // requires
> IdentityHashMap or similar
>     @Inject @Synchronized @Linked Map<String, Integer> elements; // requires
> a synchronized LinkedHashMap or similar
>
> Is support for features like this planned for a future release of Guice? Or
> is there a way to do this already?

Can't you simply do MyMap extends Map<String, Integer> and inject
that? Or create a wrapper with a getter and inject an instance of that
wrapper? Sure, it's a bit more work but how many different maps do you
need?

In fact, you should probably use MyMap directly (without extending
Map) so you can limit its API to exactly what you need. The exact map
implementation that it uses is then really just an implementation
detail (it need not even implement Map<String, Integer>).

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to