-
Notifications
You must be signed in to change notification settings - Fork 33
Guice type conversions for constant bindings don't work for primitive types #35
Comments
Below are the versions of the libraries I'm using:
|
I've look into this for a little while and I have some clues as to what is happening: First, this only happens if the injectee has primitive type. The code below works as expected (assuming the setup from my previous comment): package com.foo.service.resources;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class FooResource {
private final int bar;
@Inject
public FooResource(@Named("named.bar") Integer bar) {
this.bar = bar;
}
@GET
public int getBar() {
return bar;
}
} Looking at the case when the injectee is a primitive, say an
locator.getServiceHandle(getDescriptors(BuilderHelper.createNameAndContractFilter("int", "named.bar")).get(0)).getService() Which indeed does return the expected value (3).
Observations
SystemInjecteeImpl(requiredType=int,parent=FooResource,qualifiers={},position=0,optional=false,self=false,unqualified=null,777637353) This causes the internal cache of the ServiceLocator to be used, instead of looking up the descriptor in the internal mapping.
Any ideas how to fix this? The workaround for now is to not inject named primitives, and instead boxed types. /cc @rkapsi |
According to the
Binder
documentation:However, this seems to not work in jersey2-guice. Below is code to reproduce this:
Given that the bound instance is a string, it should be possible to automatically have it converted to an int:
Unfortunately this results in the following exception being thrown:
The text was updated successfully, but these errors were encountered: