简体   繁体   中英

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. There is a real question here and it's not about JDK 1.5 being EOL'ed...

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:

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.

Changing the ternary "assignment" to an if/else allows compilation.

Using JDK 1.6 allows compilation.

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?

Does it crash for those of you that are under Java 1.5 too?

The crash says something like this:

An exception has occured in the compiler (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. 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.

I think it is legal. The evidence is that JDK 1.6.0_21 compiles it with options -source 1.5 -target 1.5 . Can't you use JDK 1.6 with these options to compile and JRE 1.5 to run?

It crashes for me, too (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] is java.lang.Object & java.io.Serializable & java.lang.Cloneable .

This looks like AutoBoxing-hell.

Consider using Boolean instead of boolean and Integer[1] instead of 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.

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. 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] . 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.

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM