DWilches
DWilches

Reputation: 23035

HK2 cannot inject Boolean into boolean

I'm providing a boolean for injection to HK2 like this:

binder.bind(disableMyFeature).named("disableMyFeature").to(Boolean.class)

Then I tried to inject it like this:

@Named("disableMyFeature") final boolean disableMyFeature

But I got this exception:

A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=boolean,parent=MyService,qualifiers={@javax.inject.Named(value="disableMyFeature")},position=11,optional=false,self=false,unqualified=null,381397683)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of my.package.MyService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on my.package.MyService

If I change the class of this line to Boolean, the injection works:

@Named("disableMyFeature") final Boolean disableMyFeature

Which is weird because I can mix Integer and int, or Double and double and HK2 can inject them with no issue.

HK2 can even inject this:

binder.bind(disableMyFeature).named("disableMyFeature").to(boolean.class)

into this:

@Named("disableMyFeature") final Boolean disableMyFeature

So, why does HK2 have problems with injecting from Boolean -> boolean if it can inject from boolean -> Boolean and other boxed classes to primitives?

Upvotes: 1

Views: 232

Answers (1)

sarah
sarah

Reputation: 1

Boolean can be null, and it would be undefined whether this becomes true or false. Because of this, it's forbidden.

Upvotes: 0

Related Questions