Retrieving the type of a Collection

Posted by nevets1219 on Stack Overflow See other posts from Stack Overflow or by nevets1219
Published on 2010-03-26T21:49:23Z Indexed on 2010/03/26 21:53 UTC
Read the original article Hit count: 300

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about collections