所以我使用Java Reflections API在另一个jar中搜索Foo 使用以下代码扩展的类:
Reflections reflections = new Reflections("com.example");
for(Class<? extends Foo> e : reflections.getSubTypesOf(Foo.class)) {
doSomething()
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,思考会抛出以下错误:
org.reflections.ReflectionsException: could not get type for name com.example.ExtendsFoo
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题我难倒?
提前致谢!
cho*_*oks 13
问题可能是由于没有可以解析名称的类加载器(即使它可以解析子类型).这听起来很矛盾,但是当我构建一个配置并ClasspathHelper.forClassLoader在应用程序实例化的URLClassloader上使用以确定要在类路径上扫描的内容时,我有错误消息,但是没有将所述URLClassLoader传入Reflection配置,以便它可以实例化事物正确.
所以你可能想尝试以下几点:
URLClassLoader urlcl = new URLClassLoader(urls);
Reflections reflections = new Reflections(
new ConfigurationBuilder().setUrls(
ClasspathHelper.forClassLoader(urlcl)
).addClassLoader(urlcl)
);
Run Code Online (Sandbox Code Playgroud)
where urls是包含要加载的类的jar的URL的数组.如果我没有最后的addClassLoader(...)呼叫,我会得到与你相同的错误ConfigurationBuilder.
如果这不起作用或不适用,则可能值得设置断点ReflectionsUtil.forName(String typeName, ClassLoader... classLoaders))以查看正在发生的情况.
| 归档时间: |
|
| 查看次数: |
14417 次 |
| 最近记录: |