我使用Reflections库开发了一个应用程序,用于查询具有特定注释的所有类。在我决定从我的应用程序创建一个Eclipse插件之前,一切都非常顺利。然后Reflections停止工作
考虑到我的应用程序在不作为Eclipse插件的一部分时运行良好,我认为这应该是一个类加载器问题。因此,我向Reflections
类添加了插件激活器类的类加载器、上下文类加载器和所有我能想到的其他类加载器,但没有成功。这是我的代码的简化版本:
ConfigurationBuilder config = new ConfigurationBuilder();
config.addClassLoaders(thePluginActivatorClassLoader);
config.addClassLoaders(ClasspathHelper.getContextClassLoader());
config.addClassLoaders("all the classloaders I could imagine");
config.filterInputsBy(new FilterBuilder().include("package I want to analyze"));
Reflections reflections = new Reflections(config);
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(MyAnnotation.class); //this Set is empty
我还尝试添加类的url,我想加载到ConfigurationBuilder
类,但它没有帮助。
有人能告诉我是否有一种方法使Reflections
作为Eclipse插件的一部分工作?或者我应该更好地寻找另一个替代方案?非常感谢,我真的很困惑。
我假设你已经知道如何创建包(否则,检查这个)。
在对Reflections API进行了一些调试和探索之后,我意识到问题在于Reflections无法读取OSGi url (bundlerresource://…),从而导致异常:
org.reflections.ReflectionsException: could not create Vfs.Dir from url,
no matching UrlType was found [bundleresource://1009.fwk651584550/]
和这个建议:
either use fromURL(final URL url, final List<UrlType> urlTypes)
or use the static setDefaultURLTypes(final List<UrlType> urlTypes)
or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.
所以我相信实现一个UrlType OSGi(例如class BundleUrlType implements UrlType {...}
),并注册它像这样:
Vfs.addDefaultURLTypes(new BundleUrlType());
应该使Reflections API可以从bundle内部使用。reflection依赖项应该像这里所描述的那样添加到Eclipse Plugin项目中。
这就是我的示例MANIFEST。添加所需jar后MF的样子:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: ReflectivePlugin
Bundle-SymbolicName: ReflectivePlugin
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: reflectiveplugin.Activator
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.annotation;version="1.0.0",
org.osgi.framework;version="1.3.0",
org.osgi.service.log;version="1.3",
org.osgi.util.tracker;version="1.3.1"
Bundle-ClassPath: .,
lib/dom4j-1.6.1.jar,
lib/guava-r08.jar,
lib/javassist-3.12.1.GA.jar,
lib/reflections-0.9.5.jar,
lib/slf4j-api-1.6.1.jar,
lib/xml-apis-1.0.b2.jar
Export-Package: reflectiveplugin,
reflectiveplugin.data
注意:Used Reflections v. 0.9.5
下面是一个UrlType实现示例:
package reflectiveplugin;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import org.osgi.framework.Bundle;
import org.reflections.vfs.Vfs;
import org.reflections.vfs.Vfs.Dir;
import org.reflections.vfs.Vfs.File;
import org.reflections.vfs.Vfs.UrlType;
import com.google.common.collect.AbstractIterator;
public class BundleUrlType implements UrlType {
public static final String BUNDLE_PROTOCOL = "bundleresource";
private final Bundle bundle;
public BundleUrlType(Bundle bundle) {
this.bundle = bundle;
}
@Override
public boolean matches(URL url) {
return BUNDLE_PROTOCOL.equals(url.getProtocol());
}
@Override
public Dir createDir(URL url) {
return new BundleDir(bundle, url);
}
public class BundleDir implements Dir {
private String path;
private final Bundle bundle;
public BundleDir(Bundle bundle, URL url) {
this(bundle, url.getPath());
}
public BundleDir(Bundle bundle, String p) {
this.bundle = bundle;
this.path = p;
if (path.startsWith(BUNDLE_PROTOCOL + ":")) {
path = path.substring((BUNDLE_PROTOCOL + ":").length());
}
}
@Override
public String getPath() {
return path;
}
@Override
public Iterable<File> getFiles() {
return new Iterable<Vfs.File>() {
public Iterator<Vfs.File> iterator() {
return new AbstractIterator<Vfs.File>() {
final Enumeration<URL> entries = bundle.findEntries(path, "*.class", true);
protected Vfs.File computeNext() {
return entries.hasMoreElements() ? new BundleFile(BundleDir.this, entries.nextElement()) : endOfData();
}
};
}
};
}
@Override
public void close() { }
}
public class BundleFile implements File {
private final BundleDir dir;
private final String name;
private final URL url;
public BundleFile(BundleDir dir, URL url) {
this.dir = dir;
this.url = url;
String path = url.getFile();
this.name = path.substring(path.lastIndexOf("/") + 1);
}
@Override
public String getName() {
return name;
}
@Override
public String getRelativePath() {
return getFullPath().substring(dir.getPath().length());
}
@Override
public String getFullPath() {
return url.getFile();
}
@Override
public InputStream openInputStream() throws IOException {
return url.openStream();
}
}
}
下面是我在Activator类中创建反射的方法:
private Reflections createReflections(Bundle bundle) {
Vfs.addDefaultURLTypes(new BundleUrlType(bundle));
Reflections reflections = new Reflections(new Object[] { "reflectiveplugin.data" });
return reflections;
}
最后一点非常令人困惑,但仍然很重要:如果你在Eclipse(运行/OSGi框架)中运行插件,你必须将你的类输出目录也添加到Reflections路径模式(即。"bin"或"target/classes")。虽然,对于一个已发布的插件来说,它是不需要的(要构建一个插件/包,请执行"Export"->"可部署的插件和片段")。
只是为了记录以防其他人有同样的问题。这里对Vlad的答案做了一个小修改,以避免必须将输出目录添加到Reflections路径模式中。区别只存在于BundleDir类中。在我所有的测试中,它似乎工作得很好:
public class BundleUrlType implements UrlType {
public static final String BUNDLE_PROTOCOL = "bundleresource";
private final Bundle bundle;
public BundleUrlType(Bundle bundle) {
this.bundle = bundle;
}
@Override
public Dir createDir(URL url) {
return new BundleDir(bundle, url);
}
@Override
public boolean matches(URL url) {
return BUNDLE_PROTOCOL.equals(url.getProtocol());
}
public static class BundleDir implements Dir {
private String path;
private final Bundle bundle;
private static String urlPath(Bundle bundle, URL url) {
try {
URL resolvedURL = FileLocator.resolve(url);
String resolvedURLAsfile = resolvedURL.getFile();
URL bundleRootURL = bundle.getEntry("/");
URL resolvedBundleRootURL = FileLocator.resolve(bundleRootURL);
String resolvedBundleRootURLAsfile = resolvedBundleRootURL.getFile();
return("/"+resolvedURLAsfile.substring(resolvedBundleRootURLAsfile.length()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public BundleDir(Bundle bundle, URL url) {
//this(bundle, url.getPath());
this(bundle, urlPath(bundle,url));
}
public BundleDir(Bundle bundle, String p) {
this.bundle = bundle;
this.path = p;
if (path.startsWith(BUNDLE_PROTOCOL + ":")) {
path = path.substring((BUNDLE_PROTOCOL + ":").length());
}
}
@Override
public String getPath() {
return path;
}
@Override
public Iterable<File> getFiles() {
return new Iterable<Vfs.File>() {
public Iterator<Vfs.File> iterator() {
return new AbstractIterator<Vfs.File>() {
final Enumeration<URL> entries = bundle.findEntries(path, "*.class", true);
protected Vfs.File computeNext() {
return entries.hasMoreElements() ? new BundleFile(BundleDir.this, entries.nextElement()) : endOfData();
}
};
}
};
}
@Override
public void close() { }
}
public static class BundleFile implements File {
private final BundleDir dir;
private final String name;
private final URL url;
public BundleFile(BundleDir dir, URL url) {
this.dir = dir;
this.url = url;
String path = url.getFile();
this.name = path.substring(path.lastIndexOf("/") + 1);
}
@Override
public String getName() {
return name;
}
@Override
public String getRelativePath() {
return getFullPath().substring(dir.getPath().length());
}
@Override
public String getFullPath() {
return url.getFile();
}
@Override
public InputStream openInputStream() throws IOException {
return url.openStream();
}
}
}
Eclipse是建立在OSGi之上的,你要面对OSGi类加载…这不是一场容易取胜的战斗。
看看Neil Bartlett的这篇文章:OSGi就绪-加载类。你也可以谷歌"OSGi伙伴策略"