Just An Application

October 30, 2009

An Extended NewAnnotation Wizard For Eclipse: Implementing A Wizard Page

Filed under: Eclipse, Eclipse Plugins, Eclipse Wizards, Java — Tags: , , , — Simon Lewis @ 12:41 pm

The new wizard page is implemented by the class


which sub-classes


following the example in JDT Plug-in Developer Guide:Programmer’s Guide:JDT UI: Java wizard pages

The constructor is based on that from the equivalent Eclipse supplied class as follows

            super(ANNOTATION_TYPE, "NewAnnotationWizardPage");
            retention = RetentionPolicy.CLASS;
            targets   = EnumSet.noneOf(ElementType.class);

as is the initialization method

        void init(IStructuredSelection theSelection) 
            IJavaElement element= getInitialJavaElement(theSelection);

            retention = RetentionPolicy.CLASS;

The initContainerPage(IJavaElement) method is inherited from


which is the super-class of NewTypeWizardPage.

The initTypePage(IJavaElement) method is inherited from NewTypeWizardPage.

You might expect that this method would invoke initContainerPage(IJavaElement) itself but apparently not.

The updateStatus() method is as follows.

        private void updateStatus()
                new IStatus[] 

It is responsible for collecting together the statuses of all the data required to construct the Annotation
and passing them to the updateStatus(IStatus[]) method inherited from the



This method then updates the status of the wizard page as appropriate.

For example, if all the necessary data is present and there are no problems with it then the ‘Finish’ or ‘Next’ button will be enabled.

Alternatively if, for example, a name for the Annotation has been entered and then deleted then fTypeNameStatus has an IStatus value with a severity of ERROR and the message

    Type name is empty.

which is duly displayed by the wizard as shown below.


Since an Annotation can legally be defined without any annotation this method is the same as that of the equivalent Eclipse supplied class.

Copyright (c) 2009 By Simon Lewis. All Rights Reserved


An Extended NewAnnotation Wizard For Eclipse: Implementing A New Wizard

An Eclipse wizard comprises a wizard object and one or more pages. In the case of the Eclipse supplied NewAnnotation wizard the wizard object is an instance of


which has a single page which is an instance of


Neither of these classes were designed to be extensible, and certainly not dynamically.

As it states in the documentation for the class NewAnnotationWizardPage

Note: This class is not intended to be subclassed, but clients can instantiate.

so we need a new wizard page, and the NewAnnotationCreationWizard default constructor is hard-wired to use an instance of NewAnnotationWizardPage so we need a new wizard object as well.

The class


is supplied for use as a base class for new wizard pages for creating new Java type instances, but what about the wizard object itself ? The NewAnnotationCreationWizard class is undocumented and is in a package with ‘internal’ in its name, as is its base class


so it is probably not intended for use by other bundles, but a fragment is effectively part of its host bundle, it uses the same class loader for example, so the classes in internal packages are at least visible to it.

As an aside, looking at the manifest of the org.eclipse.jdt.ui bundle shows the following entry in the Export-Package header

    org.eclipse.jdt.internal.ui.wizards;x-friends:="org.eclipse.jdt.junit, org.eclipse.jdt.apt.ui"

so the package is being exported but qualified using the Eclipse specific x-friends directive which is used

to specify a list of bundles which are allowed access to the package

so the package is being exported, but only for use by those two bundles.

As an aside on the aside, the IDE will allow a reference to a type in the package from an arbitrary bundle but there is an associated warning.

Anyway, from within a fragment hosted by the org.eclipse.jdt.ui bundle we can access any class, irrespective of the export status of its containing package. ‘tho the standard Java access controls still apply obviously. This is handy since it means we can effectively clone the NewAnnotationCreationWizard class as follows

Note that the NewAnnotationWizardPage class being referenced is the new wizard page class not the Eclipse supplied one.

package xper.eclipse.plugin.jdt.ui.annotation;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;

public final class NewAnnotationWizard 
    public NewAnnotationWizard()
    public void addPages() 
        if (page == null) 
            page = new NewAnnotationWizardPage();
    public IJavaElement getCreatedElement() 
        return page.getCreatedType();
    public boolean performFinish() 
        boolean result= super.performFinish();
        if (result) 
            IResource resource= page.getModifiedResource();
            if (resource != null) 
        return result;

    protected void finishPage(IProgressMonitor theMonitor)
    private NewAnnotationWizardPage	page;

