Java Generics Issue (w/ Spring)
Posted
by drewzilla
on Stack Overflow
See other posts from Stack Overflow
or by drewzilla
Published on 2010-06-03T10:47:22Z
Indexed on
2010/06/03
11:44 UTC
Read the original article
Hit count: 183
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
© Stack Overflow or respective owner