Add ConverterProvider type to handle Converter retrieval for parameterized type
See original GitHub issueDescription
Suppose that I create a parameterized type as follow:
public class MyObject<T> {
...
}
If I want to use this parameterized type in the following injection points
@Inject
@ConfigProperty(name = "property1")
private MyObject<Type1> property1;
@Inject
@ConfigProperty(name = "property2")
private MyObject<Type2> property2;
I have two provide a global converter or an implicit converter. But the problem with both of them is they won’t tell nothing me about the type argument of the parameterized type at the given injection point.
Proposal
So my proposal would be to add a ConverterProvider
type as follow to handle this case
public interface ConverterProvider {
public <T> Converter<T> getConverter(Class<T> rawType, Type genericType);
}
Just like Converter
this ConverterProvider
would be discovered by SPI or provided programatically.
Then it would be invoked when resolving injection points to retrieve the right Converter
.
If no ConverterProvider
is available or if it does not provide no eligible Converter
then the current Converter
retrieval mechanism is applied.
The concept of ConverterProvider
is taken form JAX-RS spec where the equivalent is ParamConverterProvider
.
WDYT ? Am I missing something ?
Thanks
Issue Analytics
- State:
- Created 3 years ago
- Comments:21 (12 by maintainers)
Top GitHub Comments
We probably need to stay away from adding a direct dependency to CDI, so my preference would be to add a custom the like JAX-RS.
In summary, as @NicoNes points out the downside of
Type
in the signature is loss of type safety when used programmatically. Whilejavax.enterprise.util.TypeLiteral
does enforce type safety it does not allow to use theType
instance extracted via reflection as it is the case for injection points. In-between these two is using some wrapper type with a generic (likeGenericType
) which does allow wrapping aType
instance (unsafe) but also can be used type safe but cannot enforce it.