简体   繁体   English

JDK编译器错误

[英]JDK compiler error

Before anyone screams about EOL'ed JDK, I'd like to point out that my question is not about how to compile the following.在有人对 EOL 的 JDK 大喊大叫之前,我想指出我的问题在于如何编译以下内容。 There is a real question here and it's not about JDK 1.5 being EOL'ed...这里有一个真正的问题,它与 JDK 1.5 被 EOL 无关......

The following under JDK 1.5, up to 1.5.0_22 (the last one I could find) produces on my system a compiler error: JDK 1.5 下的以下内容,直到 1.5.0_22(我能找到的最后一个)在我的系统上产生编译器错误:

private Object[] boozinga() {
    boolean b = Math.abs(42) > 0;
    Object[] res = new Object[1];
    res[0] = b ? new int[1] : new String[1];
    return res;
}

Changing the Math.abs(42) > 0 to true allows compilation.Math.abs(42) > 0更改为true允许编译。

Changing the ternary "assignment" to an if/else allows compilation.将三元“赋值”更改为if/else允许编译。

Using JDK 1.6 allows compilation.使用 JDK 1.6 允许编译。

So I was wondering: is there something not legal in the above code under Java 1.5 and that is allowed under Java 1.6?所以我想知道:在 Java 1.5 下的上述代码中是否有不合法的东西,而在 Java 1.6 下是允许的?

Does it crash for those of you that are under Java 1.5 too?对于那些在 Java 1.5 下的人来说,它是否也会崩溃?

The crash says something like this:崩溃是这样说的:

An exception has occured in the compiler (1.5.0_22).编译器 (1.5.0_22) 发生异常。 Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates.在检查 Bug Parade 是否有重复项后,请在 Java 开发人员连接 (http://java.sun.com/webapps/bugreport) 提交错误。 Include your program and the following diagnostic in your report.在报告中包含您的程序和以下诊断。 Thank you.谢谢你。

I take it filling a bug report for an EOL'ed JDK is an exercice in futility but still, I'd still like to know if the above is valid Java 1.5 code or not.我认为为 EOL 的 JDK 填写错误报告是徒劳的,但我仍然想知道上述代码是否有效 Java 1.5 代码。

I think it is legal.我认为这是合法的。 The evidence is that JDK 1.6.0_21 compiles it with options -source 1.5 -target 1.5 .证据是 JDK 1.6.0_21 使用选项-source 1.5 -target 1.5编译它。 Can't you use JDK 1.6 with these options to compile and JRE 1.5 to run?您不能使用带有这些选项的 JDK 1.6 来编译和运行 JRE 1.5 吗?

It crashes for me, too (JDK 1.5.0_12).它对我来说也崩溃了(JDK 1.5.0_12)。 It crashes for me even with:即使有以下情况,它也会为我崩溃:

public Object boozinga() {
    boolean b = true;
    Object res = b ? new int[1] : new String[1];
    return res;
}

The difficulty for the compiler is that the type of b? new int[1]: new String[1]编译器的难点在于b? new int[1]: new String[1] b? new int[1]: new String[1] is java.lang.Object & java.io.Serializable & java.lang.Cloneable . b? new int[1]: new String[1] is java.lang.Object & java.io.Serializable & java.lang.Cloneable .

This looks like AutoBoxing-hell. 这看起来像 AutoBoxing-hell。

Consider using Boolean instead of boolean and Integer[1] instead of int[1]. 考虑使用 Boolean 代替 boolean 和 Integer[1] 代替 int[1]。

Edit: After the clarifying comment of this not being how to correct the code, but how to handle a compiler bug, I would suggest trying with the Eclipse compiler instead.编辑:在澄清这不是如何更正代码,而是如何处理编译器错误之后,我建议尝试使用 Eclipse 编译器。

We want to be able to build with on a plain JRE (as Eclipse can run on a plain JRE too), and therefore I experimented with using ecj35.jar as the compiler.我们希望能够在普通 JRE 上构建(因为 Eclipse 也可以在普通 JRE 上运行),因此我尝试使用 ecj35.jar 作为编译器。 We have been very satisfied.我们已经非常满意了。

The problem here is that the compiler has trouble to decide the type of the expression b? new int[1]: new String[1]这里的问题是编译器很难确定表达式b? new int[1]: new String[1] b? new int[1]: new String[1] . b? new int[1]: new String[1] I had something like this before (with 1.1.8 or 1.2, I think - but with a real error message, not a compiler crash), and then simply used a cast to help the compiler here.我以前有过这样的事情(我认为是 1.1.8 或 1.2 - 但有一个真正的错误消息,而不是编译器崩溃),然后在这里简单地使用强制转换来帮助编译器。

 res[0] = b ? (Object)new int[1] : new String[1];

I didn't look what the language specification says about this - but the compiler should never crash with an exception, it should give a real error message.我没有看语言规范对此有何评论-但编译器永远不应因异常而崩溃,它应该给出真正的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JDK 11的stream / flatMap中的编译器错误 - Compiler error in stream / flatMap using JDK 11 在JDK 7u2上重写接口方法时,出现@override的编译器错误 - Compiler error with @override when overriding an interface method on JDK 7u2 JDK编译器版本问题 - JDK compiler version issue cglib编译器jdk合规性 - cglib compiler jdk compliance 如何将jsp编译器放入jdk? - How to put the jsp compiler in the jdk? JDK编译器“打开”模块失败 - JDK compiler fails for “open” module mvn clean package error: 此环境没有提供编译器。 也许您在 JRE 而不是 JDK 上运行? - mvn clean package error: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 尽管eclipse使用自己的编译器,但错误显示“配置为使用jdk”。 可能是什么原因呢? - Error says 'configure to use a jdk' though eclipse uses its own compiler. What could be the reason for it? 无法在 Intellij 中编译 - 错误:java:不允许使用 --release 从系统模块 jdk.compiler 导出包 - Unable to compile in Intellij - Error:java: exporting a package from system module jdk.compiler is not allowed with --release 为什么我收到这种错误是由于 jdk 和编译器的版本不兼容? - Why am I receiving this sort of error is it due to incompatible versions of jdk and compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM