fork(1) download
  1. import java.lang.invoke.MethodHandle;
  2. import java.lang.invoke.MethodHandles;
  3. import java.lang.invoke.MethodHandles.Lookup;
  4. import java.lang.invoke.MethodType;
  5.  
  6. class Ideone {
  7. static class Base {
  8. Object f(Object x) {
  9. return Base.class.getSimpleName() + x.hashCode();
  10. }
  11. }
  12.  
  13. static class Ext extends Base {
  14. Object f(String x) {
  15. return Ext.class.getSimpleName() + x.length();
  16. }
  17. }
  18.  
  19. public static Object f(Base x, Object arg) throws Throwable {
  20. Lookup lookup = MethodHandles.lookup();
  21. MethodType mt = MethodType.methodType(Object.class, arg.getClass());
  22. MethodHandle mh = lookup.findVirtual(x.getClass(), "f", mt);
  23. return mh.invoke(x, arg);
  24. }
  25.  
  26. public static void main(String[] args) throws Throwable {
  27. Base x = new Ext();
  28. System.out.println(x.f("1")); // sucks
  29. System.out.println(f(x, "1")); // sig-polymorphic behavior
  30. }
  31. }
  32.  
Success #stdin #stdout 0.18s 320704KB
stdin
Standard input is empty
stdout
Base49
Ext1