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].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to