aspose file tools*
The moose likes Java in General and the fly likes How to make method generic? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make method generic?" Watch "How to make method generic?" New topic
Author

How to make method generic?

Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 187
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
If you go through the Java Tutorials, you will find a section about generic methods.
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 187
Thanks a lot Steve and Campbell, it really help !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You’re welcome
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

@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

Joined: Apr 06, 2010
Posts: 4420
    
    8

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

Joined: Dec 16, 2010
Posts: 998
    
  13

There is an excellent FAQ on generics here.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make method generic?