代码之家  ›  专栏  ›  技术社区  ›  Mark

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

  •  1
  • Mark  · 技术社区  · 7 年前

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

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

    @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 回复  |  直到 7 年前
        1
  •  0
  •   Andreas dfa    7 年前

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