| [news.eclipse.platform] Re: Communication between two plugins. |
Walter,
Regards, Suvi D.
"Suvi Dhirakaosal" <suviATcorelisDOTcom@xxxxxxxxxxxxxxxx> wrote in message news:ekg67f$vs8$1@xxxxxxxxxxxxxxxxxxxxHello all,
I have been trying to get two plug-ins that I created/modified to do some basic communication for a few days (passing text). From googling to searching the Eclipse newsgroups it seems that there are quite a few ways, and the most formal method is to use extensions/extension points.
My setup is the following:
Plug-in A with: - extension point P - interface I
Plug-in B with: - extension - class C implementing I
Now I'm at the point where I am trying to instantiate C within plug-in A and call the methods of I.
The way you do it is with IConfigurationElement.createExecutableExtension(). You get the IConfigurationElement from the platform registry - basically it amounts to parsing the XML of the extension declaration. What createExecutableExtension() does is to reach into plug-in B, cause it to instantiate a C, and then return it as an I.
So, plug-in A *never ever* sees class C; it only ever sees an I. But under the covers it's actually a C, created by plug-in B's classloader.
Here's a bit of sample code, excerpted from FactoryPathUtil in org.eclipse.jdt.apt.core. Sorry for the muddled indents. In this case, AnnotationProcessorFactory corresponds to your "I". Note the "HERE'S YOUR PAYLOAD" line, that's where you actually get your C.
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(
AptPlugin.PLUGIN_ID, // name of plugin that exposes this extension point
"annotationProcessorFactory"); //$NON-NLS-1$ - extension id
// Iterate over all declared extensions of this extension point.
// A single plugin may extend the extension point more than once, although it's not recommended.
for (IExtension extension : extensionPoint.getExtensions())
{
// Iterate over the children of the extension to find one named "factories".
for(IConfigurationElement factories : extension.getConfigurationElements())
{
if (!"factories".equals(factories.getName())) { //$NON-NLS-1$ - name of configElement
continue;
}
// Iterate over the children of the "factories" element to find all the ones named "factory".
for (IConfigurationElement factory : factories.getChildren()) {
if (!"factory".equals(factory.getName())) { //$NON-NLS-1$
continue;
}
try {
Object execExt = factory.createExecutableExtension("class"); //$NON-NLS-1$ - attribute name
if (execExt instanceof AnnotationProcessorFactory){
////// HERE'S YOUR PAYLOAD: apf = (AnnotationProcessorFactory)execExt;
}
} catch(CoreException e) {
AptPlugin.log(e, "Unable to load annotation factory from plug-in"); //$NON-NLS-1$
}
}
}
}