Because fuck dynamic dispatch, that's why.
Aug 24, 2011       1:04 pm

I'm angry at Java 6 for letting this happen.

I realized this while writing a bug report for Mockito.


public class Wtf {

  public static void main(String[] args) {
    B b = wtfB();
    System.out.println(b.foo());
    System.out.println(((A) b).foo());
  }

  interface A {
    Object foo();
  }

  interface B extends A {
    @Override
    String foo();
  }

  static B wtfB() {
    return (B)
    java.lang.reflect.Proxy.newProxyInstance(
      ClassLoader.getSystemClassLoader(),
      new Class[] { A.class, B.class },
      new WtfInvocationHandler());
  }

  static class WtfInvocationHandler
  implements java.lang.reflect.InvocationHandler {

    @Override
    public Object invoke(
        Object proxy,
        java.lang.reflect.Method method,
        Object[] args) {

      return method.getDeclaringClass()
          .getSimpleName();
    }
  }
}

new comment >
Re: Because fuck dynamic dispatch, that's why.
by Anonymous on Dec 19, 2011, 9:59 am
whaaaaat the? who's blog??