简体   繁体   English

OSGi类加载:为什么BND导入未直接引用的类?

[英]OSGi classloading: why does BND imports classes that are not referenced directly?

Bundle-A: 捆绑-A:

FooA.java FooA.java

package com.foo.a;

import com.foo.b.FooB;

class FooA {
    FooB b = new FooB();
}

Bundle-B: 捆绑-B:

FooB.java: FooB.java:

package com.foo.b;

import com.foo.c.FooC;

class FooB {

    public FooC foo() {
       ...
    }
}

Bundle-C: ... Bundle-C:...

So in short, I have 3 bundles - A, B and C. 简而言之,我有3个捆绑包-A,B和C。

Bundle A references bundle B directly, and Bundle B references C. As you see, FooA doesn't use the method in FooB that returns FooC, so FooC is not referenced in bundle A directly. 包A直接引用包B,包B引用C。如您所见,FooA不在FooB中使用返回FooC的方法,因此FooC不在包A中直接引用。

Why is BND including the OSGi import-package to com.foo.c then? 为什么BND会将OSGi导入包包含到com.foo.c中呢? The way I understand it - bundle A only needs bundle B in order to be able to resolve itself. 我的理解方式-包A仅需要包B便能自行解决。 Bundle B on the other hand needs C. But why should A require C directly, if it's not used there? 另一方面,捆绑软件B需要C。但是,如果那里不使用A,为什么A要直接要求C?

I think that bnd imports all classes that are visible to the outside for classes you use. 我认为bnd会导入您所使用的类在外部可见的所有类。 As you use class FooB you might need access to all classes it may need as parameters or return as results. 在使用FooB类时,您可能需要访问它可能需要作为参数或作为结果返回的所有类。

If you want to avoid the dependency you can create an interface that only shows the methods you really need. 如果要避免依赖关系,可以创建一个仅显示您真正需要的方法的接口。 You can then create a service with that interface in bundle B and only access the service using the interface from bundle A. 然后,可以使用捆绑软件B中的该接口创建服务,并且仅使用捆绑软件A中的接口访问该服务。

Have a look on the bytecode of the FooA class. 看一看FooA类的字节码。 You will see FooC in it somehow. 您会以某种方式看到FooC。 Use a Java Decompiler tool to see why it is used. 使用Java Decompiler工具查看使用原因。 Some decompilers create code that show a bit more information than the original java code. 一些反编译器创建的代码比原始Java代码显示更多的信息。 I do not see here, why, but here is another example: 我看不到这里,为什么,但这是另一个示例:

Guess you have the following function: 猜猜您具有以下功能:

public class ListProvider {

    public static ArrayList getMyList() { return null; }

}

And other class calls it like this: 其他类则这样称呼它:

List myVar = ListProvider.getMyList();

You will see ArrayList in the bytecode of the other class. 您将在另一个类的字节码中看到ArrayList。 The reason is that the function signature that is used on the bytecode leven contains the return type, too. 原因是在字节码leven上使用的函数签名也包含返回类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM