|
Re: Reinjection on object removal [message #1221406 is a reply to message #1221216] |
Tue, 17 December 2013 16:10 |
Thomas Schindl 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 |
Alexander Levsha 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03183 seconds