有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从不同注释中执行同名方法

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

    }

}

我觉得我离得很近,但我就是摸不着。有什么好办法解决我的问题吗


共 (1) 个答案

  1. # 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
    }