Interface method´s annotations are inherited in Java 7 but not in Java 8

Viewed 1126

I´m migrating from Java 7 to Java 8 and I have met with this change in the language.

I have a Superinterface with an annotated method:

public interface SuperInterface {

  @X
  SuperInterface getSomething();
}

I have a SubInterface with the same annotated method but returning a sub-interface:

public interface SubInterface extends SuperInterface {

  @X
  SubInterface getSomething();
}

When I run this test, it fails in Java 8 but not in Java 7:

import java.lang.reflect.Method;

public class Test {

  public static void main(String[] args) {
    final Method[] methods = SubInterface.class.getMethods();
    for (Method method : methods) {
      if (method.getAnnotations().length == 0) {
        throw new RuntimeException("No annotations found for " + method);
      }
    }
  }
}

Interface method´s annotations are inherited in Java 7 but not in Java 8, is it true?

@X is defined as:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface X {  
}
2 Answers
Related