Search code examples
javaannotationsquarkusjava-annotations

Is it possible to make an Annotation "wrapper"?


To prevent an XY Problem I am gonna state my real issue first but my question focuses on my solution for that problem.

I am using Quarkus to build a controller which contains this method:

    @GET
    @Path("image/{user}")
    @Produces("image/png")
    @PermitAll
    public Response getImageUser(@PathParam("user") String user)

For some implementation reasons out of my control this method needs to handle about a dozen different possible @PathParam, so I'd like to do something like this;

    @Path(value = {"image/{user}", "image/{user}/{foo}", "image/{user}/{bar}"})

which is not possible in Quarkus as far as I know. For this purpose I would like to write an Annotation "wrapper" for the @Path Annotation that would accept an array of Strings and then just annotates each of them with @Path and duplicates the method which was annotated with this. Think this;

public @interface Paths {
    public String[] value();
    for String value : values {
        @Path(value)
    }
}

This, of course, does not work for many reasons, (one being that annotations need to be processed) but this question is more if this is even possible. A proof of concept so to speak.


Solution

  • You want multiple @PathParam annotations. You can write these directly in Java, using the repeating annotations feature. There is a slightly better explanation at Baeldung.

    You will still need to make your annotation processor cognizant of the wrapper annotation. It is named as the argument to the @Repeatable meta-annotation.