Discussion:
Guice multiBind not working when use @Inject(optional=true)
Dilanka Muthukumarana
2018-10-03 11:47:52 UTC
Permalink
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
--
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.
Dilanka Muthukumarana
2018-10-03 11:59:33 UTC
Permalink
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
Post by Dilanka Muthukumarana
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
/**
* 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);
}
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
--
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
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
<https://groups.google.com/d/msgid/google-guice/ee4a5435-a317-497a-a044-4175f4c9340b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
~~~~~~~~
Regards,
Dilanka Muthukumarana
--
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/CANLbWWu%3D%2BHwscm3a5dahL8%2B3oHcqfwmWHYwbRs4wkLz%2B28Rdtg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Dilanka Muthukumarana
2018-10-03 13:55:51 UTC
Permalink
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
Post by Dilanka Muthukumarana
Hi Again,
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
Post by Dilanka Muthukumarana
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
/**
* 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);
}
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
--
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
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
<https://groups.google.com/d/msgid/google-guice/ee4a5435-a317-497a-a044-4175f4c9340b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
~~~~~~~~
Regards,
Dilanka Muthukumarana
--
~~~~~~~~
Regards,
Dilanka Muthukumarana
--
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/CANLbWWudjS7Q7oRxH1jJFq9DcqbTLFj3NGFoL_XVqu5xc4F7sA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Dilanka Muthukumarana
2018-10-04 07:59:24 UTC
Permalink
I guess this bug is the reason:

*https://github.com/google/guice/issues/847
<https://github.com/google/guice/issues/847>*

*Can I know whether it is fixed or not?*

*Regards,*
*Dilanka*
Post by Dilanka Muthukumarana
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
Post by Dilanka Muthukumarana
Hi Again,
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
Post by Dilanka Muthukumarana
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
/**
* 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);
}
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
--
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
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
<https://groups.google.com/d/msgid/google-guice/ee4a5435-a317-497a-a044-4175f4c9340b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
~~~~~~~~
Regards,
Dilanka Muthukumarana
--
~~~~~~~~
Regards,
Dilanka Muthukumarana
--
~~~~~~~~
Regards,
Dilanka Muthukumarana
--
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/CANLbWWuzxvYk%3D9ZOxjFE3Prq3Lt22HKsbR94O_L5420xc4oBHA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...