Eclipse RCP 4 - 处理程序方法参数 [英] Eclipse RCP 4 - Handler method parameters
问题描述
我目前正在研究新的 Eclipse RCP 框架,并有关于处理程序的问题.在 RCP 3.x 中,一个处理程序类需要实现一个接口,因此给出了方法.在 RCP 4 中,处理程序类不需要实现接口.相反,您可以对方法进行注释.例如.如果您有 ExitHandler
,如 Vogellas 教程 你有一个 @Execute
注释.如您所见,传递了一个 IWorkbench
参数.
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();
}
}
我现在的问题是:我怎么知道在使用某些注解时传递了哪些参数?在这种特定情况下,我怎么知道我得到的是 IWorkbench
对象而不是 Window 对象之类的?事实上,我可以在没有参数的情况下注释一个方法,它仍然会被执行.
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.
某处有文档吗?Eclipse e4 工具似乎也不支持我...
Is there documentation somewhere? The Eclipse e4 Tools don't seem to support me there either...
推荐答案
注解 @Execute
不决定注入的类型,方法声明决定.
The annotation @Execute
doesn't determine the type to be injected, the method declaration does.
作为行为注解,@Execute
标记了执行处理程序时应该调用的方法.要注入的对象的类型由方法的参数决定.要注入另一种对象类型,请更改方法的参数,例如
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
}
从活动上下文中注入一个 MWindow
.
to inject an MWindow
from the active context.
@Execute
注释包含 @Inject
注释,因此当事件被触发并且处理程序将被执行时,会发生以下情况:
The @Execute
annotation contains the @Inject
annotation, so when an event is triggered and the handler is going to be executed the following happens:
- 框架查找由
@Execute
注解标记的方法 - 在 E4 上下文中搜索方法参数类型的对象(例如
IWorkbench
) - 对象被注入并执行方法
除非设置了 @Optional
注释,否则在上下文中找不到对象时会抛出异常.
Unless the @Optional
annotation is set, an exception is thrown if no object is found in the context.
要进一步阅读和更详尽的解释,请参阅 Eclipse 4 (e4) 教程第 4 部分 - 依赖注入基础 和 Eclipse 4 (e4)教程第 6 部分:行为注释.
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.
可在 Eclipse 4 Wiki 中找到 Eclipse 4 注释的概述.
An overview of Eclipse 4 annotations can be found at the Eclipse 4 Wiki.
这篇关于Eclipse RCP 4 - 处理程序方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!