Firstly, is doing such thing a good practice ? I tried what seems to be the right way for me but wasn't successful :
public class FormViewImpl extends CompositeView implements HasUiHandlers<C>, FormView {
public interface SettlementInstructionsSearchFormViewUiBinder extends UiBinder<Widget, SettlementInstructionsSearchFormViewImpl> {}
@Inject
static FormViewImpl uiBinder;
@Inject
static Provider<DateEditorWidget> dateEditorProvider;
@UiField(provided = true)
MyComponent<String> myComp;
@UiField
DateEditorWidget effectiveDateFrom;
// .. other fields
@Inject
public FormViewImpl () {
myComp = new MyComponent<String>("lol");
if (uiBinder == null)
uiBinder = GWT.create(SettlementInstructionsSearchFormViewUiBinder.class);
initWidget(uiBinder.createAndBindUi(this));
}
@UiFactory
DateEditorWidget createDateEditor() {
return dateEditorProvider.get();
}
}
What other things than a class with no arguments is required ? In my company's project the same kind of code works at some other place. Sorry from the high level of noob here... If you guys had any pointers it would be nice.
Thanks
Two issues:
First, two of your @Inject fields are static - have you done anything to make static fields be injected? Static fields don't get set when Gin (or Guice) creates new instances, those have to be set once and done. As they are static, they will never be garbage collected - this may be okay with you, or it might be a problem, and you should change them to instance fields. If you want to keep them static, then you must invoke requestStaticInjection
in your module to ask Gin to initialize them when the ginjector is created.
Next, if you do choose to remove static, the uiBinder
field must still be null in that constructor, because the fields can't have been injected yet! How do you set a field on an object that you haven't yet created? That's what you are expecting Gin to be able to do. Instead, consider passing that as an argument into the @Inject
decorated constructor. You don't even need to save it as a field, since the widget will only use it the one time.