1

I have a GIN @Inject invocation where I use the optional=true attribute, but it is simply not honored / not working (and an error is being thrown because my class is not bound, which is true).

I'm using GIN via .gwt.xml file like this:

<extend-configuration-property name="gin.ginjector.modules" value="com.example.client.ClientModule" />

In the above example ClientModule extends com.google.gwt.inject.client.AbstractGinModule.

Then I have a class where I requestStaticInjection like this: requestStaticInjection(Example.class);

In Example.java I have:

class Example {
        @com.google.inject.Inject(optional=true)
        protected static SomeUnboundInterface someUnboundInterface = null;
...
}

But I get an error during app startup like this:

[INFO]             [ERROR] No binding found for com.example.SomeUnboundInterface in com.gwtplatform.mvp.client.DesktopGinjector
[INFO]    [ERROR] Errors in 'gen/com/gwtplatform/mvp/client/DesktopGinjectorProvider.java'
[INFO]       [ERROR] Line 8: Failed to resolve 'com.gwtplatform.mvp.client.DesktopGinjector' via deferred binding

Well, that's actually true; My SomeUnboundInterface is indeed not bound; however I was hoping that using the optional=true attribute would work.

Please note that the official documentation does not say that for GIN, this attribute is supported. However, it does say so for GUICE, so I was hoping it would work for GIN as well.

Do you know of any solution so that I don't get an error if a particular dependency is not bound?

Edit: Forgot to mention version. GWT 2.8.1; GIN 2.1; GWTP 1.6

Andrei
  • 1,613
  • 3
  • 16
  • 36

1 Answers1

0

Optional should works, this example compiles and prints 'null' using GWT 2.8.1 and GIN 2.1.2.

static class Foo {
    @Inject(optional = true) Runnable bar;
}

static class MyModule extends AbstractGinModule {
    @Override protected void configure() { bind(Foo.class); }
}

@GinModules(MyModule.class) interface MyGinjector extends Ginjector {
    Foo foo();
}

@Override public void onModuleLoad() {
    L.log(GWT.<MyGinjector>create(MyGinjector.class).foo().bar);
}

But if you add a static injection, like you have in your example, it fails. You can either bind it to something or remove the "requestStaticInjection".

Ignacio Baca
  • 1,538
  • 14
  • 18
  • Sorry, I don't understand (not very familiar with GIN). My code marked with optional is within a library; some clients might choose to provide an implementation which is fine, while some don't which is also fine. Binding to a provider that always returns null won't help because then I can't return the actual instance when I need to. – Andrei Oct 05 '17 at 07:13
  • Ups, sorry my answer was totally wrong! I have updated it! optional should works on methods and fields, so you should have some other problem. I get confused bc this problem actually happens with @Nullable where you need to explicitly bind it to a null instance using providers. – Ignacio Baca Oct 05 '17 at 09:14
  • So my problem is that static injections don't honor the `optional=true` attribute? Hmm, strange; I'll try it out and see. I'd very much like to bind it to some default. However, in the code that uses this library, if I do try to bind something else to it, I'd get a "duplicate key" error. – Andrei Oct 05 '17 at 09:33