waffel’s Weblog

August 21, 2009

use your own variable in eclipse code templates

Filed under: java — Thomas Wabner @ 2:08 pm
Tags: , , ,

On my work we develop eclipse plugins and want to add the current bundle id and bundle version to the since field for class comments. There are not much examples around this problem (we have searched google and eclipse help) so let me explain how you can achive this.

For example we want to define follow code template:

/**
 * @author ${user}
 *
 * ${tags}
 * @since ${bundleId} ${bundleVersion}
 */

The bundleId and bundleVersion variable are not provided by the standard code template variables. Now we have to find out how we can achive this problem.

First of all you need to develop your own eclipse plugin which should provide such feature. The new plugin requires follow dependencies:

  • org.eclipse.core.resources;bundle-version=“3.5.0″,
  • org.eclipse.ui;bundle-version=“3.5.0″,
  • org.eclipse.jface.text;bundle-version=“3.5.0″,
  • org.eclipse.jdt.core;bundle-version=“3.5.0″,
  • org.eclipse.jdt.ui;bundle-version=“3.5.0″,
  • org.eclipse.core.runtime;bundle-version=“3.5.0″,
  • org.eclipse.pde.core;bundle-version=“3.5.0″

The PDE dependency is needed to get the current bundle id and the bundle version. If you need other features (for example a maven project version) you have depend on other plugins. But in this example I’ll show you how to add bundle id and bundle version as extra variable for the eclipse code templates.

Next you have to create a extention to register your own variable resolver at startup. The variable resolvers are the heart of the plugin because they providing new variables for the code templates to be used and resolving the content if you use the code template.

plugin.xml:

< ?xml version="1.0" encoding="UTF-8"?>
< ?eclipse version="3.4"?>
<plugin>
   <extension point="org.eclipse.ui.startup">
      <startup class="yourplugin.eclipse.javadoc.internal.RegisterResolvers">
      </startup>
   </extension>
</plugin>

The RegisterResolvers class implements the IStartup interface from eclipse. This class registers the variable resolvers to the code template context. This is required to have the variables available in the code templates from eclipse.

RegisterResolver:

/**
   * 
   * {@inheritDoc}
   * 
   * @see IStartup#earlyStartup()
   * 
   */
  public void earlyStartup() {
    // check if plug-in org.eclipse.jdt.ui is already active
    final Bundle bundle = Platform.getBundle(PLUGIN_ID);
    if (bundle != null && bundle.getState() == Bundle.ACTIVE) {
      // register resolvers
      registerResolvers();
    } else {
      // register listener to get informed, when plug-in becomes active
      final BundleContext bundleContext = Activator.getDefault().getBundle().getBundleContext();
      bundleContext.addBundleListener(new BundleListener() {
        public void bundleChanged(final BundleEvent pEvent) {
          final Bundle bundle2 = pEvent.getBundle();
          if (!bundle2.getSymbolicName().equals(PLUGIN_ID)) {
            return;
          }
          if (bundle2.getState() == Bundle.ACTIVE) {
            registerResolvers();
            bundleContext.removeBundleListener(this);
          }
        }
      });
    }
  }

/**
   * 
   * Internal method to register resolvers with all context types.
   * 
   */
  private void registerResolvers() {
    final ContextTypeRegistry codeTemplateContextRegistry = JavaPlugin.getDefault().getCodeTemplateContextRegistry();
    final Iterator ctIter = codeTemplateContextRegistry.contextTypes();
    while (ctIter.hasNext()) {
      final TemplateContextType contextType = (TemplateContextType) ctIter.next();
      contextType.addResolver(new BundleIdResolver());
      contextType.addResolver(new BundleVersionResolver());

    }
  }

The bundleIdResolver extends the TemplateVariableResolver and defines (in the default constructor) the variable name which can be used later in the code template, a description and overrides the resolve method.

The resolve method checks if the current project is a plugin project and if so returns the bundle id from the project.

public class BundleIdResolver extends TemplateVariableResolver {
  /**
   * 
   * Constructs a new <code>BundleIdResolver</code>.
   * 
   */
  public BundleIdResolver() {
    super("bundleId", "id of the bundle containing the current compilation unit");
  }

