aspose file tools*
The moose likes Java in General and the fly likes Finding the Generic Type at Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding the Generic Type at Runtime" Watch "Finding the Generic Type at Runtime" New topic
Author

Finding the Generic Type at Runtime

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I am trying to print the generic type of the class in a method, but so far failed. Below is my code and the commented part is where I am trying to print T's type.



As a side note, if I insert a constructor to get T as input parameter, then I can print it like below.

Is there a way to print the type of T in the someMethod()? Also I tried to mark the method static, and tried T.class.getName(), but that's not possible.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Would T.getName() work?


Junilu - [How to Ask Questions] [How to Answer Questions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Surely, because of erasure, that is impossible?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Campbell Ritchie wrote:Surely, because of erasure, that is impossible?

Nearly everything that is wrong or excessively complex with generics is cause by erasure.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

Junilu Lacar wrote:Would T.getName() work?


In theory, T.getClass().getName() will get you the name of the class -- however, that class is not necessarly the T type of the generic. For example, it is possible to put a Thread object in an ArrayList<Runnable>; getName() will get you "Thread", so you only know that the generic contain, at least one Thread object. You don't know what the T type of the colleciton is.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

John Jai wrote:As a side note, if I insert a constructor to get T as input parameter, then I can print it like below.


But that requires you to construct and pass an object of type T, just so the Generic code can know what T is. A simpler way is to just pass T's class:



Then you create a Generic object like this:


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks all!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Pat Farrell wrote: . . . Nearly everything that is wrong or excessively complex with generics is cause by erasure.
Not quite. It was caused by generics not being introduced in JDK1.0. Erasure was one of the baleful things caused by generics not being introduced from the start, because then they could have used reification for generics. Erasure is a result, not a cause. And all the other things you are thinking of, PF, they are secondary results. Secondary to erasure, yes.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Campbell Ritchie wrote:
Pat Farrell wrote: . . . Nearly everything that is wrong or excessively complex with generics is cause by erasure.
Not quite. It was caused by generics not being introduced in JDK1.0. Erasure was one of the baleful things caused by generics not being introduced from the start, because then they could have used reification for generics. Erasure is a result, not a cause. And all the other things you are thinking of, PF, they are secondary results. Secondary to erasure, yes.

We are probably agreeing. Since generics were not in 1.0000, they had to have erasure for backward compatibility.

Once can argue that backward compatibility is over rated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
I think we are in agreement, yes.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Paul Clapham wrote:A simpler way is to just pass T's class:


There's another approach which was suggested to me by BenSchulz on the Oracle forums: Any way to get the Class from a generic Type?


luck, db
There are no new questions, but there may be new answers.
 
jQuery in Action, 2nd edition
 
subject: Finding the Generic Type at Runtime