Morrandir
Morrandir

Reputation: 615

Eclipse RCP 4 - Handler method parameters

I'm currently taking a look to the new Eclipse RCP framework and have a questions about handlers. In RCP 3.x a handler class needed to implement an interface, so the methods where given. In RCP 4 the handler class doesn't need to implement an interface. Instead you annotate the methods. E.g. if you have an ExitHandler as in Vogellas Tutorial you have an @Execute annotation. As you can see, there's an IWorkbench parameter passed.

package com.example.e4.rcp.todo.handler;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.workbench.IWorkbench;

public class ExitHandler {
  @Execute
  public void execute(IWorkbench workbench) {
    workbench.close();
  }
} 

My question now is: How do I know which parameters are passed when using certain annotations? How do I know in this certain case that I get an IWorkbench object and not a Window object or something? In fact I can annotate a method without a parameter and it will still be executed.

Is there documentation somewhere? The Eclipse e4 Tools don't seem to support me there either...

Upvotes: 3

Views: 2690

Answers (1)

Modus Tollens
Modus Tollens

Reputation: 5123

The annotation @Execute doesn't determine the type to be injected, the method declaration does.

As a behavior annotation, @Execute marks the method that should be called when the handler is executed. The type of the object to be injected is determined by the method's arguments. To inject another object type, change the method's argument, e.g.

@Execute
public void execute(MWindow window) {
    // method body
}

to inject an MWindow from the active context.

The @Execute annotation contains the @Inject annotation, so when an event is triggered and the handler is going to be executed the following happens:

  1. the framework looks for the method marked by the @Execute annotation
  2. the E4 context is searched for an object of the method's argument type (e.g. IWorkbench)
  3. the object gets injected and the method is executed

Unless the @Optional annotation is set, an exception is thrown if no object is found in the context.

For further reading and more thorough explanations see Eclipse 4 (e4) Tutorial Part 4- Dependency Injection Basics and Eclipse 4 (e4) Tutorial Part 6: Behavior Annotations.

An overview of Eclipse 4 annotations can be found at the Eclipse 4 Wiki.

Upvotes: 3

Related Questions