Question

We are evaluating using GIN in out GWT project and have had good results with typical injection via constructor arguments. The thing we have had difficulty with is field level injection. The fields always end up being null. Does anyone have a good example of how to properly achieve filed level injection with GIN?

Update:

Here is some example code similar to ours:

public class MVP implements EntryPoint {

  public static final HandlerManager EVENT_BUS = new HandlerManager(null);
  private final MVPInjector _injector = GWT.create(MVPInjector.class);

  public void onModuleLoad() {
    // set up layout for module
    RootLayoutPanel.get().add(mainPanel);

    // initialize presenters
    ListPresenter listPresenter = _injector.listPresenter();
    DetailPresenter detailPresenter = _injector.detailPresenter();

    listPresenter.go(listContainer);
    detailPresenter.go(detailContainer);

    // simulate data coming in from RPC call
    EVENT_BUS.fireEvent(new DataReadyEvent(getData()));
  }
}

public class ListPresenter {

  private final HandlerManager _eventBus;
  private final Map<String, Fruit> _myRecords = new HashMap<String, Fruit>();
  private final Display _view;

  @Inject
  public ListPresenter(Display argView, HandlerManager argEventBus) {
    _eventBus = argEventBus;
    _view = argView;
  }

  public void go(HasWidgets argContainer) {
    argContainer.clear();
    argContainer.add(_view.asWidget());
  }

  public interface Display {
    public Widget asWidget();

    public void clear();

    public SingleSelectionModel<ViewProxy> getSelectionModel();

    public void setData(List<ViewProxy> argData);
  }
}

public class DetailPresenter {

  private final HandlerManager _eventBus;
  private final Display _view;
  private Fruit _myRecord;

  @Inject
  private ImagePresenterFactory _imagePresenterFactory;

  @Inject
  private TestPresenter _testPresenter;

  @Inject
  public DetailPresenter(Display argView, HandlerManager argEventBus) {
    _view = argView;
    _eventBus = argEventBus;
  }

  public void go(HasWidgets argContainer) {
    argContainer.clear();
    argContainer.add(_view.asWidget());

    if (_testPresenter != null) {
      _testPresenter.go();
    }
  }

  public interface Display {
    public Widget asWidget();

    public HasText getDescriptionControl();

    public HasClickHandlers getImageControl();

    public HasText getNameControl();

    public HasClickHandlers getSaveControl();

    public void setEnabledControls(boolean argEnabled);
  }
}

public class TestPresenter {

  @Inject
  HandlerManager _eventBus;

  public TestPresenter() {}

  public void go() {
    if (_eventBus != null) {
      _eventBus.toString();
    }
    else {
      // event bus was not injected
    }
  }
}

@GinModules(MVPModule.class)
public interface MVPInjector extends Ginjector {

  DetailPresenter detailPresenter();

  ListPresenter listPresenter();

}

public class MVPModule extends AbstractGinModule {

  @Provides
  @Singleton
  public HandlerManager getEventBus() {
    return MVP.EVENT_BUS;
  }

  @Provides
  public TestPresenter getTestPresenter() {
    return new TestPresenter();
  }

  @Override
  protected void configure() {
    bind(ListPresenter.Display.class).to(ListView.class);
    bind(DetailPresenter.Display.class).to(DetailView.class);
    bind(ImagePresenter.Display.class).to(ImagePopup.class);
    install(new GinFactoryModuleBuilder().build(ImagePresenterFactory.class));
  }

  public interface ImagePresenterFactory {
    public ImagePresenter createImagePresenter(ImageResource argImage);
  }

}

In the above code, I have removed most of the code that doesn't involve GIN. The TestPresenter that the DetailPresenter requires is injected successfully but the HandlerManager that the TestPresenter requires is always null. As you can see, the injected HandlerManager is not used in the constructor.

Was it helpful?

Solution

Update, looking at sample code:

@Provides
public TestPresenter getTestPresenter() {
  return new TestPresenter();
}

Because you are creating it yourself, it assumes you've dealt with any injections. Remove this method, and it will invoke the default constructor (injecting there if needed), then visit any other injection site.

One other issue you could be running into: there are several HandlerManager impls, make sure all of your references to HandlerManager use the same package.


Original answer:

They will be null when the constructor is running, but this makes sense - how could they be any other value, when the injector hasn't had a chance to assign all the fields yet. Consider how this could would run (expressed here as questionably legal java, as fields may not be public):

InstanceToInject instance = new InstanceToInject(...);
instance.field = provideFieldValue();

By the time the field is even eligible to be assigned, your constructor has already run.

If the field is null when another method is run, make sure that method is not being run by the constructor, but is after injection has finished its work. Other cases where it may yet be null would be @Inject annotated setters.

Assuming it is none of those cases (easiest way to check is by setting a breakpoint, and making sure the injector isn't in the call stack), be certain that the field does have an @Inject, and that it isn't bound to a null instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top