0

尝试下面的代码,但它给出的结果为零(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

4

1 回答 1

0

类在典型的文件夹层次结构中吗?那么您的 URL 应该引用该文件夹,而不是其中的每个 .class 文件。最后/给出java信息,它应该在这里有一个文件夹:

URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL("file:///c:/projects/myProject/bin/")}, getClass().getClassLoader());
于 2020-01-17T10:15:27.333 回答