gpt4 book ai didi

java - 为什么发布的Java源代码有错误?

转载 作者:搜寻专家 更新时间:2023-11-01 03:28:39 25 4
gpt4 key购买 nike

为了研究下载了JDK源码(6u23 build b5),Eclipsed决定自动构建。令人惊讶的是,它发现了错误。

几个例子。

java.beans.MetaData, line 1365:

ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);

Type mismatch: cannot convert from Annotation to ConstructorProperties

java.awt.AWTEvent, line 220:

AWTAccessor.setAWTEventAccessor(new AWTAccessor.AWTEventAccessor() {

The type new AWTAccessor.AWTEventAccessor(){} must implement the inherited abstract method AWTAccessor.AWTEventAccessor.getAccessControlContext(AWTEvent)

我认为这段代码应该是绝对正确的,即使它不是可以学习的 Java 用法的最佳示例之一。但这甚至无法编译!

更新:我将 java 包导出到单个项目中,删除了 java 包默认导入以避免可能的命名空间冲突,并使用 JVM 1.6.0 来构建它。

最佳答案

这里的问题是泛型的规范随着时间的推移而发展。 :|最新版本的 Sun/Oracle Java 可以正确编译此代码,但是它现在没有意识到它的 IDE 可以编译。 (不幸的是,Eclipse 使用它自己的编译器,它并不总是与 Sun/Oracle 编译器完全相同)

我很确定旧版本的编译器会为这行代码产生错误。

过去,如果一个类型不是泛型,那么所有泛型都会被关闭,即使那没有意义。在这种方法的情况下。

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

// constructor is not a generic type.
private static String[] getAnnotationValue(Constructor constructor) {
ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);

旧的编译器会认为这是一个非泛型方法,因为构造函数不是泛型的。但是,较新的编译器会识别此方法是自包含的,并且该类是否为泛型类型无关紧要。

关于java - 为什么发布的Java源代码有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6573171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com