Skip to content

allow @Registration.types() to specify parameterized bean types #936

@Ladicek

Description

@Ladicek

Is your feature request related to a problem? Please describe.

The @Registration annotation from Build Compatible Extensions provides means of specifying bean types (or observed event types) on which the annotated method should operate. Since @Registration is an annotation, the types can only be Class<?> literals, so there's no direct support for parameterized types.

It is possible to work around that by using @Registration(types = Object.class) and later check bean.types() against a parameterized type created using Types, but a straightforward solution would be preferrable.

Describe the solution you'd like

We can easily achieve that by adding a special case: if the class type present in @Registration.types has a direct superclass type whose erasure is jakarta.enterprise.util.TypeLiteral, we don't use the class type itself, but the type argument to the TypeLiteral. This looks horrible when written like this, but the actual code is relatively pleasant:

@Registration(types = FooOfBar.class)
public void registration(BeanInfo bean) {
    ...
}

static class FooOfBar extends TypeLiteral<Foo<Bar>> {
}

There are 2 edge cases: FooOfBar could extend a raw type (extends TypeLiteral) or the argument to TypeLiteral could contain type variables (class FooOfBar<T> extends TypeLiteral<Foo<T>>). I propose we treat them as definition errors.

Describe alternatives you've considered

N/A

Additional context

This was originally reported in Quarkus: quarkusio/quarkus#52352

I have a specification draft and a prototype implementation in ArC. They are both relatively small changes, so it feels fine to stuff it into CDI 5.0.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions