尝试下面的代码,但它给出的结果为零(subTypesOf 大小为零)。
File projectFolder = new File("project_folder_some_path");
final Set<URL> urls = new HashSet<>();
Files.walkFileTree(projectFolder.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".class")) {
urls.add(file.toFile().toURI().toURL());
}
return super.visitFile(file, attrs);
}
});
URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[0]), getClass().getClassLoader());
ConfigurationBuilder configuration = new ConfigurationBuilder()
.addClassLoader(classLoader)
.addUrls(urls)
.setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner())
.filterInputsBy(new FilterBuilder().includePackage("com.comp"));
Reflections reflections = new Reflections(configuration);
Set<Class<?>> subTypesOf = (Set<Class<?>>) reflections.getSubTypesOf(classLoader.loadClass("com.comp.framework.bean.SuperNumeratorIdEntity"));
// subTypesOf size is zero
classLoader.close();
这里subTypesOf大小为零。但是类文件存在于扩展 com.comp.framework.bean.SuperNumeratorIdEntity 的项目文件夹中
我正在寻找的类如下所示: public class XyzEntity extends SuperNumeratorIdEntity