3

I read about org.eclipse.e4.core.contexts.IContextFunction but could not find online an actual example.
My understanding is that a component implements an IContextFunction and on calling compute another object is lazily created.
But how/when the compute method is called it is not clear to me.
For example with the following:

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

someone must call for the com.example.e4.rcp.todo.contextservice.test for compute to be called but it is unclear to me how this is used.
Does anyone have an example reference?

1 Answer 1

5

It is what gets injected into your pojos. E.g.

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

OR

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

OR

public class YourPojo {
   @Inject
   public void test(@Named("com.example.e4.rcp.todo.contextservice.test") Object yourObject) {
      // consume yourObject
   }
}
7
  • Yes I found the second example you post after googling a lot.But could not find anything about the first example.When will the Inject of the Named variable happen for your first example?
    – Cratylus
    Commented Oct 21, 2012 at 20:39
  • It happens whenever your pojo is instantiated and under the DI engine's control. Though, if you know how the second example is meant to be used, the first is almost the same. The difference is that you just can do a lot more in the test() method than just setting/injecting the @Named field. You can consume your "com.example.e4.rcp.todo.contextservice.test" object in a command handler, for instance... Commented Oct 21, 2012 at 20:43
  • When you say POJO are you referring to yourObject or the class that has yourObject as a member? Because if I do ctx.get("com.example.e4.rcp.todo.contextservice.test"); the member yourObject will not be automatically injected, right?That is why you are explicitly populating in the second example. Is the first example meant to work in a case where the object having yourObject as member is instantiated after the ctx.get("com.example.e4.rcp.todo.contextservice.test"); is called somewhere in the code? Still not clear on the first example.
    – Cratylus
    Commented Oct 21, 2012 at 20:56
  • POJO = the class that needs yourObject. You may want to have a look at this: wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection (see "Injection Order" at the bottom). The field injection will happen before the method injection. I.e. if you debug and add a breakpoint to test(), the field should have already been injected. Commented Oct 21, 2012 at 21:04
  • So you are saying that calling ctx.get is mandatory for case 1 to work, right?
    – Cratylus
    Commented Oct 28, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.