是否可以使用反射找到包中的所有类?是否有可能在给定的包中找到所有的类或接口?(快速查看。Package,这似乎是不可能的。)
4 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
Reflections reflections = new Reflections("my.project.prefix"); Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);
长风秋雁
TA贡献1757条经验 获得超7个赞
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>(); classLoadersList.add(ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.your.package"))));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
开满天机
TA贡献1786条经验 获得超13个赞
ClassPath
getTopLevelClasses()
getTopLevelClasses(String packageName)
getTopLevelClassesRecursive(String packageName)
ClassPath
添加回答
举报
0/150
提交
取消