Java Reflection - Generics
Jakob Jenkov |
I have often read in articles and forums that all Java Generics information is erased at compile time so that you cannot access any of that information at runtime. This is not entirely true though. It is possible to access generics information at runtime in a handful of cases. These cases actually cover several of our needs for Java Generics information. This text explains these cases.
The Generics Reflection Rule of Thumb
Using Java Generics typically falls into one of two different situations:
- Declaring a class/interface as being parameterizable.
- Using a parameterizable class.
When you write a class or interface you can specify that it should
be paramerizable. This is the case with the java.util.List
interface. Rather than create a list of Object
you can
parameterize java.util.List
to create a list of say
String
, like this:
List<String> myList = new ArrayList<String>();
When inspecting a parameterizable type itself at runtime via reflection, like java.util.List
,
there is no way of knowing what type is has been parameterized to. The object itself
does not know what it was parameterized to.
However, the reference to the object knows what type including generic type it is referencing. That is, if it is not a local variable. If an object is referenced by a field in an object, then you can look at the Field declaration via reflection, to obtain information about the generic type declared by that field.
The same is possible if the object is referenced by a parameter in a method. Via the Parameter
object for that method (a Java reflection object) you can see what generic type that parameter is declared to.
Finally, you can also look at the return type of a method to see what generic type it is declared to. Again, you cannot see it from the actual object returned. You need to look at the method declaration via reflection to see what return type (including generic type) it declares.
To sum it up: You can only see from the declarations of references (fields, parameters, return types) what generic type an object referenced by these references would have. You cannot see it from the object itself.
The following sections take a closer look at these situations.
Generic Method Return Types
If you have obtained a java.lang.reflect.Method
object it is possible to
obtain information about its generic return type.
You can read how to obtain Method
objects in the text "Java Generics: Methods".
Here is an example class with a method having a parameterized return type:
public class MyClass { protected List<String> stringList = ...; public List<String> getStringList(){ return this.stringList; } }
In this class it is possible to obtain the generic return type of the getStringList()
method. In other words, it is possible to detect that getStringList()
returns a
List<String>
and not just a List
. Here is how:
Method method = MyClass.class.getMethod("getStringList", null); Type returnType = method.getGenericReturnType(); if(returnType instanceof ParameterizedType){ ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); for(Type typeArgument : typeArguments){ Class typeArgClass = (Class) typeArgument; System.out.println("typeArgClass = " + typeArgClass); } }
This piece of code will print out the text "typeArgClass = java.lang.String". The
Type[]
array typeArguments
array will contain one
item - a Class
instance representing the class java.lang.String
.
Class
implements the Type
interface.
Generic Method Parameter Types
You can also access the generic types of parameter types at runtime via Java Reflection.
Here is an example class with a method taking a parameterized List
as parameter:
public class MyClass { protected List<String> stringList = ...; public void setStringList(List<String> list){ this.stringList = list; } }
You can access the generic parameter types of the method parameters like this:
method = Myclass.class.getMethod("setStringList", List.class); Type[] genericParameterTypes = method.getGenericParameterTypes(); for(Type genericParameterType : genericParameterTypes){ if(genericParameterType instanceof ParameterizedType){ ParameterizedType aType = (ParameterizedType) genericParameterType; Type[] parameterArgTypes = aType.getActualTypeArguments(); for(Type parameterArgType : parameterArgTypes){ Class parameterArgClass = (Class) parameterArgType; System.out.println("parameterArgClass = " + parameterArgClass); } } }
This code will print out the text "parameterArgType = java.lang.String".
The Type[]
array parameterArgTypes
array will contain one
item - a Class
instance representing the class java.lang.String
.
Class
implements the Type
interface.
Generic Field Types
It is also possible to access the generic types of public fields. Fields are class
member variables - either static or instance variables. You can read about
obtaining Field
objects in the text "Java Generics: Fields".
Here is the example from earlier, with an instance field called stringList
.
public class MyClass { public List<String> stringList = ...; }
Field field = MyClass.class.getField("stringList"); Type genericFieldType = field.getGenericType(); if(genericFieldType instanceof ParameterizedType){ ParameterizedType aType = (ParameterizedType) genericFieldType; Type[] fieldArgTypes = aType.getActualTypeArguments(); for(Type fieldArgType : fieldArgTypes){ Class fieldArgClass = (Class) fieldArgType; System.out.println("fieldArgClass = " + fieldArgClass); } }
This code will print out the text "fieldArgClass = java.lang.String".
The Type[]
array fieldArgTypes
array will contain one
item - a Class
instance representing the class java.lang.String
.
Class
implements the Type
interface.
Tweet | |
Jakob Jenkov |