tags:

views:

809

answers:

7

I have;

List<String> stringList = new ArrayList<String>();
List<Integer> integerList = new ArrayList<Integer>();

Is there a (easy) way to retrieve the generic type of the list?

+6  A: 

Short answer: no.

This is probably a duplicate, can't find an appropriate one right now.

Java uses something called type erasure, which means at runtime both objects are equivalent. The compiler knows the lists contain integers or strings, and as such can maintain a type safe environment. This information is lost (on an object instance basis) at runtime, and the list only contain 'Objects'.

You CAN find out a little about the class, what types it might be parametrized by, but normally this is just anything that extends "Object", i.e. anything. If you define a type as

class <A extends MyClass> AClass {....}

AClass.class will only contain the fact that the parameter A is bounded by MyClass, but more than that, there's no way to tell.

roe
+3  A: 

The type is erased so you will not be able to. See http://en.wikipedia.org/wiki/Type%5Ferasure and http://en.wikipedia.org/wiki/Generics%5Fin%5FJava#Type%5Ferasure

peter.murray.rust
+2  A: 

As others have said, the only correct answer is no, the type has been erased.

If the list has a non-zero number of elements, you could investigate the type of the first element ( using it's getClass method, for instance ). That won't tell you the generic type of the list, but it would be reasonable to assume that the generic type was some superclass of the types in the list.

I wouldn't advocate the approach, but in a bind it might be useful.

Chris Arguin
+2  A: 

Generally impossible, because List<String> and List<Integer> share the same runtime class.

You might be able to reflect on the declared type of the field holding the list, though (if the declared type does not itself refer to a type parameter whose value you don't know).

meriton
+7  A: 

If those are actually fields of a certian class, then you can get them with a little help of reflection:

package test;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

public class Test {

    List<String> stringList = new ArrayList<String>();
    List<Integer> integerList = new ArrayList<Integer>();

    public static void main(String... args) throws Exception {
        Field stringListField = Test.class.getDeclaredField("stringList");
        ParameterizedType stringListType = (ParameterizedType) stringListField.getGenericType();
        Class<?> stringListClass = (Class<?>) stringListType.getActualTypeArguments()[0];
        System.out.println(stringListClass); // class java.lang.String.

        Field integerListField = Test.class.getDeclaredField("integerList");
        ParameterizedType integerListType = (ParameterizedType) integerListField.getGenericType();
        Class<?> integerListClass = (Class<?>) integerListType.getActualTypeArguments()[0];
        System.out.println(integerListClass); // class java.lang.Integer.
    }
}

You can also do that for parameter types and return type of methods.

But if they're inside the same scope of the class/method where you need to know about them, then there's no point of knowing them, because you already have declared them yourself.

BalusC
while this is definitely an interesting exercise in generics, if you now what field you want, you usually know what type it is.. :) Determining the type of a parameter would've been a lot more useful I suppose. Good when making injection frameworks or similar though. +1
roe
There are certainly situations where this is above useful. In for example configurationless ORM frameworks.
BalusC
..which can use Class#getDeclaredFields() to get all fields without the need to know the field name.
BalusC
BalusC: That sounds like an injection framework to me.. That's the kind of use I meant anyway.
roe
+2  A: 

At runtime, no, you can't.

However via reflection the type parameters are accessible. Try

for(Field field : this.getDeclaredFields()) {
    System.out.println(field.getGenericType())
}

The method getGenericType() returns a Type object. In this case, it will be an instance of ParametrizedType, which in turn has methods getRawType() (which will contain List.class, in this case) and getActualTypeArguments(), which will return an array (in this case, of length one, containing either String.class or Integer.class).

Scott Morrison
+1  A: 

Solved:

((ParameterizedType) field.getGenericType()).getActualTypeArguments()
MrThys
Why is the answer at the bottom? (Silly java programmers. :-P
Crazy Chenz
Probably because it's the same answer (much abbreviated) as @BalusC gave months earlier, and the other has more info.
Chadwick
No, because it's the questioner himself. I however wonder why my answer wasn't accepted since it basically contains the same answer. This is at least not the ideology of Stackoverflow :/ Maybe MrThys was just new to the site at the time and didn't understood how it all is supposed to work.
BalusC