samedi 27 juillet 2019

How to get the annotation above the annotation

I added an annotation to the method, and there are other annotations in this annotation. When I try to get the annotation above by the method, unfortunately, the return is always null. I want to know why?

This is the method I defined

@ApiTest
  public void add() {
  }

This is the annotation I defined.

@ValueName("hello word")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiTest {

}
////////////////////////////////////////////////////////
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ValueName {
  String value() default "";
}

The result always returns null when I try to get @ValueName

Class<?> testApi = Class.forName("com.huawei.netty.TestApi");
    Method add = testApi.getMethod("add", null);
    ApiTest apiTest = add.getDeclaredAnnotation(ApiTest.class);
    ValueName valueName = apiTest.getClass().getDeclaredAnnotation(ValueName.class);
    if (valueName != null) {
      System.out.println(valueName.annotationType());
    }

But this way can be obtained

Class<?> apiTest = Class.forName("com.huawei.netty.ApiTest");
    ValueName valueName = apiTest.getDeclaredAnnotation(ValueName.class);
    if (valueName != null) {
      System.out.println(valueName.value());
    }

Can I know why this is?





Aucun commentaire:

Enregistrer un commentaire