接口方法的注释在 Java 7 中继承,但在 Java 8 中不继承
Interface method´s annotations are inherited in Java 7 but not in Java 8
我正在从 Java 7 迁移到 Java 8,我遇到了语言的这种变化。
我有一个带有注释方法的超级接口:
public interface SuperInterface {
@X
SuperInterface getSomething();
}
我有一个带有相同注释方法但返回子接口的子接口:
public interface SubInterface extends SuperInterface {
@X
SubInterface getSomething();
}
当我 运行 这个测试时,它在 Java 8 中失败,但在 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);
}
}
}
}
接口方法的注解在Java7中继承,在Java8中不继承,是吗?
@X 定义为:
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 {
}
据我所知,根据 this,它至少可以在 build 94 of java-8 中使用。因此 这是一个 eclipse 编译器错误(我无法用 javac
重现它)。
您在这里使用协方差,因此将生成两种方法(一种是桥梁):
for (Method method : methods) {
if (method.getAnnotations().length == 0) {
System.out.println("Not present " + method.getName() + " isBridge? " + method.isBridge());
} else {
System.out.println("Present :" + method.getName() + " isBridge? " + method.isBridge());
}
}
但这应该再次起作用,因为错误清楚地表明:具有 运行 时间保留的注释应由 javac 复制到桥接方法 .
输出 javac
:
Present :getSomething isBridge? false
Present :getSomething isBridge? true
输出 eclipse compiler
:
Present :getSomething isBridge? false
Not present getSomething isBridge? true
对于 Eclipse ecj 编译器,这看起来像 Eclipse 错误 495396,它引用了 JDK 6695379。
它被标记为针对 4.7,但 4.7 已经处于发布候选状态,所以我猜它没有进入。
我正在从 Java 7 迁移到 Java 8,我遇到了语言的这种变化。
我有一个带有注释方法的超级接口:
public interface SuperInterface {
@X
SuperInterface getSomething();
}
我有一个带有相同注释方法但返回子接口的子接口:
public interface SubInterface extends SuperInterface {
@X
SubInterface getSomething();
}
当我 运行 这个测试时,它在 Java 8 中失败,但在 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);
}
}
}
}
接口方法的注解在Java7中继承,在Java8中不继承,是吗?
@X 定义为:
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 {
}
据我所知,根据 this,它至少可以在 build 94 of java-8 中使用。因此 这是一个 eclipse 编译器错误(我无法用 javac
重现它)。
您在这里使用协方差,因此将生成两种方法(一种是桥梁):
for (Method method : methods) {
if (method.getAnnotations().length == 0) {
System.out.println("Not present " + method.getName() + " isBridge? " + method.isBridge());
} else {
System.out.println("Present :" + method.getName() + " isBridge? " + method.isBridge());
}
}
但这应该再次起作用,因为错误清楚地表明:具有 运行 时间保留的注释应由 javac 复制到桥接方法 .
输出 javac
:
Present :getSomething isBridge? false
Present :getSomething isBridge? true
输出 eclipse compiler
:
Present :getSomething isBridge? false
Not present getSomething isBridge? true
对于 Eclipse ecj 编译器,这看起来像 Eclipse 错误 495396,它引用了 JDK 6695379。
它被标记为针对 4.7,但 4.7 已经处于发布候选状态,所以我猜它没有进入。