• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding the Generic Type at Runtime

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would T.getName() work?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely, because of erasure, that is impossible?
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all!
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we are in agreement, yes.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic