Writing a generic FactoryModuleBuilder

372 views
Skip to first unread message

glenviewjeff

unread,
May 10, 2012, 12:08:17 PM5/10/12
to google...@googlegroups.com
I'd like to know how to represent a generic version of the following binding that won't fail with xxx cannot be used as a key it is not fully specified

install(new FactoryModuleBuilder(). implement(new TypeLiteral<SomeClass<T,?>>(){}, new TypeLiteral<SomeOtherClass<T, U>>(){}). build(new TypeLiteral<SomeFactory<T>>(){}));


It seems that the implement method isn't available with a parameterized type parameter.

Thanks,
Jeff

Alen Vrečko

unread,
May 10, 2012, 12:47:08 PM5/10/12
to google-guice
Assisted inject must figure out the real class. It can't figure it out
from a parameterized type parameter.

If you want to save some keystrokes you can go with a custom "utility"
like:

useAssisedInjectFor(Foo.class).using(BarFactory.class);
useAssisedInjectFor(Foo.class).generifiedWith(Baz.class).using(BarFactory.class);

or similar. But I doubt this will give much added value.

Cheers
Alen

glenviewjeff

unread,
May 10, 2012, 1:41:44 PM5/10/12
to google...@googlegroups.com
If doable, this would sure be more readable than the ten copies of the concrete code complete with long-named types duplicated three times in each copy.  How would one write a custom utility like you mentioned?  Aren't you just re-posing the question implemented as a method chain?  Or does using a method chain somehow help solve the problem in a way that I'm missing?

Alen Vrečko

unread,
May 10, 2012, 4:53:04 PM5/10/12
to google-guice
Imho the best you can do with generics is something like

install(new Assist2<Payment, RealPayment>(PaymentFactory.class){});
or

install(new Assist3<SomeClass<Foo, Bar>, SomeOtherClass<Foo, Bar>,
SomeFactory<Foo>(){});

Should be pretty easy to implement this approach. Just look in the
source of TypeLiteral.

If you absolutely need type parameters something like

install(new CustomAssist<Baz>(){}); where

CustomAssist<T> extends
Assist<SomeClass<Foo,T>,SomeOtherClass<Foo,Bar>{...

might be possible but not trivial to implement. But a lot of fun.

Cheers
Alen
Reply all
Reply to author
Forward
0 new messages