public class Parent {}
public class A extends Parent {}
public class B {}
public class Test {
private List<A> aList;
private List<B> bList;
}
I was playing around with reflection and generics and created these classes just for fun.
Now I want to print the names of variables in the Test
class which are of type List<T extends Parent>
. So I want to print only "aList"
because A
extends Parent
, while B
does not.
So far I have been able to get the List
variables. I looked at various methods of reflection but could not extract the type of List <...>
and see if its superclass is Parent
or not. Here is my code which currently prints both "aList"
and "bList"
:-
public static void main(String[] args) {
Arrays.stream(Test.class.getDeclaredFields())
.filter(field -> field.getType().equals(List.class)) // && fieldActualTypeExtendsA
.forEach(field -> System.out.println(field.getName()));
}
What should I add in the filter?
Aucun commentaire:
Enregistrer un commentaire