Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Reinjection on object removal
Reinjection on object removal [message #1221216] Tue, 17 December 2013 01:11 Go to next message
Alexander Levsha is currently offline Alexander Levsha
Messages: 3
Registered: December 2013
Junior Member
Hello, i recently started to use reinjection and noticed this weird behavior:

when i remove a dependency from a context that is injected into, say, a method - the system detects the "change" in the dependency and tries to reinject it and call the dependant method.
However, the object is already removed, and if i didn't mark the method or the parameter as @Optional - the system cannot find the object to inject and kind of breaks itself.

Is this intended design or maybe an oversight?

Example code:
public class TestPart {
    
    class A {
    }

    @Inject
    IEclipseContext ctx;
    
    int i;
    
    @Inject
    public TestPart(IEclipseContext ctx) {
        ctx.set(A.class, new A()); // 1
    }

    @Inject
    private void reinjectMe(A a) {
        System.out.println("Injected instance of A: " + a);
    }

    @PostConstruct
    public void createUI(Composite parent) {
        Button btn = new Button(parent, SWT.PUSH);
        btn.setText("test");
        btn.addSelectionListener(new SelectionAdapter() {
            
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(i==0) {
                    ctx.set(A.class, new A()); // 2
                    ++i;
                } else {
                    ctx.remove(A.class); // 3
                }
            }
        });
    }
}
Output:
Injected instance of A: TestPart$A@1d51055 // 1
Injected instance of A: TestPart$A@b6377c // 2

// 3
!ENTRY org.eclipse.e4.core 4 0 2013-12-17 09:40:15.378
!MESSAGE Unable to process "TestPart#reinjectMe()": no actual value was found for the argument "A".
Re: Reinjection on object removal [message #1221406 is a reply to message #1221216] Tue, 17 December 2013 11:10 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5260
Registered: July 2009
Senior Member
You have to allow the value to be optional so that the system can inject
NULL.

Tom

On 17.12.13 15:20, Alexander Levsha wrote:
> Hello, i recently started to use reinjection and noticed this weird
> behavior:
>
> when i remove a dependency from a context that is injected into, say, a
> method - the system detects the "change" in the dependency and tries to
> reinject it and call the dependant method.
> However, the object is already removed, and if i didn't mark the method
> or the parameter as @Optional - the system cannot find the object to
> inject and kind of breaks itself.
>
> Is this intended design or maybe an oversight?
>
> Example code:
> public class TestPart {
> class A {
> }
>
> @Inject
> IEclipseContext ctx;
> int i;
> @Inject
> public TestPart(IEclipseContext ctx) {
> ctx.set(A.class, new A()); // 1
> }
>
> @Inject
> private void reinjectMe(A a) {
> System.out.println("Injected instance of A: " + a);
> }
>
> @PostConstruct
> public void createUI(Composite parent) {
> Button btn = new Button(parent, SWT.PUSH);
> btn.setText("test");
> btn.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(SelectionEvent e) {
> if(i==0) {
> ctx.set(A.class, new A()); // 2
> ++i;
> } else {
> ctx.remove(A.class); // 3
> }
> }
> });
> }
> }Output:Injected instance of A: TestPart$A@1d51055 // 1
> Injected instance of A: TestPart$A@b6377c // 2
>
> // 3
> !ENTRY org.eclipse.e4.core 4 0 2013-12-17 09:40:15.378
> !MESSAGE Unable to process "TestPart#reinjectMe()": no actual value was
> found for the argument "A".
>
Re: Reinjection on object removal [message #1221617 is a reply to message #1221406] Tue, 17 December 2013 23:24 Go to previous messageGo to next message
Alexander Levsha is currently offline Alexander Levsha
Messages: 3
Registered: December 2013
Junior Member
Yes, i know i can mark it as @Optional.

But what i'm asking is this: is it right for the system to try to reinject an object that was just removed, possibly by the system itself, and then report an error?
It looks as if it "breaks itself". Maybe it shouldn't try reinjecting removed objects, possibly those that aren't marked @Optional? Or does this feature have its use?

[Updated on: Tue, 17 December 2013 23:27]

Report message to a moderator

Re: Reinjection on object removal [message #1221786 is a reply to message #1221617] Wed, 18 December 2013 07:34 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5260
Registered: July 2009
Senior Member
IMHO it is.

Tom

On 18.12.13 05:24, Alexander Levsha wrote:
> Yes, i know i can mark it as @Optional.
>
> But what i'm asking is this: is it right for the system to try to
> reinject an object that was just removed, possibly by the system itself,
> and then report an error?
> It looks as if it "breaks itself". Maybe it shouldn't try reinjecting
> removed objects, possibly those that aren't marked @Optional? Or does
> this has its use?
Previous Topic:Active Perspective Core Expression
Next Topic:Overriding Ctrl-W behavior
Goto Forum:
  


Current Time: Fri Aug 01 03:57:53 EDT 2014

Powered by FUDForum. Page generated in 0.01886 seconds