A iterator with a generic method next()?

Paulo Levi i30817 at gmail.com
Thu Nov 12 13:23:07 PST 2009


Actually i think i found a bug in the compiler with these shenanigans
(one that makes my idea almost useless). One would think
that this class would call the method, catch the exception inside it
and then return the (correct) instance.
Apparently the compiler only introduces the casts at the outer part of
the method. Not inside, so the class cast exception
is never caught.

I'm disappointed. Feet of clay and all that for the compiler.

public class Main{

    public static void main(String[] args) {
        String s = failingCatchingClassCastException(new Callable() {

            public Object call() throws Exception {
                return "Hi BUG";
            }
        });
    }

    public static <T> T failingCatchingClassCastException(Callable t) {
        try {
            Object raw = new Object();
            return (T) raw;
        } catch (ClassCastException ex) {
            System.out.println("WTF!!!");
        }
        try {
            return (T) t.call();
        } catch (Exception ex) {
            return null;
        }
    }
}



More information about the coin-dev mailing list