Copyright (c) 2009 By Simon Lewis. All Rights Reserved

October 29, 2009

An Extended NewAnnotation Wizard For Eclipse: Getting Started

Because I wanted to re-use the existing Eclipse NewAnnotation wizard code, if at all possible, I decided to create a fragment hosted by the bundle containing the code for the Eclipse Java type creation wizards, rather than a stand-alone bundle. Note that Eclipse uses the term ‘plug-in’, but an Eclipse ‘plug-in’ is an OSGi bundle with some added stuff, and I’m going to stick to the term bundle.

To do this select ‘New’ from the ‘File’ menu on the toolbar, ‘Project’ from the resulting sub-menu and ‘Fragment Project’ from the ‘Plug-in Development’ section of the resulting dialog


Hit ‘Next’ and you will get the first page of the ‘New Fragment Project’ wizard


Fill in the Project Name and hit ‘Next’


The Host Plug-in (bundle) section needs to be filled-in, so hit ‘Browse’ and you will get the ‘Plug-in Selection’ dialog show below


We are actually interested in the code in the package


but which bundle is it in ?

The mapping of packages to JDT bundles can be found in the JDT Plug-ins Map which shows that we are after the



Entering this is in the text field at the top gets us two matches


Picking the top-most one and hitting ‘OK’ gets us back to the second page


That’s it. Hit ‘Finish’, respond to the resulting dialog about opening the associated perspective and its done.

Not that it does currently does anything but the resulting fragment can be ‘run’ by selecting the ‘run’ button from the toolbar and then selecting ‘Eclipse Application’ from the ‘Run As’ sub-menu, unless you are running Eclipse on Snow Leopard, that is, in which case you may get an error dialog instead, and a log containing the following

!ENTRY org.eclipse.osgi 4 0 2009-10-29 21:13:30.206
!MESSAGE Application error
java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM
	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:182)
	at org.eclipse.swt.internal.Library.loadLibrary(Library.java:159)
	at org.eclipse.swt.internal.C.(C.java:21)
	at org.eclipse.swt.internal.cocoa.NSThread.isMainThread(NSThread.java:33)
	at org.eclipse.swt.graphics.Device.(Device.java:116)
	at org.eclipse.swt.widgets.Display.(Display.java:628)
	at org.eclipse.swt.widgets.Display.(Display.java:619)
	at org.eclipse.ui.internal.Workbench.createDisplay(Workbench.java:532)
	at org.eclipse.ui.PlatformUI.createDisplay(PlatformUI.java:161)
	at org.eclipse.ui.internal.ide.application.IDEApplication.createDisplay(IDEApplication.java:143)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:88)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1311)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1287)

which is a bit of a pain

Fortunately it is easily fixed.

Bring up the ‘Run Configurations’ dialog, using ‘Run Configurations …’ from the menu on the ‘run’ button for example, select ‘Eclipse Application’ then select the ‘Arguments’ tab.

In the ‘Program arguments’ text box at the top prepend the argument


as shown below. This tells the VM not to mess about with all that 64-bit stuff, or something like that.


Try ‘running’ the fragment again and you should now get a second copy of Eclipse uncannily similar to the one you are already running.

Copyright (c) 2009 By Simon Lewis. All Rights Reserved


An Extended NewAnnotation Wizard For Eclipse

Something you cannot currently do with any of the Eclipse Java type creation wizards is add annotations. This is not a particularly big deal, you can always add them later using the editor, unless you happen to be defining a lot of new annotations which are themselves annotated with one or more of the built-in meta-annotations

in which case it would be quite handy to be able to tick some boxes in the wizard and have them appear auto-magically

Here’s the wizard as implemented by Eclipse


Anyway, because I have been defining quite a few new annotations and was getting quite bored with adding the meta-annotations by hand I thought I would have a go at customizing the default wizard and adding the features I wanted.

It turned out to be a bit more involved than I had expected, but it can be done, and here it is.


Not the world’s greatest UI design for sure, but it does the job.

Copyright (c) 2009 By Simon Lewis. All Rights Reserved


Blog at WordPress.com.

%d bloggers like this: