Skip to main content


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 06:11 Go to next message
Alexander Levsha is currently offline Alexander LevshaFriend
Messages: 5
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 16:10 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
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] Wed, 18 December 2013 04:24 Go to previous messageGo to next message
Alexander Levsha is currently offline Alexander LevshaFriend
Messages: 5
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: Wed, 18 December 2013 04:27]

Report message to a moderator

Re: Reinjection on object removal [message #1221786 is a reply to message #1221617] Wed, 18 December 2013 12:34 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
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 Apr 26 07:19:22 GMT 2024

Powered by FUDForum. Page generated in 0.03183 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top