我有一个特定的目标类型(在运行时决定),以及一个可迭代的类,我正在与它进行比较.我正在尝试编写一个检查类的泛型参数的方法,以查看它是否是可以迭代我的目标类型的子类.例子:
Class<?> X = SomeObject.class;
matches(X, new ArrayList<SomeObject>()) -> true
matches(X, new ArrayList<SubclassOfSomeObject>()) -> true
matches(X, new ArrayList<SomeOtherObject>()) -> false
matches(X, new ArrayList()) -> true (I think?)
matches(X, new Iterable<SomeObject>() { ... }) -> true
matches(X, new ListOfSomeObjects()) -> true
(where ListOfSomeObjects extends Iterable<SomeObject>)
Run Code Online (Sandbox Code Playgroud)