• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding the Generic Type at Runtime

 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6548
22
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would T.getName() work?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely, because of erasure, that is impossible?
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we are in agreement, yes.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic