2

I am trying to use the Reflections library to obtain all the subclasses of a particular class. I need to filter the search to only the subclasses in a particular classpath url. This is the code I am using:

ConfigurationBuilder config = new ConfigurationBuilder();
config.addUrls(<my_url>);
Reflections reflections = new Reflections(config);
Set<Class<? extends A>> set = reflections.getSubTypesOf(A.class)

My problem is the following: Let's assume that class B extends A, and class C extends B. So we have:

A <|-- B <|-- C

A and B are in a classpath URL different than the one of C. And the url given to reflections is the one of C. Then I would expect C to be in the returned set, and B should not be present since it is in another classpath url.

However, the returned set is empty. C is not in the set of classes returned by reflections, though it is a transitive descendant of A. If I move B to the same classpath URL of C, then both B and C are returned. Then I guess the problem is that Reflections by default does not answer (transitive) subclasses if any of the classes in the middle of the hierarchy are not in the filtered url.

My questions is: How can I instruct reflections to answer (transitive) subclasses of a particular class, even if certain classes in the middle of the hierarchy are not in the filtered url (but the subclass itself IS in the filtered url)?.

4

1 回答 1

0

关键是传递性无法发现是否没有扫描所有相关的 url。你应该知道你的类路径。

如果您不知道所有涉及的网址,您可以扫描所有网址,使用

ClasspathHelper.forClassLoader()
于 2013-03-10T07:54:59.233 回答