aspose file tools*
The moose likes Java in General and the fly likes how to use java reflection to get element type of List ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to use java reflection to get element type of List ?" Watch "how to use java reflection to get element type of List ?" New topic
Author

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

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Thanks, Jim!
shwetank singh
Greenhorn

Joined: Apr 02, 2007
Posts: 26
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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().


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 53
Jim Thanks a lot !!! the example is very useful.


SCJP 1.5
shwetank singh
Greenhorn

Joined: Apr 02, 2007
Posts: 26
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!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Neha Sumungla
Greenhorn

Joined: Nov 15, 2011
Posts: 3
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....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch Neha Sumungla
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

shwetank singh,
Your post was moved to a new topic.
Chuck Zheng
Greenhorn

Joined: Apr 03, 2004
Posts: 5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use java reflection to get element type of List ?