【问题标题】:How to execute method with same name from different annotations如何从不同的注释中执行具有相同名称的方法
【发布时间】:2018-10-12 20:17:23
【问题描述】:

尽管我的问题源于注释处理,但我的问题更多的是与 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'

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

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 generics annotations jax-rs annotation-processing


【解决方案1】:

在 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
}

【讨论】:

  • 不是我想象的那样,但很干净!谢谢?
猜你喜欢
  • 2017-09-29
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2020-03-30
  • 1970-01-01
相关资源
最近更新 更多