我正在从Java 7迁移到Java 8,并且在语言上遇到了这种变化。
我有一个带有注释方法的Superinterface:
public interface SuperInterface {
@X
SuperInterface getSomething();
}我有一个带有相同注释方法的SubInterface,但返回一个子接口:
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);
}
}
}
}接口方法的注释是在Java 7中继承的,而不是在Java 8中继承的,是真的吗?
@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 {
}发布于 2017-05-23 14:12:53
据这说,据我所知,它至少应该与94个java-8构建一起工作。因此,--这是一个eclipse编译器bug (我不能用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());
}
}但是这同样有效,因为bug很清楚地说:注释和运行时保持应该被javac复制到桥接方法。
用javac输出
Present :getSomething isBridge? false
Present :getSomething isBridge? true用eclipse compiler输出
Present :getSomething isBridge? false
Not present getSomething isBridge? true发布于 2017-05-23 14:43:25
对于Eclipse编译器,这看起来像Eclipse 495396,它引用了JDK 6695379。
它被标记为4.7的目标,但是4.7已经处于发布候选状态,所以我想它没有进入。
https://stackoverflow.com/questions/44136834
复制相似问题