• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
gomathi jayakumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!