• 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

how to use java reflection to get element type of List ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to get element type for a list ?

for example
java.util.List<data.People> peopleList;

how to use java refection to get a List element type: data.People ?

Thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think this can be done, because generic types apply only at compile time. At runtime, a List is simply a List.

See Generics - Type Erasure...

For instance, Box<String> is translated [at compile time] to type Box, which is called the raw type -- a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime.


[ September 20, 2007: Message edited by: marc weber ]
 
Wanderer
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ermmm... actually this can be done. The class file contains info about the generic types of its member variables - that info is necessary if/when you try to compile another class using classes in a jar file, for example. That info is also accessible via reflection. But only generics-related info is only available for the class in general - not for specific instances of the class. To make a concrete example:

As you see, you can get info about the declared type of instance variable fooList. You can learn that it's a List<? extends Foo>. However, if you try to examine an actual instance that is referenced by that variable, the most you can learn is that it's (in this case) an ArrayList. You can't find out whether it's an ArrayList<Foo> or an ArrayList<Bar> or an ArrayList<? extends Bar>. That specific info doesn't exist for runtime instances of a generic class. It exists for the declared type of a field, or of a method parameter or return value - but not for actual instances of the objects that are represented or referenced by those fields, parameters or return values.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jim!
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well..may be i am digging something very old here...but i want an answer for something similar available for java 1.4..or does anything similar exist?..
as for this..what's the reason for null ownerType when the program is run?

type: java.util.List<? extends GenericTypeInfo$Foo>
raw type: interface java.util.List
owner type: null
actual type args:
? extends GenericTypeInfo$Foo

obj: []
obj class: class java.util.ArrayList
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shwetank singh wrote:but i want an answer for something similar available for java 1.4..or does anything similar exist?..


No, because generics did not exist at all in Java 1.4.

shwetank singh wrote:as for this..what's the reason for null ownerType when the program is run?


Because the class that's being examined doesn't have an owner type. Lookup the API documentation of java.lang.reflect.ParameterizedType.getOwnerType().
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim Thanks a lot !!! the example is very useful.
 
shwetank singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper..and Jim..for wonderful insight!
perhaps i could seek your comments on something similar..

..using reflection (java.lang.reflect) in JDK1.4, am trying to create a generic function to create a hibernate Criteria {this is not going to be a hibernate question }..the task is so organized that i've to write a very generic function that anyone can call and use by passing a reference of class {please read "object reference"} whose criteria has to be created.
now the question, i've an object of type A that has a List..the List contains objects of type B, C..etc. i wish to find out their type (of B,C,etc..) (similar to OwnerType here) at runtime.

do we have any way to make that possible in java 1.4?..to add to this, i can't use instanceOf test as the component in which this utility goes has no other knowledge than the Object being passed to it.

..again, please let me know if this should be posted as a new topic..am posting here for some inquisitive minds who may be looking out for this solution in java 1.4

much thanks!
 
Sheriff
Posts: 22783
131
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
In Java 1.4 all you can do is make a guess. You loop through all the elements and find out a common super class (worst case this will be java.lang.Object) or a number of common interfaces (0 or more). The common super class for any two classes X and Y is easy:
- if X is a direct or indirect super class of Y then it's X
- if Y is a direct or indirect super class of X then it's Y
- otherwise it's the common super class of X.getSuperclass() and Y.getSuperclass()

For interfaces it's also not that hard. You need to collect all interfaces a class implements using Class.getInterfaces() (don't forget the super classes!). Collect these in a Set. Do this for all elements, each time using Collection.retainAll. In pseudo code:
Of course you can get multiple results, so you would have to take a pick.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solutions provided are good. I could also suggest that instead trying to get it from the List itself, check the List for null and obtain the first element if that's not the case.

Then depending on the instance returned you can use

if (List.get(0) instanceof MyCustomClass)

OR

if (superclass.class.isAssignableFrom(List.get(0).getClass() ))

OR

if (XYZ.class.equals(List.get(0).getClass() ))

Good Luck....
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Neha Sumungla
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shwetank singh,
Your post was moved to a new topic.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe there are some magic with "Field", I cannot get it working for a local variable (or parameter). for this code:



I get:


see the "class" it has already erased info about String.

Does anyone know another solution or I did something wrong?

Thanks
Chuck


Jim Yingst wrote:Ermmm... actually this can be done. The class file contains info about the generic types of its member variables - that info is necessary if/when you try to compile another class using classes in a jar file, for example. That info is also accessible via reflection. But only generics-related info is only available for the class in general - not for specific instances of the class. To make a concrete example:

As you see, you can get info about the declared type of instance variable fooList. You can learn that it's a List<? extends Foo>. However, if you try to examine an actual instance that is referenced by that variable, the most you can learn is that it's (in this case) an ArrayList. You can't find out whether it's an ArrayList<Foo> or an ArrayList<Bar> or an ArrayList<? extends Bar>. That specific info doesn't exist for runtime instances of a generic class. It exists for the declared type of a field, or of a method parameter or return value - but not for actual instances of the objects that are represented or referenced by those fields, parameters or return values.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic