gogoWebsite

Inheritance of Java annotations

Updated to 2 days ago
  • Annotations on classes, methods, and parameters can all be inherited
  • If the method is rewritten, the Annotation on the method and parameters will not be inherited, but the annotation of the class will still be inherited.
  • Annotation inheritance cannot be applied to interfaces
  • Annotations on classes, methods, and parameters can all be inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedTest {
    String hello();
}

@InheritedTest(hello = "bob")
public class InheritedParent {

    @InheritedTest(hello = "smith")
    public void doSomething(@InheritedTest(hello = "param") String param){
        System.out.println("Parent do something!");
    }
}

public class InheritedChild extends InheritedParent {


}

test

//Inheritance of class annotations
@Test
public void test01(){
    Class<InheritedChild> inheritedChildClass = InheritedChild.class;
    if(inheritedChildClass.isAnnotationPresent(InheritedTest.class)){
        InheritedTest annotation = inheritedChildClass.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

//Inheritance of method annotations
@Test
public void test02() throws NoSuchMethodException {
    Class<InheritedChild> inheritedChildClass = InheritedChild.class;
    Method doSomething = inheritedChildClass.getMethod("doSomething", new Class[]{});
    if (doSomething.isAnnotationPresent(InheritedTest.class)) {
        InheritedTest annotation = doSomething.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

//Inheritance of parameter annotations
@Test
public void test03() throws NoSuchMethodException {
    Class<InheritedChild> inheritedTestClass = InheritedChild.class;
    Method doSomething = inheritedTestClass.getMethod("doSomething", new Class[]{String.class});
    Parameter[] parameters = doSomething.getParameters();
    for (Parameter parameter : parameters) {
        if (parameter.isAnnotationPresent(InheritedTest.class)) {
            InheritedTest annotation = parameter.getAnnotation(InheritedTest.class);
            System.out.println(annotation.hello());
        }
    }
}

If the method is rewritten, the Annotation on the method and parameters will not be inherited, but the annotation of the class will still be inherited.

public class InheritedChild extends InheritedParent {

    public void doSomething(String param){
        System.out.println("Parent do something!");
    }

}

test

//Inheritance of method annotations
 @Test
 public void test02() throws NoSuchMethodException {
     Class<InheritedChild> inheritedChildClass = ;
     Method doSomething = ("doSomething", new Class[]{});
     if (()) {
         InheritedTest annotation = ();
         (());
     }
 }

Annotation inheritance cannot be applied to interfaces

@InheritedTest(hello = "interface")
public interface InheritedParent02 {
}

public class InheritedChild02 implements InheritedParent02{
}

//Inheritance of interface annotations
@Test
public void test04(){
    Class<InheritedChild02> inheritedChild02Class = InheritedChild02.class;
    if (inheritedChild02Class.isAnnotationPresent(InheritedTest.class)) {
        InheritedTest annotation = inheritedChild02Class.getAnnotation(InheritedTest.class);
        System.out.println(annotation.hello());
    }
}

Reference: /blog/yahaitt-144565