我正在发送一个 bundle-jar 文件并将其安装在我的 osgi 中。到目前为止,它就像一个魅力。但是,一旦我检查了内容,我的代码就无法发现捆绑包中的注释。注释都是相同的(捆绑,发现和发送)使用相同的注释-jar,所以它们应该是相同的。安装的包在收到后会保存到文件系统中。此外,我想知道这是否可能是 osgi 中不同类加载器的问题。有人知道为什么在这种情况下注释为空吗?
BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
Bundle bundle = context.installBundle("foobar.jar");
bundle.start();
BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
Bundle bundle = context.installBundle("foobar.jar");
bundle.start();
BundleWiring wiring = bundle.adapt(BundleWiring.class);
Collection<String> classes = wiring.listResources("/", "*.class",BundleWiring.LISTRESOURCES_RECURSE);
LinkedList<String> c = new LinkedList<String>();
for (String str : classes) {
str = str.replaceAll(".class", "");
c.add(str.replaceAll("/", "."));
}
String classname = c.get(0);
Class<?> clazz = bundle.loadClass(classname);
for (Method m: clazz.getDeclaredMethods()) {
System.out.println(m.getName());
TestAnnotation testAnnotation= m.getAnnotation(TestAnnotation.class);
if (txAnnotation != null)
System.out.println("\tsource=" + testAnnotation.sourceUnit() + "; target=" + testAnnotation.targetUnit());
}
注释如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestAnnotation{
String a();
String b();
}
注释类的示例:
public class annoClass{
@TestAnnotation(sourceUnit="a", targetUnit="b")
public int foo(int i) {
return 2;
}
@TestAnnotation(sourceUnit="xyz", targetUnit="abc")
public int bar(int i) {
return 3;
}
}
收货人清单:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Receiver
Bundle-SymbolicName: com.foo.bar.receiver.impl
Bundle-Version: 1.0.0
Bundle-Activator: comfoo.bar.impl.receiver
Import-Package: com.foo.bar.receiver;version="1.0.0",
com.foo.bar.annotation,
javax.jms,
org.apache.activemq.command;version="5.8.0",
org.osgi.framework;version="1.3.0",
org.osgi.util.tracker;version="1.5.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.eclipse.osgi
注释清单:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Annotations
Bundle-SymbolicName: com.foo.bar.annotations
Bundle-Version: 1.0.0
Import-Package: javax.jms,
org.apache.activemq.command;version="5.8.0",
org.osgi.framework;version="1.3.0",
org.osgi.util.tracker;version="1.5.1"
Export-Package: com.foo.bar.annotation
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.eclipse.osgi
生产者清单:(生产者不需要注释的导入,因为它只是转发)
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Producer
Bundle-SymbolicName: com.foo.bar.producerplugin
Bundle-Version: 1.0.0
Bundle-Activator: com.foo.bar.producerplugin.ProducerPlugin
Bundle-ActivationPolicy: lazy
Import-Package: org.osgi.framework;version="1.3.0",
org.apache.activemq;version="0.0.0",
org.apache.activemq.command;version="0.0.0",
javax.jms;version="0.0.0",
com.foo.bar.brokerplugin;version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.eclipse.osgi