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

Introspection of a instance variable type

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Building a really easy to implement and maintain framework using a good bit of introspection based on code conventions, but I've hit a roadblock.

public List<Type> getSearchResults() {
return searchResults;
}

is being called, and giving me a List<FooResult> in some cases, and a List<BarResult> in other cases, and quite a few more in the future.


If that list has one or more objects in it, I can do:
Class resultClass = getSearchResults().get(0).getClass();

And resultClass.getSimpleName() will return FooResult, for example.


If the list is empty - but not null - how do I introspect out the type and not just get "Type" back, or has that information been lost?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/generics/erasure.html
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the information about type may be lost. Java generics are applied at compile time only; once the compiler has finished checking that every "<T>" is a "<T>" and not a "<U>", all that information vanishes. Maybe if generics had been implemented in Java at the beginning, the information would have been maintained, but it isn't.

That's as far as I can get; not sure I understand the whole problem, but others might.
 
Campbell Ritchie
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Newton beat me by 5 seconds. Even Prime can't beat me by such a narrow margin.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately yes.

The type parameter in your List<Type> will be erased during compilation to just be a List.

This means that you cannot at runtime get hold of the type the List was declared to hold.

A couple of other interesting examples, if you have:

class MyClass<T>
{
T someVariable;
}

At compile time the type parameter <T> is erased and the usage of T in the variable declaration will be changed to Object.

class MyContainerClass<T extends Number>
{
T someVariable;
}

When this class is compiled the type parameter <T extends Number> is erased, and the usage of T is replace by Number.

 
Dean Jackson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:http://java.sun.com/docs/books/tutorial/java/generics/erasure.html



Thanks for all the responses; I really appreciate the help.
 
Campbell Ritchie
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic