Situation:
I have an Entity
interface, some entity implementations, and a Repository
interface, of which I want to produce injectable instances with CDI in the following way:
@Inject @Generic
private Repository<Product, Long> repository;
During initialization of the Weld container (I'm using Weld-SE 2.3.4 Final), I get an exception that indicates my producer wasn't recognized as matching for the injection point:
java.lang.ExceptionInInitializerError
...
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408:
Unsatisfied dependencies for type Repository<Product, Long> with qualifiers @Generic
at injection point [BackedAnnotatedField] @Inject @Generic private idx.contacts.persistence.CDITest.repository
I strongly assume that the cause lies with the generics used (two generic types of which one (the Entity type) depends on the other (the Entity's ID). When I change in so that both generic types are independent, all works well (but would not make sense for the Entity and Repository).
The Entity
interface:
public interface Entity<ID> extends Serializable {
// some methods
}
An example for such an entity:
public class Product implements Entity<Long> {
// some methods
}
CDI qualifier:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public @interface Generic { }
The Repository
interface for a given entity type:
public interface Repository<E extends Entity<ID>, ID> {
// some methods
}
And the Repository
producer, which would then create instances, but isn't recognized as an appropriate producer for the injection example at the top of this question:
@ApplicationScoped
public class RepositoryProducer {
@Produces
@Generic
public <ID, E extends Entity<ID>> Repository<E, ID> create(
final InjectionPoint injectionPoint) {
// would create a repository instance, but is never called
}
}
Is CDI with generics somehow limited to only trivial cases? Could this even work? The generic types are retained (not erased) in the code, I verified this using reflection. Where's the error?