• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to retrieve type of Generic object?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to retrieve the type of a Generic object. Code like this:



...doesn't work. What is the magic formula for retrieving the type in use for a generic object off of the "this" keyword?

(I'd ideally like to construct a method that returns a string stating the type in use...Integer, float, etc.)

~~Tom
 
Saloon Keeper
Posts: 14260
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getComponentType works only for arrays.

I believe there is no way to get the generic parameter, because of type erasure.
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: . . . I believe there is no way to get the generic parameter, because of type erasure.

I think you are correct.
 
Sheriff
Posts: 3042
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, for compatibility reasons, generics are really only a compile-time check. By run-time, it's as if you didn't use them at all. That works fine most of the time, and it allowed generics to be added to the Java Language Specification without making big changes to the byte code. Sometimes though, it's a really pain in the butt. You could hack together a solution to your problem by storing the class you need as a member field.
 
Tom Brodhead
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I puzzled over this far longer than I should have, but it's not my nature to give up. And I've found a solution. There's one sacrifice: you *must* instantiate your class as an anonymous class; by doing so, type erasure doesn't happen. Then you may add this method to your generic class and you'll have access to the type.

If anyone can see a way to simplify this (especially with regards to getting the actual short name of the type from the longer, complete class name), I'd be very interested:



 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic