【发布时间】:2017-03-23 04:54:30
【问题描述】:
我正在使用下面的代码来查找给定包名称中的所有类。当代码直接放入我的项目时,这工作正常。但是,从我放在一起的 Commons Jar 调用服务不会从我的项目中返回数据。这可以实现吗?我正在使用 org.reflection.Reflections 库。
public Set<Class<?>> reflectPackage(String packageName){
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(
new ConfigurationBuilder().setScanners(
new SubTypesScanner(false),
new ResourcesScanner()).setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(
new ClassLoader[0]))).filterInputsBy(
new FilterBuilder().include(FilterBuilder.prefix(packageName))));
return reflections.getSubTypesOf(Object.class);
}
Project Structure
Eclipse --- Security Base // Project
| |
| --- reflectPackage("com.app.controller") call
| // Note: this package is within this project, not the Commons project.
|
--- Commons // Project
|
--- Reflector // Class
|
--- reflectPackage // Method
【问题讨论】:
标签: java reflection classpath reflections