javaeclipse

Is there a Java program snippet which can compile with a Java 5 compiler on JRE 6 but not Java 6 compiler?


I want to have a source file which can compile with javac / ecj set to Java 5 but not Java 6 (even if the underlying Java runtime is Java 6).

This is to be certain that the compiler level is set correctly in Eclipse 3.5 running with Java 6 installed, but where the result needs to run on a Java 5 installation.

For java 1.4 I could use "enum" as a variable name (which fails under Java 5 and later) but I cannot locate a similar approach for Java 5 versus 6 (and later).

Suggestions?


Solution

  • There's nothing in the Java language that was removed between JDK5 and 6. The only thing which was added, as has been said, was the @Override annotation being allowable on interface methods - no keywords. Hence you are left with library diferences as the only cause of breaking changes, I'm afraid.

    These do exist, even in the core API; in an unusual fit of backwards-compatibility-breaking revelry they changed the signature of some methods on the ExecutorService interface. This was because the generic signatures of the methods were overly restrictive. This was a pure library change (although, being part of java.util, a pretty core library); nothing to do with any language-level modification.

    For example, from JDK5 :

    <T> T invokeAny(Collection<Callable<T>> tasks)
    

    to JDK6:

    <T> T invokeAny(Collection<? extends Callable<T>> tasks)
    

    This means that any program which contained code implementing this interface in JDK5, would not have compiled against JDK6. A snippet is easy to create; just let your IDE create an empty implementation of the JDK5 interface and then build against JDK6.

    Note: that the wildcard was added because the previous version would not have accepted a parameter like List<MyCallable<String>> (i.e. the collection being typed by some subclass of callable) whereas the later version does.