Java Generics Issue (w/ Spring)
- by drewzilla
I think I may be a victim of type erasure but thought I'd check with others here first.
I have the requirement to do something like this:
public interface FooFactory {
public <T extends Bar> Foo<T> createFoo( Class<T> clazz );
}
It is perfectly valid to write this code. However, I'm trying to implement this functionality using a Spring BeanFactory and I can't do it.
What I'd like to do is this...
public class FooFactoryImpl implements BeanFactoryAware {
private BeanFactory beanFactory;
public <T extends Bar> Foo<T> createFoo( Class<T> clazz ) {
return beanFactory.getBean( ????????? );
}
public void setBeanFactory( BeanFactory beanFactory ) {
this.beanFactory = beanFactory;
}
}
As you can see, I've put in ???????? where I'd like to retrieve a bean of type Foo<T>, where T extends Bar. However, it is not possible to derive a Class object of type Foo<T> and so I assume what I'm trying to do is impossible?
Anyone else see a way round this or an alternative way of implementing what I'm trying to do?
Thanks,
Andrew