简体   繁体   English

是否可以制作注释“包装器”?

[英]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.为了防止出现XY 问题,我首先要解决我真正的问题 state,但我的问题集中在我对该问题的解决方案上。

I am using Quarkus to build a controller which contains this method:我正在使用 Quarkus 构建包含此方法的 controller:

    @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;由于某些我无法控制的实现原因,此方法需要处理大约十几种不同的可能@PathParam ,所以我想做这样的事情;

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

which is not possible in Quarkus as far as I know.据我所知,这在 Quarkus 中是不可能的。 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.为此,我想为@Path Annotation 编写一个Annotation“包装器”,它将接受一个字符串数组,然后用@Path 对它们中的每一个进行注释,并复制用这个注释的方法。 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.可以说是概念证明。

You want multiple @PathParam annotations.您需要多个@PathParam注释。 You can write these directly in Java, using the repeating annotations feature .您可以使用重复注释功能直接在 Java 中编写这些。 There is a slightly better explanation at Baeldung . 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.它被命名为@Repeatable元注释的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM