Develop simple useful eclipse plugin to Scan java source | Good example for beginners

In this article we will create a simple eclipse plugin for Java source code.

Plugin example in this article

  • Add a menu item & toolbar button to eclipse.
  • On click of button/menu, plugin will scan Java source code & check if improper empty String checks are being done like string.equals(“”).
  • If improper checks found then show message dialog to use string.isEmpty() instead.
  • Mark the lines of code with improper checks using markers.

Quick facts about eclipse architecture & plugin development

  • Basic core eclipse mainly provides common user interface, Operating system compatibility & an architecture for dynamic discovery, loading, and running of plug-ins.
  • Eclipse architecture is completely based on plug-in model. Apart from core abilities mentioned above, everything else is added in the form on plug-ins.
  • Even the java file editor, java related menus like source, reference etc. belong to JDT Java development tooling plugins.
  • Plug-ins can add more functionalities to eclipse by using ‘Extension Points’ as shown in below diagram. By extending you can create your own views, Menu items, Editors & lot more.

Common Extension Points

These are basic extension points that can be used in plugins to add new functionality.

  • UI Based extension points
    • org.eclipse.ui.views – Used for adding view similar to the one shown in above image.
    • org.eclipse.ui.editors – Used for adding editor similar to the one shown in above image.
    • org.eclipse.ui.menus – Used for adding menu similar to the one shown in above image.
  • Execution based extension points
    • org.eclipse.ui.commands – This is the trigger or message on trigger that occurs when specific user action happens.
    • org.eclipse.ui.handlers – This is the actual class that implements the behavior for given user action.
    • org.eclipse.ui.bindings – This binds keyboard shortcuts to menus, toolbar etc. (Like Ctrl+6 in our example)




This diagram explains basic plugin execution i.e. how user actions & the plugin code is tied together to perform expected operation.

  • On user operation like click of menu, command is triggered & sent to specific handler object.
  • Handler object will perform the behavior as needed for plugin.
  • Keyboard shortcut bound using ‘Binding’ extension point can also be used to trigger command.

Lets create a basic plugin project

Follow steps in this presentation to create a basic eclipse plugin using eclipse. This slide uses ‘Spring tool suite’ which is just another specialized variation of eclipse to be used for ‘Spring’ projects. But steps are same in any eclipse that you might be having.

Watch it on YouTube – Hello World Eclipse Plugin Project (Java) | Create, run & customize




In the project created, in plugin.xml you can see that there is an extension added to “org.eclipse.ui.menus” to introduce new menu item in eclipse UI for our plugin.

It also has command & handler binding in the form of extension.

This is the default handler class  that generates message dialog box.

Improve basic plugin to scan java file using JDT

Now lets get to our requirementgs for plugin. In order to deal with java source code, we first need to import Java development tooling (JDT) related package & few other bundles to our plugin.

Java development tooling (JDT) allows users to write, compile, test, debug, and edit programs written in the Java programming language.

Eclipse provides easy way to add such packages & bundles as shown in below screenshot. Open plugin.xml in eclipse. Go to ‘Dependencies’ tab > Imported Packages/Required Plug-ins > Add. This will add packages & bundles to MANIFEST.MF. You can also add this directly to MANIFEST.MF





Here is the modified MANIFEST.MF file with new packages & bundles added.

Modify labels & tooltip in plugin.xml to make it more meaningful to our requirements.

Now modify handler class to –

  • Get java source code of selected file.
  • Scan it & look for code like <string>.equals(“”)
  • If found, then add message “Improper String check. Please use String#isEmpty()”
  • Mark line numbers with improper empty string check with message to correct check. We will use ‘Problem’ marker type which is existing eclipse marker type.

This is the handler code with explanation in code comments.



Execute plugin

Now execute plugin same like we did earlier. You should see updated menu label. Sometimes you might still see old label. In such case add “-clean” to “Run Configurations > Arguments > Program Arguments”.

Once you run plugin as eclipse application, in newly opened eclipse, create a simple java project in that eclipse application. Create a simple test java program which has empty string check as shown below.

Now keep cursor on this java file (So that active editor becomes JavaEditor) & then click menu “String check verification > Verify String checks”. A command will be sent to our handler & then it will scan java source code & show message box like shown below. It will also mark line with improper String check. In ‘Problems’ tab below, you can see an entry added with our message.

You can also try using Ctrl+6 keyboard shortcut which will also trigger our plugin.

You can experiment with changing code, running from different codes, editors etc. to assess how handler works.


Source Code

You can find complete source code for plugin at below github repository.

GitHub Source Code – StringCheckEclipsePlugin

Further Read

Custom eclipse plugin | Build, create & publish to update site & marketplace | Good for beginners

References

Eclipse guide, reference & API documentation

Plugin spy short cuts –

  • Alt + Shift + F1 – Plugin Selection Spy
  • Alt + Shift + F2 – Plugin Menu Spy



Leave a Reply

Your email address will not be published. Required fields are marked *