Thanks for the note, Jesse.
I am afraid I haven't been able to get it working ;-(
I trolled thru the tests (FactoryProviderTest in particular) and
included a quick JUnit test in FactoryProviderTest itself that mimics
my scenario, modeled on 'testGenericAssistedFactory'.
Guice complains of 'CreationException' when the test is run - I'd
appreciate if someone can have a look and point out if there's a gap
in my understanding of AssistedInject.
BTW, I get two different errors when I use @Inject and @AssistedInject
on the constructor. I believe Guice 2.0 indicate @AssistedInject is
@deprecated and @Inject should be used instead - please clarify.
Thanks heaps,
Rahul
----------------------------
class CarDealer<T extends Car> {
private Class<T> klass;
@AssistedInject
public CarDealer(@Assisted Class<T> klass) {
super();
this.klass = klass;
}
public T buyCar() throws Exception {
return klass.newInstance();
}
}
interface CarDealerFactory<T extends Car> {
CarDealer<T> create(Class<T> klass);
}
public void testCarDealerFactory() throws Exception{
final TypeLiteral<CarDealerFactory<Mustang>> mustangTypeLiteral
= new TypeLiteral<CarDealerFactory<Mustang>>() {};
final TypeLiteral<CarDealerFactory<Camaro>> camaroTypeLiteral
= new TypeLiteral<CarDealerFactory<Camaro>>() {};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(mustangTypeLiteral).toProvider(
FactoryProvider.newFactory(mustangTypeLiteral,
TypeLiteral.get(Mustang.class)));
bind(camaroTypeLiteral).toProvider(
FactoryProvider.newFactory(camaroTypeLiteral,
TypeLiteral.get(Camaro.class)));
}
});
CarDealerFactory<Mustang> mustangFactory
= injector.getInstance(Key.get(mustangTypeLiteral));
CarDealerFactory<Camaro> camaroFactory
= injector.getInstance(Key.get(camaroTypeLiteral));
CarDealer<Mustang> mustangCarDealer = mustangFactory.create
(Mustang.class);
assertEquals(Mustang.class, mustangCarDealer.buyCar().getClass
());
CarDealer<Camaro> camaroCarDealer = camaroFactory.create
(Camaro.class);
assertEquals(Camaro.class, camaroCarDealer.buyCar().getClass
());
}
---------------------------
On May 16, 9:21 pm, "[email protected]" <[email protected]> wrote:
> In the latest snapshot,assistedinjectsupports parameterized
> factories. You'll need to add this interface to your code:
> interface EclipseLinkStoreFactory<T> {
> EclipseLinkStore<T> create(Class<T> clazz);
> }
> And then use FactoryProvider to create the binding. See theassistedinjectdocs
> for more info.
> http://code.google.com/p/google-guice/wiki/AssistedInject
>
> Cheers,
> Jesse
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---