CHANNELS
 
 
 
 
 
 
 
 
ON THE WEB
 
 
 
 
PRINT EDITION
 
 
 
 
BZ MEDIA
 
 
 
 
ADVERTISER LINKS
 
 
 
 
 
 
 
AS OF 11/19/2008 6:33AM EST
Classic Tips: Yes You Can! (Access Classes of One Plug-in From Another)
Stories Columns Opinions Resources

By Dwight Deugo

August 27, 2008 —  I was once asked the same question three times in the period of a single week: "Can classes in one plug-in access classes in another, and if so, how?" This question seems to crop up more during RCP development, but the answer is the same for building any plug-in.

The answer is yes, and if you think about it, the reason should seem obvious.

Whenever you build a plug-in, you are making use of classes in other Eclipse plug-ins. To prove it, just try to develop an Eclipse view without being able to have your class inherit from the class org.eclipse.ui.part.ViewPart.

The motivation for the question comes from every developer's desire to organize their code in a modular way. Take for example the situation of developing two different plug-ins, each requiring the same base classes. Do you copy the base classes into each plug-in? Of course not.

What you want to do is create a plug-in that contains the base code and two other plug-ins that depend on it. That way you only have to worry about one development stream for the base-plug. If you are building RCP applications, this situation is likely to happen often. Code you write for one RCP application is often useful in another. So the lesson for today is don't make monolithic plug-ins containing all your code. Think of a plug-in as just another package structure, and break your code up accordingly. Then let Eclipse help make your packages in a plug-in either public or private.

The first step to making a package available for export from a plug-in is to double-click on its plugin.xml file or its MANIFEST.MF file. This action produces a multi-tabbed editor similar to the one shown in Figure 1. I clicked on my base project's MANIFEST.MF file to get the editor.

I have one package in this plug-in (com.espirity.content), and I want to have its class visible to other plug-ins. Only public classes will be visible. I did this by going to the editors Runtime tab and adding my package to the list of exported packages, as shown in Figure 2.

The default is to have this package visible to all downstream plug-ins. However, you can also make a package visible to select plug-ins by manipulating the package visibility plug-in list on the right side of the editor. To make a package visible to all plug-ins, leave this list empty.

With the package visibility established, the next step is to access classes in your exported package from a dependent plug-in's code. Double-click on your dependent plug-in's plugin.xml file or its MANIFEST.MF file and go to its Dependencies tab as shown in Figure 3.

In the Required Plug-ins list, make sure to add your previous plug-in. In my case it was the plug-in named BaseProject. If you forget to do this, your code will not compile while attempting to access the remote classes, even if you add import statements to the packages.

In my example, I added the following import statement to the part of my code that required access to the remote classes:

import com.espirity.content.ExampleContent;


Once everything compiles correctly, you are ready to test your two plug-ins.

A word of warning: When you try to load your dependent plug-in, it will fail to load if the one you depend on is not in the Eclipse plug-in's directory or you haven't configured it to be included in your test run. That makes perfect sense, because if code you depend on is not available, then your code won't work, so Eclipse figures there's no point in loading it.

This column was originally run in September, 2006, and may be inconsistent with current versions.


Share this link: http://www.sdtimes.com/link/32768

 


 
 
 
 
 
 
 
 
 
 
SUBSCRIBE TODAY!
 E-Newsletters:
  News on Mon/Thurs.  More info
  Test & QA Report  More info
  EclipseNews  
  SPTech Report  More info
 
 
 
PDF & PRINT EDITION
* Requires Resource Account!  LOGIN or SIGN UP

Download Current Issue!
ISSUE 11/15/2008 PDF

Need Back Issues?
DOWNLOAD HERE

Receive The Print Edition?
SUBSCRIBE HERE
 
REGISTER
 
GET NOTIFIED!
About all of the latest Resources
 
 
SD TIMES 100
It's time once again to
recognize the organizations
or individuals that have
demonstrated leadership in
their markets.