jeudi 16 janvier 2020

How to get child classes with prefix packages using reflection classloading from deployment folder in Java?

Tried below code but it's giving zero results (subTypesOf size is zero).

 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();

here subTypesOf size is zero. But the class files are present in project folder which extends com.comp.framework.bean.SuperNumeratorIdEntity

The class i am looking for looks like this: public class XyzEntity extends SuperNumeratorIdEntity





Aucun commentaire:

Enregistrer un commentaire