传递第3个库对象时的Netbeans Platform LinkageError

时间:2014-02-26 07:34:13

标签: netbeans-platform linkageerror

嗨我在Netbeans平台上遇到模块设计问题。

我的例子解释了这个问题:

我的项目:

Test-API (API module)
  SP1 (ServiceProvider interface)
  SP2 (ServiceProvider interface)

Test-Module1
  SP1Impl (ServiceProvider class implements SP1)

Test-Module2
  SP2Impl (ServiceProvider class implements SP2)

Test-Module3
  TestAction

SP1:

package linkagetest.api;

import org.jdom2.Element;

public interface SP1 {
  public Element create();
}

SP2:

package linkagetest.api;

import org.jdom2.Element;

public interface SP2 {
  public void doStuff(Element element);
}

SP1Impl:

package linkagetest.mod1;

import linkagetest.api.SP1;
import org.jdom2.Element;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = SP1.class)
public class SP1Impl implements SP1 {

  @Override
  public Element create() {
    return new Element("BLA");
  }
}

SP2Impl:

package linkagetest.mod2;

import linkagetest.api.SP2;
import org.jdom2.Element;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = SP2.class)
public class SP2Impl implements SP2 {

  @Override
  public void doStuff(Element element) {
    element.addContent(new Element("BLA"));
  }
}

TestAction:

package linkagetest.mod3;

import linkagetest.api.SP1;
import linkagetest.api.SP2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "File", id = "linkagetest.mod3.TestAction")
@ActionRegistration(displayName = "#CTL_TEST")
@ActionReference(path = "Menu/File", position = 1300)
@Messages("CTL_TEST=TEST")
public final class TestAction implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent e) {
    SP1 sp1 = Lookup.getDefault().lookup(SP1.class);
    SP2 sp2 = Lookup.getDefault().lookup(SP2.class);

    // and this is where the linkage fails
    sp2.doStuff(sp1.create());
  }
}

我明白了:

java.lang.LinkageError: loader constraint violation: loader (instance of org/netbeans/StandardModule$OneModuleClassLoader) previously initiated loading for a different type with name "org/jdom2/Element"

我理解为什么org.jdom2.Element在Module1和Module2中不是同一个类实例,但我怎么设计这样的层次结构而没有得到LinkageError?我怎样才能传递这个对象?

1 个答案:

答案 0 :(得分:2)

好的,我明白了。我必须只提供一个模块的lib,并将包声明为public。

因此,在pom.xml Test-API模块中,我添加了公共包:

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>nbm-maven-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
      <publicPackages>
        <publicPackage>linkagetest.api</publicPackage>
        <publicPackage>org.jdom2</publicPackage>
      </publicPackages>
    </configuration>
  </plugin>
  ...