This document describes how to build a relatively useless eclipse
plug-in. The plug-in adds an option to the drop down menu that appears
when right clicking on a user defined method (i.e., not a library
method) in the Project Explorer frame. When the option is selected a
dialog box will appear that states the total number of user defined
methods in the project along with the number of method calls (both to
library and to user defined methods) made by the defining class of the
method the option was invoked on. The goal is to show an interface with
some of the entities that Eclipse makes available, such as
IMethod
s, IClassFile
s,
ICompilationUnit
s, etc. It also demonstrates how to build
an Abstract Syntax Tree (AST) and how to utilize the visitor pattern to
traverse its nodes.
Note: This document is meant to be used in
conjunction with InfoGatherer.java
and MyVisitor.java
.
You should read and understand both of these files.
If you haven't done so already, download Eclipse. There are different configurations of Eclipse available; I recommend Eclipse for RCP and RAP Developers (choose the latest version). There's also an installer you can use but be sure to pick the RCP and RAP developer package. If you already have Eclipse and want to use that installation, you may need to install additional plug-ins.
Open Eclipse.
In the Workspace Launcher window create a new workspace (e.g.,
C:\sandbox\ASTTrav
). This will be the folder that contains
all the code and classes for the plug-in.
If the Welcome screen is open close it.
Select Window -> Perspective -> Open Perspective -> Other.
In the "Open Perspective" dialog box select "Plug-in Development." This tells Eclipse that you will be creating a plug-in rather then a Java application or other entity. Eclipse has a unique look and tool set for each perspective.
Select File -> New -> Project.
In the "New Project" dialog window select Plug-in Development -> Plug-in Project.
In "New Plug-in Project" enter "ASTTrav" as the project name and hit next.
The Plug-in Content page should be in focus hit next.
In the Templates window ensure the "Create a ..." check box is checked and select "Hello, World Command," hit next.
In the Sample Command Contribution form, change the value of the "Handler Class Name" field to ASTTravHandler.
Upon hitting finish you should see the ASTTrav project in the Project Explorer frame Expand it and double click the "plugin.xml."
You should now see a page in the main window with multiple tabs along the bottom. Select the "Extensions" tab, thus bringing the Extensions page into focus.
In the "All Extensions" pane of this window:
commandId
field and select the (first) entry
"ASTTrav.commands.sampleCommand". This is the identifier of a command
that was created when you invoked the Hello World wizard.Now select the "Dependencies" tab at the bottom of the main window pane. Add "org.eclipse.jdt.core", "org.eclipse.core.resources", and "org.eclipse.core.runtime" to the "Required Plug-ins" list if they are not already there.
Save all of this by either pressing CTRL-S or selecting File -> Save from the toolbar menu.
Add the included MyVisitor.java
file and the
InfoGatherer.java
file to the asttrav.handlers
package (this can be done by creating new classes of the same name and
coping the code or copying the files right clicking on the package in
the "Project Explorer" pane and selecting paste.) NOTE:
These files are heavily commented and should be reviewed
carefully.
In the "Project Explorer" pane, open
asttrav.handlers.ASTTravHandler.java
. The source code
should be in the main window pane.
Import the org.eclipse.jface.viewers.*
and
org.eclipse.jdt.core.*
packages.
Add the following code to the class:
//This method converts a Selection to an object
public static Object getSingleElement(ISelection s) {
if (!(s instanceof IStructuredSelection))
return null;
= (IStructuredSelection) s;
IStructuredSelection selection if (selection.size() != 1)
return null;
return selection.getFirstElement();
}
//Converts an object to an IMethod
private static IMethod getSelectedMethod(Object element) {
= null;
IMethod method if (element instanceof IMethod) {
= (IMethod) element;
method }
return method;
}
Delete the current execute()
method and replace it
with:
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
= HandlerUtil.getActiveWorkbenchWindowChecked(event);
IWorkbenchWindow window
// Get the IMethod that this action was invoked on
=
IStructuredSelection selection .getCurrentStructuredSelection(event);
HandlerUtil
// Get the IMethod that this action was invoked on
Object element = getSingleElement(selection);
= getSelectedMethod(element);
IMethod method
// Get the info on the method
= new InfoGatherer();
InfoGatherer ig
int x = ig.getNumberOfUserMethods();
int y = ig.getNumberOfMethodCalls(method);
.openInformation(window.getShell(), "ASTTrav Plug-in",
MessageDialog"The number of user defined methods = " + x + ". There were " + y +
" method calls made from " + method.getCompilationUnit().getElementName() +
" which is the declaring class of " + method.getElementName() +
", the method you selected.");
return null;
}
This assignment is based on an assignment by Jason Sawin and Atanas Rountev.