I have an interface with an annotated method. The annotation is marked with @Inherited, so I expect an implementor to inherit it. However, it is not the case:
Code:
import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; import java.util.Arrays; public class Example { public static void main(String[] args) throws SecurityException, NoSuchMethodException { TestInterface obj = new TestInterface() { @Override public void m() {} }; printMethodAnnotations(TestInterface.class.getMethod("m")); printMethodAnnotations(obj.getClass().getMethod("m")); } private static void printMethodAnnotations(Method m) { System.out.println(m + ": " + Arrays.toString(m.getAnnotations())); } } interface TestInterface { @TestAnnotation public void m(); } @Retention(RetentionPolicy.RUNTIME) @Inherited @interface TestAnnotation {} The above code prints:
public abstract void annotations.TestInterface.m(): [@annotations.TestAnnotation()]
public void annotations.Example$1.m(): []
So the question is why does not the obj.m() have @TestAnnotation despite that it implements a method marked with @TestAnnotation which is @Inherited?
@Inheritedas pointed out by @AbhinavSakar and @Gilberto. Sorry about that. I did quite a bit of experimentation but missed the obvious - the docs.org.springframework.core.type.classreading.MetadataReader.