Quantcast
Channel: Scratch Where It's Itching
Viewing all articles
Browse latest Browse all 73

Java 8 Compilation Strangeness

$
0
0
A colleague of mine (thanks Clement) showed me some strange issues his team met when they decided to move their project to Java 8. These issues did not appear while compiling with Eclipse, but showed when moving to the command line compiler.

The first issue compiles correctly on my compiler, but I use an older version than my colleague's (he tried with 1.8.0_20 and 1.8.0_40-ea-b06). On his machine, however, compilation fails. The workaround he found was to reverse the order of the getName methods!

publicabstractclass Entity {publicabstract String getName(String type);publicfinal String getName() {return getName("DEFAULT");
        } 
    } publicclass MyClass<E extends Entity> {protectedboolean test(E value, String name, String type) {return name.equals((type != null) ? value.getName(type) : value.getName());
        } 
    } 

The second issue compiles fine, but when running the program, if fails with a ClassCastException. The compiler seems to have linked to the wrong class constructor.

import java.util.Collection;publicclass Main {
        @SuppressWarnings("unchecked")publicstatic<U extends B>U get() {return (U)new B();
        } publicstaticvoid main(String[] args) {
            A<B> a = new A<>(get());
        } staticclass A<T> {
            A(Collection<? extends T> lst) {
                System.out.println("Bad constructor");
            } 
            A(T e) {
                System.out.println("ok");
            } 
        } staticclass B {
        } 
    } 

Does someone else experience those strange issues?


Viewing all articles
Browse latest Browse all 73

Trending Articles