  /**
   * 
   * {@inheritDoc}
   * 
   * @see TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateContext)
   * 
   */
  @Override
  protected String resolve(final TemplateContext pContext) {
    final CodeTemplateContext context = (CodeTemplateContext) pContext;
    final IPluginModelBase pluginModelBase = PluginRegistry.findModel(context.getJavaProject().getProject());
    if (pluginModelBase == null) {
      return null;
    }
    return pluginModelBase.getBundleDescription().getSymbolicName();
  }
}

Now you can come up with the question why not you can use the standard eclipse extention point mechanism and create your own context.

Well, we want to extend the java context but the current eclipse implementation doe’s not provide such functionality. For sure if you want to have your own context you can do this with the standard extention point mechanism (as the Ant example). There is a small example about the eclipse editor templates.

Thanks to my colleague Marco Lehmann for the complete solution.

13 Kommentare »

  1. […] use your own variable in eclipse code templates « waffel's Weblog By Thomas Wabner On my work we develop eclipse plugins and want to add the current bundle id and bundle version to the since field for class comments. There are not much examples around this problem (we have searched google and eclipse help) so let me … waffel's Weblog – https://thomaswabner.wordpress.com/ […]

    Pingback von Eclipse | All Days Long — August 21, 2009 @ 4:03 pm | Antworten

  2. The eclipse „code template“ are (currently) not extendable but the „editor-templates“ are.
    You post the link to the eclipse extension point documentation, i found a other blog how describe the steps http://dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/ … its very easy to register new variables in existing context and new contexts.

    It would be nice if eclipse use the registered variables (i.e. „java-context“ and „javadoc-context“) from the „editor-templates“ in the „code templates“ then it would be easy to extend these templates too.

    Kommentar von Andreas Höhmann — August 21, 2009 @ 4:08 pm | Antworten

  3. […] Read the original post:  use your own variable in eclipse code templates « waffel's Weblog […]

    Pingback von use your own variable in eclipse code templates « waffel's Weblog | Webmaster Tools — August 21, 2009 @ 4:57 pm | Antworten

  4. […] The rest is here: use your own variable in eclipse code templates « waffel's Weblog […]

    Pingback von Webmaster Crap » Blog Archive » use your own variable in eclipse code templates « waffel's Weblog — August 21, 2009 @ 6:14 pm | Antworten

  5. […] von Andreas Höhmann am Montag, 24. August 2009 Based on waffel’s blog i wrote a eclipse plugin which provides the current artifact-version of a maven-project to the […]

    Pingback von Use Maven Artifact Version in Eclipse Templates « Andreas Höhmann’s Weblog — August 24, 2009 @ 1:42 pm | Antworten

  6. Hello waffel,
    i wrote a additional blog to provide the maven-artifact-version in all eclipse-templates.

    Use Maven Artifact Version in Eclipse Code Templates


    regards
    höhmi

    Kommentar von Andreas Höhmann — August 24, 2009 @ 1:43 pm | Antworten

  7. Thanks much! Saved me a lot of digging!

    Take care,
    — Scott

    Kommentar von Scott Stanchfield — August 27, 2009 @ 10:05 pm | Antworten

  8. This site rocks!

    Kommentar von Bill Bartmann — September 2, 2009 @ 8:04 pm | Antworten

  9. I’m so glad I found this site…Keep up the good work

    Kommentar von Bill Bartmann — September 2, 2009 @ 9:17 pm | Antworten

  10. Cool site, love the info.

    Kommentar von Bill Bartmann — September 4, 2009 @ 4:40 pm | Antworten

  11. Great site…keep up the good work.

    Kommentar von Bill Bartmann — September 8, 2009 @ 7:50 am | Antworten

  12. How to pass parameter into template? like maven:prop(„prop“)

    Kommentar von raveman — Oktober 15, 2009 @ 1:07 pm | Antworten

  13. awesome !!

    Kommentar von ranjit — März 29, 2010 @ 5:13 pm | Antworten


RSS feed for comments on this post. TrackBack URI

Hinterlasse eine Antwort zu Use Maven Artifact Version in Eclipse Templates « Andreas Höhmann’s Weblog Antwort abbrechen

Bloggen auf WordPress.com.