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?
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?
If those are actually fields of a certain 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.
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.
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).
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).
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.
A tiny helper method for that:
/**
* Get type of collection field.
*
* @param aClass A class containing collection.
* @param collectionName A collection field name.
*/
@SneakyThrows
public static Class<?> getCollectionType(Class<?> aClass, String collectionName) {
Field field = aClass.getDeclaredField(collectionName);
ParameterizedType genericType = (ParameterizedType) field.getGenericType();
return (Class<?>) genericType.getActualTypeArguments()[0];
}
I found an answer that works perfectly in my case.
class ArrayListString extends ArrayList<String> {}
class ArrayListInteger extends ArrayList<Integer> {}
ArrayListString als = new ArrayListString();
ArrayListInteger ali = new ArrayListInteger();
void whatType(Object a) {
if (a instanceof ArrayListString)
System.out.println("ArrayListString");
else if (a instanceof ArrayListInteger)
System.out.println("ArrayListInteger");
}
whatType(als);
whatType(ali);
als and ali will work as their generics do.
What this code does is convert a generic type into a regular Java type. In Java, regular types can be detected with instanceof.
Use Reflection to get the Field for these then you can just do: field.genericType to get the type that contains the information about generic as well.