Given the type:
public interface Foo<T, X extends A>{...}
I need to use programmatic lookup to find bean that implement the given interface regardless of parameter types. Because of type safe resolution this returns an empty set:
final Set<Bean<?>> foos= BeanManagerProvider.getInstance().getBeanManager().getBeans(Foo.class, new AnyLit());
or via Deltaspike:
org.apache.deltaspike.core.api.provider.BeanProvider.getDependent(Foo.class, new AnyLit())
where AnyLit is:
private static class AnyLit extends AnnotationLiteral<Any> implements Any
{
}
Is there any way to get around this?
Thanks
I think you could make use of TypeLiteral
- a special CDI class which can hold the type and it's parameters. Through this you can specify what exactly you want in approximately this way:
TypeLiteral<Foo<Object, Object>> typeLiteral = new TypeLiteral<Foo<Object, Object>>() {};
BeanManager bm; //assuming you can retrieve BM somehow
bm.getBeans(typeLiteral.getType(), new AnyLit());
Now, this is (I hope) in accord with CDI assignability rules (WARNING: generics ahead, grab a coffee before reading). In short, you want to use Object
as a type so that:
<Bar>
(which are all assignable to Object
)FooImpl<T> implements Foo<T>
will also be assignable to Object
MyFoo implements Foo