方法,为java 1.7.0_79和java 1.7.0_80版本提供不同的输出
在使用java 1.7.0_79和1.7.0_80版本进行编译和运行时,相同代码的输出不同。测试代码段如下所示:方法,为java 1.7.0_79和java 1.7.0_80版本提供不同的输出,java,reflection,annotations,Java,Reflection,Annotations,在使用java 1.7.0_79和1.7.0_80版本进行编译和运行时,相同代码的输出不同。测试代码段如下所示: interface Constraint<T> extends Serializable { T getValue(); } @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @interface NonZero { } public class Reflection
interface Constraint<T> extends Serializable {
T getValue();
}
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@interface NonZero {
}
public class ReflectionTest {
@SuppressWarnings("serial")
public static void main(String[] args) {
Constraint<Integer> c = new Constraint<Integer>() {
@Override
@NonZero
public Integer getValue() {
return null;
}
};
for(Method m : c.getClass().getDeclaredMethods()) {
System.out.println("Method :: " + m.toGenericString());
if(m.getAnnotations().length == 0){
System.out.println("No annotations detected");
} else {
for(Annotation o: m.getAnnotations()) {
System.out.println("Annotation :: " + o);
}
}
}
}
}
1.7.0_80的输出为:
Method :: public java.lang.Integer ReflectionTest$1.getValue()
Annotation :: @NonZero()
Method :: public java.lang.Object ReflectionTest$1.getValue()
Annotation :: @NonZero()
在使用jdk-1.7.0_80版本编译的代码中,注释应用于父方法,而早期版本中并非如此。为什么会有这种差异
Method :: public java.lang.Integer ReflectionTest$1.getValue()
Annotation :: @NonZero()
Method :: public java.lang.Object ReflectionTest$1.getValue()
Annotation :: @NonZero()