关于如何在 Eclipse 4 中使用 IContextFunction 的示例

Cra*_*lus 3 java eclipse osgi eclipse-plugin eclipse-rcp

我阅读了有关org.eclipse.e4.core.contexts.IContextFunction但无法在网上找到实际示例的信息。
我的理解是一个组件实现了一个IContextFunction并且在调用compute另一个对象时被延迟创建。
但是compute我不清楚如何/何时调用该方法。
例如以下内容:

<?xml version="1.0" encoding="UTF-8"?>  
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"   
  name="com.example.e4.rcp.todo.contextservice.translate">  

<implementation class="com.example.e4.rcp.todo.contextservice.Test"/>  

 <service>  
   <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>  
 </service>  

 <property name="service.context.key" type="String"   
   value="com.example.e4.rcp.todo.contextservice.test"/>  

</scr:component>   
Run Code Online (Sandbox Code Playgroud)

必须有人呼吁com.example.e4.rcp.todo.contextservice.testcompute被调用,但目前还不清楚我这是怎么使用的。
有人有示例参考吗?

erd*_*aca 5

它是注入到您的 pojo 中的内容。例如

public class YourPojo {
   @Inject
   @Named("com.example.e4.rcp.todo.contextservice.test")
   private Object yourObject;
}
Run Code Online (Sandbox Code Playgroud)

或者

public class YourPojo {
   @Inject
   public void test(IEclipseContext ctx) {
        Object yourObject = ctx.get("com.example.e4.rcp.todo.contextservice.test");
   }
}
Run Code Online (Sandbox Code Playgroud)

或者

public class YourPojo {
   @Inject
   public void test(@Named("com.example.e4.rcp.todo.contextservice.test") Object yourObject) {
      // consume yourObject
   }
}
Run Code Online (Sandbox Code Playgroud)