• 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

How java reflection will handle the genericReturnType List<List<List<...>>>?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In java reflection, when we want to get the generic return type there is a method getGenericReturnType, but it supports only for ex. List<String>.
But when I want to get and parse the return type like this List<List<String>>, how to do?

I tried this , it throws classcastexception.

Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl

Thanks in Advance
Gomathi
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of type erasure, all generic information is lost at runtime. This means that at runtime, something like List<List<String>> does not exist - it's simply List.
 
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
gomathi jayakumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Because of type erasure, all generic information is lost at runtime. This means that at runtime, something like List<List<String>> does not exist - it's simply List.




Hi Rob Prime,
Thanks. If that is the case then how can we handle these kind of return types(List<List><String>>)?. Please clarify.
 
Campbell Ritchie
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics are available at compile time only. That tries to ensure that you don't make any mistakes while programming. Like I said, at runtime it is perfectly legal. For instance:
When you remove the generics, or force the object into a wrong generic type as I did above, it's still possible to add elements of the wrong type*. That's because at runtime it's nothing more than a List which holds plain old Object references. Only when you retrieve the elements as the original type will it throw a ClassCastException, because it's the wrong type.

Reflection is used to inspect classes etc at runtime, and since the generics have been removed they are no longer available.


* The Collections.checkedXXX methods can help you out if you need this type of safety when adding elements.
 
Campbell Ritchie
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note there seems to be something causing a < to appear as >< in two posts in this thread. I have seen it before.
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's caused by the forum software, and you can prevent it by using &lt; instead of < - that's the HTML escape sequence for it.
 
Well THAT's new! Comfort me, reliable tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic