Hello,

Thanks for you help. I forgot to mention that i get the type
information for the person as String which doesn't give me
(AFAIK) any other options:

String personClassname = "example.BigPerson";
Class<Person> personClass = (Class<Person>)
Class.forName(personClassname);
Person person = injector.getInstance(personClass);
Butler<Person> butler = findButler(person, injector);
String drink = butler.serve(person);
assertEquals("Beer", drink);



On 5 Nov., 14:05, Sam Berlin <[email protected]> wrote:
> 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