Michel T. wrote:
My first suggestion is not to use reflection.
I have to use reflection. This example is a very-reduced sample that
does not reflect the reality where this is required. I created this
simple class to show the warning.
If not, does BeanFactory<? extends Bean> work for you? (There is no
guarantee that the T in a subclass of BeanFactory is exactly Bean.)
I would not mind changing the return value of createFactory2 to
BeanFactory<? extends Bean> createFactory2(String beanClass)
but I get the same warning.
Also, I tried the following but it does not compile (thus it must not
make sense either):
public BeanFactory<? extends Bean> createFactory2(String beanClass)
throws Exception{
Class<?> bc = Class.forName(beanClass);
Class<? extends BeanFactory<? extends Bean>> bfc =
bc.asSubclass(BeanFactory.class);
Constructor<? extends BeanFactory<? extends Bean>> cstr =
bfc.getConstructor(new Class[]{});
return cstr.newInstance(new Object[]{});
}
Alternatively, create a bean factory factory. (But stick with using a
Constructor rather than evil Class.newInstance.)
I must use reflection to instantiate the factory. Otherwise, I would
not be posting that question ;)
--
Michel T.
circumstances, you don't need to use it, even if you *think* you do.
Moving forward, assuming that you do indeed need to do it this way...