在代码下面尝试过,但它给出的结果为零(subTypesOf size为零(。
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();
这里子类型的大小为零。但是类文件存在于扩展com.comp.framework.bean.SuperNumeratorIdEntity的项目文件夹中
我正在寻找的类如下所示:公共类 XyzEntity 扩展 SuperNumeratorIdEntity
类是否在典型的文件夹层次结构中? 那么,您的 URL 应该引用该文件夹,而不是其中的每个.class文件。并以/
结尾,以提供 java 信息,它应该在这里需要一个文件夹:
URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL("file:///c:/projects/myProject/bin/")}, getClass().getClassLoader());