Eclipse RCP 4 - Handler method parameters

試著忘記壹切 提交于 2020-01-23 11:44:32

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/17651022/eclipse-rcp-4-handler-method-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!