Once a Java class is compiled, any generic information is lost. Meaning that an ArrayList of Cars at runtime becomes an ArrayList and the fact that it stores Cars is lost.
There is a way around it, if you have access to the class or object that contains that ArrayList.
Lets take a simple domain object that contains a list :
There is a way around it, if you have access to the class or object that contains that ArrayList.
Lets take a simple domain object that contains a list :
public class Domain { private ListmyList = new ArrayList (); // Access methods removed }
public class TypeInspector { public void inspect(Class clazz) { for(Field field : clazz.getDeclaredFields()) { if(isCollection(field)) { inspectType(field); } } } private void inspectType(Field field) { String rawType = field.getGenericType().toString(); rawType = rawType.substring(rawType.indexOf('<') + 1); rawType = rawType.substring(0, rawType.length() - 1); try { System.out.println(Class.forName(rawType)); } catch (ClassNotFoundException e) { System.out.println("Couldnt find "+ rawType); } } private boolean isCollection(Field field) { return Collection.class.isAssignableFrom(field.getType()); } }
1 comment:
Huh?? What's wrong with:
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
/// stuff hiree
}
Post a Comment