Skip to content

Commit

Permalink
ArC AnnotationsTransformer - add more specific builders
Browse files Browse the repository at this point in the history
- and add the convenient thenTransform() method that applies
  transformation automatically
- also mention the builders in the docs
  • Loading branch information
mkouba committed Mar 27, 2023
1 parent eaee7ae commit a8a5c4b
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 107 deletions.
26 changes: 21 additions & 5 deletions docs/src/main/asciidoc/cdi-integration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,32 @@ For example, classes from the _runtime module_ of a Quarkus extension are not in
_Solution_: Use the `AdditionalBeanBuildItem` as described in <<additional_bean_build_item>>.

[[annotations_transformer_build_item]]
== Use Case - I Need To Transform Metadata
== Use Case - I Need To Transform Annotation Metadata

In some cases, it's useful to be able to modify the metadata.
In some cases, it's useful to be able to modify the annotation metadata.
Quarkus provides a powerful alternative to https://jakarta.ee/specifications/cdi/2.0/cdi-spec-2.0.html#process_annotated_type[`jakarta.enterprise.inject.spi.ProcessAnnotatedType`, window="_blank"].
With an `AnnotationsTransformerBuildItem` it's possible to override the annotations that exist on bean classes.

NOTE: Keep in mind that annotation transformers must be produced _before_ the bean discovery starts.

For example, you might want to add an interceptor binding to a specific bean class.
Here is how to do it:
You can use a convenient builder-like API to create a transformer instance:

Builder Example
[source,java]
----
@BuildStep
AnnotationsTransformerBuildItem transform() {
return new AnnotationsTransformerBuildItem(AnnotationsTransformer.appliedToClass() <1>
.whenClass(c -> c.name().toString().equals("org.acme.Bar")) <2>
.thenTransform(t -> t.add(MyInterceptorBinding.class))); <3>
}
----
<1> The transformer is only applied to classes.
<2> Only apply the transformation if the class name equals to `org.acme.Bar`.
<3> Add the `@MyInterceptorBinding` annotation.

The example above can be rewritten with an anonymous class:

.`AnnotationsTransformerBuildItem` Example
[source,java]
Expand All @@ -188,8 +206,6 @@ AnnotationsTransformerBuildItem transform() {
<1> The transformer is only applied to classes.
<2> If the class name equals to `org.acme.Bar` then add `@MyInterceptorBinding`. Don't forget to invoke `Transformation#done()`.

NOTE: Keep in mind that annotation transformers must be produced _before_ the bean discovery starts.

Build steps can query the transformed annotations for a given annotation target via the `TransformedAnnotationsBuildItem`.

.`TransformedAnnotationsBuildItem` Example
Expand Down

0 comments on commit a8a5c4b

Please sign in to comment.