Guice multiBind not working when use @Inject(optional=true)

178 views
Skip to first unread message

Dilanka Muthukumarana

unread,
Oct 3, 2018, 7:47:52 AM10/3/18
to google-guice
Hi All,


Currently I am working in Guice injection related development and experiencing injection problem with multiBind  with optional= true.

In guice-4.0, I have seen skipping injection if it is optional.

code location: MembersInjectorStore.java
Method: 
/**
 * Returns the injectors for the specified injection points.
*/
ImmutableList<SingleMemberInjector> getInjectors(
Set<InjectionPoint> injectionPoints, Errors errors) {
List<SingleMemberInjector> injectors = Lists.newArrayList();
for (InjectionPoint injectionPoint : injectionPoints) {
try {
Errors errorsForMember = injectionPoint.isOptional()
? new Errors(injectionPoint)
: errors.withSource(injectionPoint);
SingleMemberInjector injector = injectionPoint.getMember() instanceof Field
? new SingleFieldInjector(this.injector, injectionPoint, errorsForMember)
: new SingleMethodInjector(this.injector, injectionPoint, errorsForMember);
injectors.add(injector);
} catch (ErrorsException ignoredForNow) {
// ignored for now
}
}
return ImmutableList.copyOf(injectors);
}

My code is like below:
private Set<WarningInfoClientPlugin> warningInfoClientPlugins;

@Inject(optional = true)
public void setWarningPlugins(final Set<WarningInfoClientPlugin> warningInfoClientPlugins)
{
this.warningInfoClientPlugins = warningInfoClientPlugins;
}

Code is perfectly working without optional = true attribute.
Appreciate your help on this. Thank you.

Regards,
Dilanka

Dilanka Muthukumarana

unread,
Oct 3, 2018, 7:59:47 AM10/3/18
to google...@googlegroups.com
Hi Again,

I am using configuration as below:

multibind(WarningInfoClientPlugin.class, EPedDoseValidationWarningPlugin.class);

 protected final <T> void multibind(final Class<T> classType, final Class<?>... implementations)
  {
    final Multibinder<T> pluginBinder = Multibinder.newSetBinder(binder(), classType);

    for (final Class<?> impl : implementations)
    {
      pluginBinder.addBinding().to((Class<T>)impl);
    }
  }

Thanks and Regards,
Dilanka


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/ee4a5435-a317-497a-a044-4175f4c9340b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
  
~~~~~~~~
Regards,                              
Dilanka Muthukumarana                                                      
      

Dilanka Muthukumarana

unread,
Oct 3, 2018, 9:56:05 AM10/3/18
to google...@googlegroups.com
Hi All,

By further debug information, I have found that below error is throwing.

Unable to create binding for java.util.Set< <package>  .WarningInfoClientPlugin>. 
It was already configured on one or more child injectors or private modules
    bound at <package>.AbstractInjectModule.multibind(AbstractInjectModule.java:130) 
(via modules: com.google.inject.util.Modules$OverrideModule -> com.google.inject.util.Modules$OverrideModule -> com.google.inject.util.Modules$CombinedModule ->  <package> .Configuration -> com.google.inject.multibindings.Multibinder$RealMultibinder)
  If it was in a PrivateModule, did you forget to expose the binding?

Any idea about it, Implementation of class is at another module, do I need to add another configuration or something like that?

Regards,
Dilanka

Dilanka Muthukumarana

unread,
Oct 4, 2018, 3:59:39 AM10/4/18
to google...@googlegroups.com
I guess this bug is the reason:


Can I know whether it is fixed or not?

Regards,
Dilanka
Reply all
Reply to author
Forward
0 new messages