Using expressions in your extension points
September 7th, 2010 by Eugene Ostroukhov

Originally I meant to create a reference of the Eclipse expressions and properties available in SDK but there is already such article in Eclipse Wiki. I would strongly recommend bookmarking that article – it is an excellent reference. I would like to describe how you could leverage the expressions in your extension points (when you declare extension points as opposed to providing extensions to platform extension points). There are several reasons to use expressions as a part of your extension point:

  1. Lazy initialization – expression can be tested without activating the plugin that contributed the extension.
  2. External declaration – expressions are declared in the plugin.xml and that sometimes makes it easier to maintain them.
  3. Flexible and pluggable language. Users can leverage org.eclipse.core.expressions.definitions extension point to declare reusable expressions. Users can also leverage org.eclipse.core.expressions.propertyTesters to declare property testers that contain complex logic.

To use expression in your extension point you will need to update your extension point schema file (exsd file) to declare expression language elements. The easiest way is by referencing expression language schema:

<include schemaLocation="schema://org.eclipse.core.expressions/schema/expressionLanguage.exsd"/>

This will import all the elements so you can use them in the extension elements editor:

Expression elmeents in the extension element editorNote that you can specify any expression element as the expression root. I would recommend to using the “enablement” element as this would let you get all changes to expression languages as they appear in the subsequent Eclipse releases.┬áPlug-in developers will see the expressions in the PDE editor after extension point definition is updated.

To test the expression you will need to parse the expression:

IConfigurationElement[] children = configurationElement.getChildren("enablement");
if (children.length == 1) {
    expression = ExpressionConverter.getDefault().perform(children[0]);

Then you will have to create “evaluation context” and evaluate the expression. Evaluation context provides all the variables that are available to the expression – like “activeWorkbenchWindow” and others in the commands framework. You may spacify one object as a “default” (it is “project” variable in my snippet):

final EvaluationResult result = expression.evaluate(new EvaluationContext(null, project));

“result” will be one of FALSE, TRUE and NOTLOADED. NOTLOADED is mostly when the user references the propery tester from inactive plug-in and tells the framework to avoid plug-in activation to instantiate just the property tester.

After this point the user will be able to use the expressions with your extension point just the same way they are used in extension points declared by core Eclipse.

2 Responses  
  • antkei writes:
    September 14th, 20116:11 pmat

    I have compile error:

    Referenced element ‘enablement’ is not defined

    at line:

    Did I missed anything? Thanks.

    • Eugene Ostroukhov writes:
      September 18th, 20115:00 pmat

      Please make sure you have “<include schemaLocation=”schema://org.eclipse.core.expressions/schema/expressionLanguage.exsd”/>” in your extension point schema (exsd) file.

»  Substance:WordPress   »  Style:Ahren Ahimsa