Reinjection on object removal [message #1221216] |
Tue, 17 December 2013 01:11  |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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 #1221786 is a reply to message #1221617] |
Wed, 18 December 2013 07:34  |
Eclipse User |
|
|
|
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?
|
|
|
Powered by
FUDForum. Page generated in 0.04303 seconds