• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make method generic?

 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guru,

May i know how i can make the method be more general by accept runtime type and return the list of runtime type? i try following but seem not work



Thanks for enlightenment!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through the Java Tutorials, you will find a section about generic methods.
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Steve and Campbell, it really help !
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston

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?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Jha wrote: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>.

But with the class argument, you don't need to cast. It's compile-time type safe.

The class argument is there simply to specify T because it can't be derived from other arguments.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an excellent FAQ on generics here.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston


Matthew Brown wrote:But with the class argument, you don't need to cast. It's compile-time type safe.

The class argument is there simply to specify T because it can't be derived from other arguments.


My confusion is because the following code compiles and runs fine. It does generate a warning, and correctly so, but if the list is indeed of the correct type, it works well.

I didn't have to pass a generic class parameter here. I'm sure context.getBean() will also work fine.

I think we need a generic class parameter only when we need to perform something like typeParam.isInstance(obj) or similar with the generic class parameter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic