problems implementing Provider<T>, where T is a generic

1,658 views
Skip to first unread message

Grary

unread,
Dec 12, 2009, 6:50:24 PM12/12/09
to google-guice
Folks,

I want to provide multiple instances of
java.util.Set<java.lang.String> in my class, but I just can't seem to
get it to work with Guice's Provider<T>.

There seems to be a hole in the documentation as far the following key
steps are concerned:

1) Can Provider<T> be implemented where T is a generic class?
2) If so, how does the binding look?

Any thoughts?

Thanks,

Grar

Karthik Krishnan

unread,
Dec 12, 2009, 8:16:12 PM12/12/09
to google...@googlegroups.com
Have you looked at multibinding?

http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/multibindings/Multibinder.html


--

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



Grary

unread,
Dec 12, 2009, 9:27:16 PM12/12/09
to google-guice
Karthik,

> Have you looked at multibinding?

That looks good, but a side-concern of mine is that whatever
functionality I discover in this regard also be available in Guice's
client-side cousin, GIN. For this reason, I was hoping to stick with
major releases.

Have you any thoughts on my design issue here: I'm attempting to
follow the Guice DI design imperative and not use new(), but how to
generate simple generic containers as return values? I gleaned from
Dhanji's Google IO 2009 talk that DI can help one write better code by
forcing one to think about depedencies. In this case, however, I can't
see how to get any simpler with Guice 2.0 than attempt a return of
some java.util generic container.

Thanks,

Grary

dg

unread,
Dec 12, 2009, 9:53:33 PM12/12/09
to google-guice

bind( new TypeLiteral<Set<String>>(){} ).to( MyCustomStringSet.class )

Then you can

@Inject SomeClass( Set<String> strings ) { ..... }
@Inject SomeClass( Provider<Set<String>> stringProvider ) { ..... }

of course, toInstance, toProvider, work too, it's the TypeLiteral
that's the magic.

-d

Stuart McCulloch

unread,
Dec 12, 2009, 10:00:24 PM12/12/09
to google...@googlegroups.com
2009/12/13 dg <gil...@gmail.com>

bind( new TypeLiteral<Set<String>>(){} ).to( MyCustomStringSet.class )

also from the Guice wiki:

  "Guice has complete type information for everything it injects. If you're injecting parameterized types, you can inject a TypeLiteral<T> to reflectively tell you the element type."

so you could even do this:

  bind( new TypeLiteral<Set<String>>(){} ).toProvider( MyCustomSetProvider.class );

and have TypeLiteral injected in the provider constructor, which would tell you what sort of set to provide...

--
Cheers, Stuart

Grary

unread,
Dec 13, 2009, 10:44:29 AM12/13/09
to google-guice
Gents,

Thanks all -- it works. Not sure I entirely grasp the full potential
of this capability from TypeLiteral<T>, per the documentation:

"Java doesn't yet provide a way to represent generic types, so this
class does."

But, in the meanwhile, having a means to generate generic Java
containers is great.

Thanks,

Grar
Reply all
Reply to author
Forward
0 new messages