java - 如何从不同的注释执行具有相同名称的方法

标签 java generics annotations jax-rs annotation-processing

尽管我的问题源于注释处理,但我的问题更多的是与 Java 注释相关。

我正在编写一些代码,直到我意识到我不知道实现某些东西的好方法。

程序使用注释处理,我试图获取多个JAX-RS注释的值,让我们以@PathParam和@QueryParam为例。两个注释都有名为 value()

的抽象方法 <小时/>

下面的代码是我不想写的一个例子。我必须对每个 JAX-RS 注释执行此操作。

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

    for(Element element : roundEnv.getElementsAnnotatedWith(PathParam.class)) {
        PathParam parameter = element.getAnnotation(PathParam.class);
        String value = parameter.value();

        // Process data & more program code.
    }

    for(Element element : roundEnv.getElementsAnnotatedWith(QueryParam.class)) {
        QueryParam parameter = element.getAnnotation(QueryParam.class);
        String value = parameter.value();

        // Process data & more program code.
    }

    // Etc... do the same for other JAX-RS annotations.

    return true;
}
<小时/>

我知道使用抽象类你可以执行以下操作:

abstract class Animal { 
    abstract String name();
}

class Dog extends Animal {
    public String name() {
        return "Dog";
    }
}

class Cat extends Animal {
    public String name() {
        return "Cat";
    }
}

Animal animal = new Cat();
System.out.println(animal.name()); // Prints 'Cat'

animal = new Dog();
System.out.println(animal.name()); // Prints 'Dog'

但我不确定如何使用注释完成类似的事情,因为没有可以将其转换为的父类(super class)。 我想它应该是这样的:

ArrayList<Class<? extends Annotation>> annotationsToCheck = 
        new ArrayList<>(Arrays.asList(PathParam.class, QueryParam.class));

for(Class<? extends Annotation> annotationToCheck : annotationsToCheck) {
    for(Element element : roundEnv.getElementsAnnotatedWith(annotationToCheck)) {

        // Somehow cast it to something so that the value() method can be accessed

        // Process data & more program code.

    }

}
<小时/>

我觉得我已经很接近了,但我只是不能完全确定它。有什么好的方法可以解决我的问题吗?

最佳答案

在 Java 9+ 中,不需要转换:

for (Element element : roundEnv.getElementsAnnotatedWithAny​(Set.of(PathParam.class,
                                                                   QueryParam.class))) {
    PathParam pathParam = element.getAnnotation(PathParam.class);
    if (pathParam != null) {
        String value = pathParam.value();
        // Process @PathParam value
    }
    QueryParam queryParam = element.getAnnotation(QueryParam.class);
    if (queryParam != null) {
        String value = queryParam.value();
        // Process @QueryParam value
    }
}

或者,如果您只期望其中之一:

for (Element element : roundEnv.getElementsAnnotatedWithAny​(Set.of(PathParam.class,
                                                                   QueryParam.class))) {
    String value = null;
    PathParam pathParam = null;
    QueryParam queryParam = null;
    if ((pathParam = element.getAnnotation(PathParam.class)) != null) {
        value = pathParam.value();
    } else if ((queryParam = element.getAnnotation(QueryParam.class)) != null) {
        value = queryParam.value();
    }
    // Process value. Exactly one of pathParam and queryParam will be non-null
}

关于java - 如何从不同的注释执行具有相同名称的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52786466/

相关文章:

ios - Swift 3 为 Mapbox 标注添加长按手势

java - API 服务发现设计

java - Spring Boot 和 ElasticSearch 文档中日期转换异常

java - 如何限制字符串生成器的长度

android - Kotlin - 具有通用返回类型的抽象函数

swift - 在泛型中使用类型变量

java - 缓冲与非缓冲,使用哪一个?

java - 构造函数中的虚函数,为什么语言不同?

swift - 进一步限制 Swift 协议(protocol)中的通用函数

generics - F# 泛型类型问题