Re: Binding types that are parameterized with wildcards
On Nov 28, 6:09 pm, "je...@swank.ca" wrote: > You'll need casting > because the built-in Type classes aren't themselves parameterized > types The cast solved the problem, thanks! For completeness, I'd like to provide a somewhat more generalized solution here, which isn't limited to lists: public abstract class C { } public class CImpl extends C { } public class MyModule extends AbstractModule { protected void configure() { Type questionMark = Types.subtypeOf(Object.class); Type cOfQuestionMark = Types.newParameterizedType(C.class, questionMark); TypeLiteral> typeLiteral = (TypeLiteral) TypeLiteral.get (cOfQuestionMark); bind(typeLiteral).to(new TypeLiteral>() {}); } } Not very concise or easy to understand, but at least it works :-) -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-gu...@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Re: Binding types that are parameterized with wildcards
On Nov 28, 7:22 am, chris_l wrote: > This works fine. But when I replace with , it doesn't work Here's the building blocks. First you need a "?" type, which is actually a shorthand for "? extends Object". You can get that like this: Type questionMark = Types.subtypeOf(Object.class) Next you'll need a list of those Type listOfQuestionMark = Types.listOf(questionMark); Finally, you'll need a TypeLiteral for that. You'll need casting because the built-in Type classes aren't themselves parameterized types: TypeLiteral> typeLiteral = (TypeLiteral) TypeLiteral.get (listOfQuestionMark); -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-gu...@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Binding types that are parameterized with wildcards
Hi, I have a class A: public class A { private List list; @Inject A(List list) { this.list = list; } } And a Module: public class MyModule extends AbstractModule { @Override protected void configure() { bind(new TypeLiteral>() {}).to(new TypeLiteral>() {}); } } This works fine. But when I replace with , it doesn't work (of course). I have read the Javadoc for TypeLiteral, and it says: "This syntax cannot be used to create type literals that have wildcard parameters, such as Class or List. Such type literals must be constructed programatically, either by extracting types from members or by using the Types factory class." But what exactly am I supposed to do here? How do I use the Types factory in this case? I tried, but had no success so far. I know, I can use an @Provides method, but I'd prefer to do it with bind(), if possible. Thanks Chris -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-gu...@googlegroups.com. To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Re: Wildcards...
Thanks for your suggestion - I have discovered the provides methods (annotated with @Provides) that do the same thing. So for all you out there - just use that annotation - will save a lot of work and the code gets a lot clearer. Btw: Guice is great Btw2: Plz upload to maven central Regards, Johannes je...@swank.ca wrote: > > > On Jun 6, 8:48 am, Johannes Schneider wrote: >> bind( ( TypeLiteral> ) TypeLiteral.get( >> Types.listOf( Types.subtypeOf( new TypeLiteral>() { >> }.getType() ) ) ) ).toProvider( BProvider.class ); > > Not much I can really say - creating models for complex types code- > intensive. TypeLiteral works for some wildcards, and for the ones it > doesn't you can either use the Types factory class, or you can do > something reflective. > private final List unused = null; > private final TypeLiteral> type = fieldToType > (MyClass.class, unused); > > private static Type fieldToType(Class definedIn, String name) { > return definedIn.getDeclaredField(name).getGenericType(); > } > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Wildcards...
On Jun 6, 8:48 am, Johannes Schneider wrote: > bind( ( TypeLiteral> ) TypeLiteral.get( > Types.listOf( Types.subtypeOf( new TypeLiteral>() { > }.getType() ) ) ) ).toProvider( BProvider.class ); Not much I can really say - creating models for complex types code- intensive. TypeLiteral works for some wildcards, and for the ones it doesn't you can either use the Types factory class, or you can do something reflective. private final List unused = null; private final TypeLiteral> type = fieldToType (MyClass.class, unused); private static Type fieldToType(Class definedIn, String name) { return definedIn.getDeclaredField(name).getGenericType(); } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Wildcards...
one more time: public class GuiceListWildcardTest { @Test public void testIt() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind( ( TypeLiteral>> ) TypeLiteral.get( Types.listOf( Types.subtypeOf( new TypeLiteral>() { }.getType() ) ) ) ).toProvider( BProvider.class ); } } ); A a = injector.getInstance( A.class ); assertEquals( a.bs.size(), 2 ); } public static class A { private final List> bs; @Inject public A( @NotNull List> bs ) { this.bs = bs; } } public static class B { } public static class BProvider implements Provider>> { @Override public List> get() { return Arrays.>asList( new B(), new B() ); } } } Johannes Schneider wrote: > Improved a little (forgot wildcard at provider): > > public class GuiceListWildcardTest { > @Test > public void testIt() { > Injector injector = Guice.createInjector( new AbstractModule() { > @Override > protected void configure() { > bind( ( TypeLiteral> ) TypeLiteral.get( > Types.listOf( Types.subtypeOf( new TypeLiteral>() { > }.getType() ) ) ) ).toProvider( BProvider.class ); > } > } ); > > A a = injector.getInstance( A.class ); > assertEquals( a.bs.size(), 2 ); > } > > public static class A { > private final List> bs; > > @Inject > public A( @NotNull List> bs ) { > this.bs = bs; > } > } > > public static class B { > } > > public static class BProvider implements Provider>> { > @Override > public List> get() { > return Arrays.>asList( new B(), new B() ); > } > } > } > > > > > > Johannes Schneider wrote: >> Hi, >> >> I think someone at Google really understands Generics ;-). >> At the moment I try to understand how Guice manges those things. >> >> Therefore I have created a small sample that works. But I would like to >> have any feedback how that should/could be solved better: >> >> >> Regards, >> >> Johannes >> >> >> public class GuiceListWildcardTest { >> @Test >> public void testIt() { >> Injector injector = Guice.createInjector( new AbstractModule() { >> @Override >> protected void configure() { >> bind( ( TypeLiteral> ) TypeLiteral.get( >> Types.listOf( Types.subtypeOf( new TypeLiteral>() { >> }.getType() ) ) ) ).toProvider( BProvider.class ); >> } >> } ); >> >> A a = injector.getInstance( A.class ); >> assertEquals( a.bs.size(), 2 ); >> } >> >> public static class A { >> private final List> bs; >> >> @Inject >> public A( @NotNull List> bs ) { >> this.bs = bs; >> } >> } >> >> public static class B { >> } >> >> public static class BProvider implements Provider> { >> @Override >> public List get() { >> return Arrays.asList( new B(), new B() ); >> } >> } >> } >> >> > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Wildcards...
Improved a little (forgot wildcard at provider): public class GuiceListWildcardTest { @Test public void testIt() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind( ( TypeLiteral> ) TypeLiteral.get( Types.listOf( Types.subtypeOf( new TypeLiteral>() { }.getType() ) ) ) ).toProvider( BProvider.class ); } } ); A a = injector.getInstance( A.class ); assertEquals( a.bs.size(), 2 ); } public static class A { private final List> bs; @Inject public A( @NotNull List> bs ) { this.bs = bs; } } public static class B { } public static class BProvider implements Provider>> { @Override public List> get() { return Arrays.>asList( new B(), new B() ); } } } Johannes Schneider wrote: > Hi, > > I think someone at Google really understands Generics ;-). > At the moment I try to understand how Guice manges those things. > > Therefore I have created a small sample that works. But I would like to > have any feedback how that should/could be solved better: > > > Regards, > > Johannes > > > public class GuiceListWildcardTest { > @Test > public void testIt() { > Injector injector = Guice.createInjector( new AbstractModule() { > @Override > protected void configure() { > bind( ( TypeLiteral> ) TypeLiteral.get( > Types.listOf( Types.subtypeOf( new TypeLiteral>() { > }.getType() ) ) ) ).toProvider( BProvider.class ); > } > } ); > > A a = injector.getInstance( A.class ); > assertEquals( a.bs.size(), 2 ); > } > > public static class A { > private final List> bs; > > @Inject > public A( @NotNull List> bs ) { > this.bs = bs; > } > } > > public static class B { > } > > public static class BProvider implements Provider> { > @Override > public List get() { > return Arrays.asList( new B(), new B() ); > } > } > } > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Wildcards...
Hi, I think someone at Google really understands Generics ;-). At the moment I try to understand how Guice manges those things. Therefore I have created a small sample that works. But I would like to have any feedback how that should/could be solved better: Regards, Johannes public class GuiceListWildcardTest { @Test public void testIt() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind( ( TypeLiteral> ) TypeLiteral.get( Types.listOf( Types.subtypeOf( new TypeLiteral>() { }.getType() ) ) ) ).toProvider( BProvider.class ); } } ); A a = injector.getInstance( A.class ); assertEquals( a.bs.size(), 2 ); } public static class A { private final List> bs; @Inject public A( @NotNull List> bs ) { this.bs = bs; } } public static class B { } public static class BProvider implements Provider> { @Override public List get() { return Arrays.asList( new B(), new B() ); } } } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---