2

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.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
mreynolds0404
  • 183
  • 1
  • 13
  • And not trying to access its value from within the constructor? (as, obviously, the object has to be _constructed_ before its fields are injected) – Thomas Broyer Feb 27 '12 at 02:29

1 Answers1

1

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.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • I added some sample code to my original post with some clarification as to the where I am getting unexpected nulls. – mreynolds0404 Feb 27 '12 at 15:31
  • Updated, check out the suggestion of removing getTestPresenter. If you need that method for other reasons, then it needs to handle injections - that is sort of what @Provides means when you use it. – Colin Alworth Feb 28 '12 at 19:10
  • I see; I misunderstood the purpose of the Provides annotation. Thank you. – mreynolds0404 Feb 29 '12 at 18:57
  • What if I want to inject something like `CurrentUser`? Where and when would I initialize this object? Would I use `@Provided` in this case? What I need/want is `CurrentUser` being injected into my `LoggedInGatekeeper` but all I get is an empty object since only the default constructor gets called instead of `CurrentUser(String name)` for example. :/ – Stefan Falk Jun 17 '16 at 15:56
  • @displayname this could easily be a new question - it is hard to get enough details from your short post. However, assuming you want a `@Provides` method to create `LoggedInGateKeeper`, that means you are alone responsible for the injection. This is not what the asker wanted, and not what you want either. Instead, let `LoggedInGateKeeper` have a constructor that takes a `CurrentUser`, and has `@Inject` on it so Guice knows to call it. The only other thing then is to make sure Guice also knows how to _find_ the current user object... but for more detail, please post a full new question. – Colin Alworth Jun 17 '16 at 19:20
  • @ColinAlworth I posted [another question](http://stackoverflow.com/questions/37906753/how-to-inject-session-user-object-after-bootstrapping) - maybe you can explain how I can do this with GWTP :) – Stefan Falk Jun 19 '16 at 11:34