static {
    try {
      MethodHandles.Lookup lookup = MethodHandles.lookup();

      BIND_CALL_SITE =
          lookup.findStatic(
              InvokeDynamicSupport.class,
              "bindCallSite",
              methodType(MethodHandle.class, MethodCallSite.class));
      BIND_INIT_CALL_SITE =
          lookup.findStatic(
              InvokeDynamicSupport.class,
              "bindInitCallSite",
              methodType(MethodHandle.class, RoboCallSite.class));
      MethodHandle cleanStackTrace =
          lookup.findStatic(
              RobolectricInternals.class,
              "cleanStackTrace",
              methodType(Throwable.class, Throwable.class));
      EXCEPTION_HANDLER =
          filterArguments(throwException(void.class, Throwable.class), 0, cleanStackTrace);
      GET_SHADOW =
          lookup.findVirtual(ShadowedObject.class, "$$robo$getData", methodType(Object.class));
    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new AssertionError(e);
    }
  }
  public static void main(String[] args) throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle handle = lookup.findStatic(FunctionProgramming.class, "print", typeCallback);
    forEach(new Object[] {1, 2, 4}, handle);

    MethodHandle handle2 = lookup.findStatic(FunctionProgramming.class, "add", typeCallback2);
    Object[] result = map(new Object[] {1, 2, 4}, handle2);
    forEach(result, handle);

    MethodHandle handle3 = lookup.findStatic(FunctionProgramming.class, "reduce", typeCallback3);
    System.out.println(reduce(new Object[] {1, 2, 4}, 0, handle3));

    System.out.println(FunctionProgramming.add5(8));
  }
 public static MethodHandle getMH(Class<?> c) {
   try {
     return lookup.findStatic(C.class, "m_" + c.getSimpleName(), methodType(c, String.class));
   } catch (NoSuchMethodException | IllegalAccessException e) {
     throw new RuntimeException(e);
   }
 }
 public static int add5(int a) throws Throwable {
   MethodHandles.Lookup lookup = MethodHandles.lookup();
   MethodType type = MethodType.methodType(int.class, int.class, int.class);
   MethodHandle mhAdd = lookup.findStatic(FunctionProgramming.class, "add", type);
   MethodHandle mh = curry(mhAdd, 5);
   return (int) mh.invoke(a);
 }
Пример #5
0
  static {
    try {
      MethodHandles.Lookup lookup = MethodHandles.lookup();
      THROW_OR_RETURN =
          lookup.findStatic(
              TestCase.class,
              "throwOrReturn",
              MethodType.methodType(Object.class, Object.class, Throwable.class));
      CATCHER =
          lookup.findStatic(
              TestCase.class, "catcher", MethodType.methodType(Object.class, Object.class));
      FAKE_IDENTITY =
          lookup.findVirtual(
              TestCase.class, "fakeIdentity", MethodType.methodType(Object.class, Object.class));

    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new Error(e);
    }
    PartialConstructor[] constructors = {
      create(Object.class, Object.class::cast),
      create(String.class, Objects::toString),
      create(int[].class, x -> new int[] {Objects.hashCode(x)}),
      create(long.class, x -> Objects.hashCode(x) & (-1L >>> 32)),
      create(void.class, TestCase::noop)
    };
    Throwable[] throwables = {
      new ClassCastException("testing"),
      new java.io.IOException("testing"),
      new LinkageError("testing")
    };
    List<Supplier<TestCase>> list =
        new ArrayList<>(constructors.length * throwables.length * ThrowMode.values().length);
    //noinspection unchecked
    for (PartialConstructor f : constructors) {
      for (ThrowMode mode : ThrowMode.values()) {
        for (Throwable t : throwables) {
          list.add(f.apply(mode, t));
        }
      }
    }
    CONSTRUCTORS = Collections.unmodifiableList(list);
  }
Пример #6
0
  static {
    final MethodHandles.Lookup lookup = MethodHandles.lookup();

    MethodHandle methodHandle = null;
    try {
      methodHandle = lookup.findStatic(Thread.class, "onSpinWait", methodType(void.class));
    } catch (final Exception ignore) {
    }

    ON_SPIN_WAIT_METHOD_HANDLE = methodHandle;
  }
  public static void main(String[] args) throws Throwable {
    Object x, y;
    String s;
    int i;
    MethodType mt;
    MethodHandle mh;
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    // mt is (char,char)String
    mt = MethodType.methodType(String.class, char.class, char.class);
    mh = lookup.findVirtual(String.class, "replace", mt);
    s = (String) mh.invokeExact("daddy", 'd', 'n');
    System.out.println("Result: " + s);
    // invokeExact(Ljava/lang/String;CC)Ljava/lang/String;

    // weakly typed invocation (using MHs.invoke)
    s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
    System.out.println("Result: " + s);
    // mt is (Object[])List

    mt = MethodType.methodType(java.util.List.class, Object[].class);
    mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
    assert (mh.isVarargsCollector());
    x = mh.invoke("one", "two");
    // ArrayList
    System.out.println("x: " + x + " type: " + x.getClass().getCanonicalName());

    // invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
    // assertEquals(x, java.util.Arrays.asList("one","two"));

    // mt is (Object,Object,Object)Object
    mt = MethodType.genericMethodType(3);
    mh = mh.asType(mt);
    x = mh.invokeExact((Object) 1, (Object) 2, (Object) 3);
    // invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
    // 1,2,3
    System.out.println("x: " + x + " type: " + x.getClass().getCanonicalName());

    // assertEquals(x, java.util.Arrays.asList(1,2,3));
    // mt is ()int
    mt = MethodType.methodType(int.class);
    mh = lookup.findVirtual(java.util.List.class, "size", mt);
    i = (int) mh.invokeExact(java.util.Arrays.asList(1, 2, 3));
    // invokeExact(Ljava/util/List;)I
    assert (i == 3);

    mt = MethodType.methodType(void.class, String.class);
    mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
    mh.invokeExact(System.out, "Hello, world.");
    // invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
  }
Пример #8
0
 public static MethodHandle resolveCallHandle() {
   try {
     return lookup.findStatic(
         InterfaceCallSite.class,
         "call",
         MethodType.methodType(
             Object.class, // return type
             InterfaceCallSite.class, // interface method being invoked
             Object.class, // receiver
             Object[].class) // args
         );
   } catch (Throwable e) {
     throw new IllegalStateException(e);
   }
 }