So I have something like the following in Java:
private List<SomeType>variable;
// ....variable is instantiated as so ...
variable = new ArrayList<SomeType>();
// there's also a getter
public List<SomeType> getVariable() { /* code */ }
What I would like to be able to do is figure out that variable
is a collection of SomeType
programmatically. I read here that I can determine that from the method getVariable()
but is there any way to tell directly from variable
?
I have been able to retrieve SomeType
from the getter method based on the information in the link. I have also been successful in retrieving all the fields of the surrounding class via SurroundingClass.getClass().getDeclaredFields()
but this doesn't tell me that it is List<SomeType>
.
EDIT: Based on bmargulies's response doing the following will achieve what I want:
Field[] fields = SurroundingClass.getDeclaredFields();
/* assuming it is in fields[0] and is a ParameterizedType */
ParameterizedType pt = (ParameterizedType) fields[0].getGenericType();
Type[] types = pt.getActualTypeArguments();
/* from types I'm able to see the information I've been looking for */