lea*_*qui 18 java eclipse eclipse-jdt java-7 java-8
我正在从Java 7迁移到Java 8,我已经遇到了这种语言的变化.
我有一个带有注释方法的Superinterface:
public interface SuperInterface {
@X
SuperInterface getSomething();
}
Run Code Online (Sandbox Code Playgroud)
我有一个SubInterface与相同的注释方法,但返回一个子接口:
public interface SubInterface extends SuperInterface {
@X
SubInterface getSomething();
}
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,它在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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
接口方法的注释在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 {
}
Run Code Online (Sandbox Code Playgroud)
Eug*_*ene 21
至于我可以告诉它应该工作在至少构建Java-8 94,根据本.因此这是一个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());
}
}
Run Code Online (Sandbox Code Playgroud)
但同样这应该有效,因为错误清楚地说明:运行时保留的注释应该由javac复制到桥接方法.
输出javac
:
Present :getSomething isBridge? false
Present :getSomething isBridge? true
Run Code Online (Sandbox Code Playgroud)
输出eclipse compiler
:
Present :getSomething isBridge? false
Not present getSomething isBridge? true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1010 次 |
最近记录: |