Hi I have problems with module design in Netbeans Platform.
My example explains the issue:
My projects:
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());
}
}
I get:
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"
I understand why org.jdom2.Element
is not the same class instance in Module1 and Module2, but how am I supposed to design such a hierarchy without getting the LinkageError? How can I pass the object?
Ok, I figured it out. I must provide the lib only by one module and declare packages as public.
So in pom.xml
of Test-API
module I added the public package:
<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>
...