How to Crash Your IDE Using Java Wildcards

In this post I will show you three ways to crash your IDE using Java wildcards. If you are impatient, just download the file InstantCrash.java, open it in your IDE and compile.

Method 1

Just copy and paste this piece of code:

interface List<X> { }
class Crash1 {
    class C<P> implements List<List<? super C<C<P>>>> {}
    public void crash() {
        C<Byte> amIASubtype = null;
        //IDE blowing assignment
        List<? super C<Byte>> superType = amIASubtype;
    }
}

 

Method 2

Type this little piece of code into your IDE:

interface List<X> { }
class Crash2 {
    class C<P extends List<? super C<D>>> implements List<P> {}
    class D implements List<C<? extends List<? super C<D>>>> {}

    public void crash() {
        //IDE blowing type
        C<D> amIAValidType = null;
    }
}

 

Method 3

And finally try this one:

interface List<X> { }
class Crash3 {
    class C<P extends List<List<? extends List<? super C<?>>>>> implements List<P> {}

    public void iAmGonnaCrash(List<? extends List<? super C<?>>>) { }
    public void crash() {
        C<?> amIAValidArgument = null;
        //It's method call this time
        iAmGonnaCrash(amIAValidArgument);
    }
}

 

In all honesty, my Eclipse crashes only on the first piece of code. But in all three code examples javac suffers StackOverflowError, and your IDE will often either crash with it, or crash on its own. The examples were taken from the article by R. Tate, A. Leung, and S. Lerner "Taming Wildcards in Java's Type System" published at PLDI'11. There you can also find explanations why the stack overflow happens. I would like to thank Rafał for sharing the article with me. It's been a fun read!

This post is the first in a mini-series of posts on a mind-blowing (and IDE-blowing) Java feature: generics.

Can your IDE handle the examples?