Is there something like Annotation Inheritance in java?

前端 未结 4 1998
走了就别回头了
走了就别回头了 2020-11-27 03:21

I\'m exploring annotations and came to a point where some annotations seems to have a hierarchy among them.

I\'m using annotations to generate code in the backgroun

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 04:06

    In addition to Grygoriys answer of annotating annotations.

    You can check e.g. methods for containing a @Qualifier annotation (or an annotation annotated with @Qualifier) by this loop:

    for (Annotation a : method.getAnnotations()) {
        if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
            System.out.println("found @Qualifier annotation");//found annotation having Qualifier annotation itself
        }
    }
    

    What you're basically doing, is to get all annotations present on the method and of those annotations you get their types and check those types if they're annotated with @Qualifier. Your annotation needs to be Target.Annotation_type enabled as well to get this working.

提交回复
热议问题