I’m playing around with Java’s reflection API and trying to handle some fields. Now I’m stuck with identifying the type of my fields. Strings are easy, just do myField.getType().equals(String.class). The same applies for other non-derived classes. But how do I check derived classes? E.g. LinkedList as subclass of List. I can’t find any isSubclassOf(...) or extends(...) method. Do I need to walk through all getSuperClass() and find my supeclass by my own?
I’m playing around with Java’s reflection API and trying to handle some fields. Now
Share
You want this method:
where in general,
List(above) should be replaced withsuperclassandmyClassshould be replaced withsubclassFrom the JavaDoc:
Reference:
Class.isAssignableFrom(Class)Related:
a) Check if an Object is an instance of a Class or Interface (including subclasses) you know at compile time:
Example:
b) Check if an Object is an instance of a Class or Interface (including subclasses) you only know at runtime:
Example: