The issue is most likely with the design of the code -- you shouldn't really
be retrieving things from the Injector.  Instead, your code should have
dependencies that are automatically injected.

However, without redesigning the code...Java doesn't provide a good way to
create parameterized types at runtime.  Guice provides some nice helper
methods to do this for you.  You can use Types.newParameterizedType (which
returns a Type), and then TypeLiteral.get(Type) to get a TypeLiteral out of
that, which can be used throughout the rest of Guice.  In your case, you
could do:
  Type parameterizedButler = Types.newParameterizedType(Butler.class,
person.getClass());
  TypeLiteral butlerLiteral = TypeLiteral.get(parameterizedButler);

sam

On Fri, Nov 5, 2010 at 8:17 AM, lahnsurfer <[email protected]> wrote:

> Hi dear Guice users,
>
> My problem is that i can't locate a generic service at runtime. To
> illustrate this issue i have
> written a testcase.
>
> I am currently stuck in the method
> private Butler<Person> findButler(Person person, Injector injector). I
> don't know how
> to get an instance for Butler<personClass>, where personClass is only
> known at runtime.
> I also tried searching the forum but could't find anything.
>
> Here's the testcase:
>
> import junit.framework.TestCase;
>
> import com.google.inject.AbstractModule;
> import com.google.inject.Guice;
> import com.google.inject.Injector;
> import com.google.inject.TypeLiteral;
>
> public class GuiceInjectServiceTest extends TestCase {
>
>        private static abstract class Person {
>        }
>
>        private static class BigPerson extends Person {
>        }
>
>        private static class SmallPerson extends Person {
>        }
>
>        public interface Butler<T extends Person> {
>                String serve(T person);
>        }
>
>        public static class BigPersonButler implements Butler<BigPerson>{
>                @Override
>                public String serve(BigPerson person) {
>                        return "Beer";
>                }
>        }
>
>        public static class SmallPersonButler implements
> Butler<SmallPerson>{
>                @Override
>                public String serve(SmallPerson person) {
>                        return "Water";
>                }
>        }
>
>        public void testGetInstance() throws Exception {
>                Injector injector = Guice.createInjector(new
> AbstractModule() {
>
>                        @Override
>                        protected void configure() {
>                                bind(new TypeLiteral<Butler<BigPerson>>() {
>                                }).to(BigPersonButler.class);
>                                bind(new TypeLiteral<Butler<SmallPerson>>()
> {
>                                }).to(SmallPersonButler.class);
>                        }
>                });
>                BigPerson bigPerson = injector.getInstance(BigPerson.class);
>                Butler<Person> butler = findButler(bigPerson, injector);
>                String drink = butler.serve(bigPerson);
>                assertEquals("Beer", drink);
>        }
>
>        private Butler<Person> findButler(Person person, Injector injector){
>                Class<? extends Person> personClass = person.getClass();
>
>                /* How can i locate the class Butler<personClass> ?? */
> //              Key<Butler> butlerKey = Key.get(new
> TypeLiteral<Butler>(){});
> //              Butler butler = injector.getInstance(butlerKey);
> //              return butler;
>
>                return null;
>        }
>
> }
>
>
> Thanks in advance
> Tim
>
> --
> 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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

-- 
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