Well, first you have to add a <T> to the method signature to signal this is a generic method for the type <T>:

But there is still a problem here - the run time has to be able to deduce the type of T at run-time, and must be able to do so from something in the method signature. One way of doing this might be to pass in the Class Object for the type you want. For example:

So if you wanted to get a List<Integer> back you would do
(or something like that, untested but general idea)

Steve Luke wrote:But there is still a problem here - the run time has to be able to deduce the type of T at run-time, and must be able to do so from something in the method signature. One way of doing this might be to pass in the Class Object for the type you want.

That's not necessarily a problem per se.

You are right that one way to do this is to include a Class<T> parameter. However, it's not strictly needed. One can have the method without the Class<T> argument, and still be able to call like: List<String> list = MyBeanFactory.<String>doReturnResourceBean(beanName, configFileName); or even List<String> list = doReturnResourceBean(beanName, configFileName);.

Aditya Jha wrote:You are right that one way to do this is to include a Class<T> parameter. However, it's not strictly needed...

Actually, if Nakataa wants to return a List with a generic runtime type, it is. The problem as I see it, is how to determine what that is. Specifically:
context.getBean(beanName) needs to be runtime castable to a List<T>, and the only way I can see of ensuring that is to pass the Class, as was mentioned above.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

Pardon my understanding. Could you please explain what difference a class parameter brings in? More specifically, what do we do with that parameter in the method? As for casting, IMHO even without the argument, we can cast a member of a non-generic list into T, or the whole non-generic list into List<T>. Am I missing something?

Winston Gutkowski wrote:Actually, if Nakataa wants to return a List with a generic runtime type, it is. The problem as I see it, is how to determine what that is. Specifically:
context.getBean(beanName) needs to be runtime castable to a List<T>, and the only way I can see of ensuring that is to pass the Class, as was mentioned above.