Reputation: 324
I am using weld 4.0 and in one of my producer methods, I have this signature:
@Produces
@ConfigurationType
public <T extends IConfiguration> T getConfigurationInstance(InjectionPoint injectionPoint){
<perform some logic, create an instance of class T and return>
}
The objective is to have several configurations. Based on the ConfigurationType (Database/Server/Datasource blah, blah), return a Configuration instance of a specific type, that implements the IConfiguration interface so that I can treat the returned instance in a more readable format like a JDK14 record or its equivalent. In other words, I merely want to say serverConfigurationInstance.port() to read the port, where serverConfigurationInstance is Produced and returned by the Injector logic.
Now, the weld container when booted up tells me this:
An exception occured while executing the Java class. WELD-001562: A producer method return type may not be a type variable or an array type whose component type is a type variable: [ERROR] [EnhancedAnnotatedMethodImpl] @ConfigurationType @Produces public config.ConfigurationInjector.getConfigurationInstance(InjectionPoint) [ERROR] at config.ConfigurationInjector.getConfigurationInstance(ConfigurationInjector.java:0)
The Jakarta weld documentation (4.0.1) or searching for the specific error code or the message did not lead me any further. What wrong am I doing?
Upvotes: 1
Views: 276
Reputation: 40308
If you check out the specs, it says:
If a producer method return type is a type variable or an array type whose component type is a type variable the container automatically detects the problem and treats it as a definition error.
This is the cause of the error you are getting.
The spec seems to allow returning a parameterized type with a type variable from a producer method, as long as the scope is @Dependent
- which is your case. Something like:
@Produces
@ConfigurationType
public <T extends IConfiguration> Supplier<T> getConfigurationInstance(InjectionPoint injectionPoint){
<perform some logic, create a supplier of an instance of class T and return>
}
This way however, the container "magic" (e.g. interception) will happen on the container object, not the instance of IConfiguration
you create, which may or may not be what you want.
Upvotes: 1