question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ConverterProvider type to handle Converter retrieval for parameterized type

See original GitHub issue

Description

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:open
  • Created 3 years ago
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
radcortezcommented, Jul 7, 2020

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.

0reactions
jbeecommented, Jul 13, 2020

In summary, as @NicoNes points out the downside of Type in the signature is loss of type safety when used programmatically. While javax.enterprise.util.TypeLiteral does enforce type safety it does not allow to use the Type instance extracted via reflection as it is the case for injection points. In-between these two is using some wrapper type with a generic (like GenericType) which does allow wrapping a Type instance (unsafe) but also can be used type safe but cannot enforce it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · eclipse/microprofile-config - GitHub
Why does a @ConfigProperties annotation on a class prevent Weld SE bean ... Add ConverterProvider type to handle Converter retrieval for ...
Read more >
With Gson and Retrofit2 Is there a way to write a "generic ...
Retrofit is doing the serialization part by delegating it to the Converter, you you can add a specific one to the builder by...
Read more >
Generic Methods - Java™ Tutorials
The way to do deal with these problems is to use generic methods. Just like type declarations, method declarations can be generic—that is,...
Read more >
4.1. Generic Types - Java in a Nutshell, 5th Edition [Book]
A generic type is defined using one or more type variables and has one or more … ... This type checking prevents you...
Read more >
Programming With Java Generics - Angelika Langer
What does the type parameter of class java.lang.Class mean? ... type? How do I retrieve the representation of a generic method?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found