afterburner.fx icon indicating copy to clipboard operation
afterburner.fx copied to clipboard

Injection context ignored for presenter members at first

Open mfuerstenau opened this issue 8 years ago • 3 comments

I am trying to instantiate a view

               Criterium newValue = ...;
               ValueView valueView = new ValueView (s ->
               {
                  if ("criterium".equals (s))
                  {
                     return newValue;
                  }
                  return null;
               });

with provided injection context. ValuePresenter has a member Criterium criterium wgich should be injected the instance supplied by the context. But instead I get an exception, because Criterium has no no-args-constructor. Instead of using the injection context

public static <T> T instantiatePresenter(Class<T> clazz, Function<String, Object> injectionContext) {
   Object presenter = registerExistingAndInject(instanceSupplier.apply(clazz));
   ...

tries an injection of the presenter and subsequently an injection of members. All without usage of the context.

Later instantiatePresenter(Class<T>, Function<String, Object>) loops over the members again and then (but too late) uses the injection context.

I can work around by using setters on the presenter, or supplying a no-args-constructor for Criterium, that way criterium would be replaced by the supplied instance later (but that feels wrong).

Is it working as intended?

mfuerstenau avatar Dec 02 '16 16:12 mfuerstenau

I have the same problem. It works for primitive types or strings (because of https://github.com/AdamBien/afterburner.fx/blob/master/src/main/java/com/airhacks/afterburner/injection/Injector.java#L145) but not for custom/complex objects.

tobiasdiez avatar Jan 13 '17 19:01 tobiasdiez

I am havin the same issue in my project.

adrianopolus avatar Jan 17 '17 11:01 adrianopolus

I have proposed fix #85 for this that prioritizes the injection context over default instantiation. For the detail see issue #65.

paulhudsonx avatar Mar 30 '20 22:03 paulhudsonx