如何从不同的注释中执行具有相同名称的方法
How to execute method with same name from different annotations
尽管我的问题源自注释处理,但我的问题更 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 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 注释相关。
我正在编写一些代码,直到我意识到我不知道实现某些东西的好方法。
程序使用注解处理,我试图获取多个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 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
}