Package java.lang.invoke

Examples of java.lang.invoke.MethodHandle.invoke()


  public void invoker_call_fallback() throws Throwable {
    DynamicObject object = new DynamicObject();
    MethodHandle fallbackHandle = lookup().findStatic(DynamicObjectTest.class, "fallbackHandle", genericMethodType(2, true));
    object.fallback(fallbackHandle);
    MethodHandle invoker = object.invoker("casper", genericMethodType(3));
    Object result = invoker.invoke(object, "foo", "bar");
    assertThat(result, notNullValue());
    assertThat(result, is((Object) "casper foo bar"));
  }

  @Test
View Full Code Here


  public void invoker_call_fallback_on_setter() throws Throwable {
    DynamicObject object = new DynamicObject();
    MethodHandle fallbackHandle = lookup().findStatic(DynamicObjectTest.class, "fallbackHandle", genericMethodType(2, true));
    object.fallback(fallbackHandle);
    MethodHandle invoker = object.invoker("foo", genericMethodType(2));
    invoker.invoke(object, "bar");
    assertThat(object.get("foo"), is((Object) "bar"));
  }

  @Test
  public void dispatch_calls() throws Throwable {
View Full Code Here

    Object result = env.asFunction("let x = 1\nreturn x + 2");

    assertThat(result, instanceOf(MethodHandle.class));

    MethodHandle func = (MethodHandle) result;
    assertThat((Integer) func.invoke(), is(3));
  }

  @Test
  public void function_with_arguments() throws Throwable {
    EvaluationEnvironment env = new EvaluationEnvironment();
View Full Code Here

    assertThat(result, instanceOf(MethodHandle.class));
    MethodHandle func = (MethodHandle) result;

    assertThat(func.type().parameterCount(), is(2));
    assertThat((Integer) func.invoke(10, 5), is(15));
  }

  @Test
  public void run() throws Throwable {
    EvaluationEnvironment env = new EvaluationEnvironment();
View Full Code Here

    Object result = env.def(code);
    assertThat(result, instanceOf(MethodHandle.class));
    MethodHandle func = (MethodHandle) result;
    assertThat(func.type().parameterCount(), is(2));
    assertThat((Integer) func.invoke(10, 5), is(15));
  }

  @Test
  public void check_source_present_with_compilation_error() {
    EvaluationEnvironment env = new EvaluationEnvironment();
View Full Code Here

  @Test
  @SuppressWarnings("unchecked")
  public void test_asInterfaceInstance() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle handle = lookup.findStatic(MyCallable.class, "hello", genericMethodType(0));
    assertThat((String) handle.invoke(), is("Hello!"));
    Callable<Object> converted = (Callable<Object>) Predefined.asInterfaceInstance(Callable.class, handle);
    assertThat((String) converted.call(), is("Hello!"));
  }

  @Test(expectedExceptions = WrongMethodTypeException.class)
View Full Code Here

  @Test(expectedExceptions = WrongMethodTypeException.class)
  public void test_asInterfaceInstance_wrong_target_type() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle handle = lookup.findStatic(MyCallable.class, "hello", genericMethodType(0));
    assertThat((String) handle.invoke(), is("Hello!"));
    Predefined.asInterfaceInstance(ActionListener.class, handle);
  }

  @Test
  public void test_fun() throws Throwable {
View Full Code Here

  }

  @Test
  public void test_fun() throws Throwable {
    MethodHandle hello = (MethodHandle) Predefined.fun("hello", MyCallable.class, 0);
    assertThat((String) hello.invoke(), is("Hello!"));
  }

  @Test
  public void test_fun_no_arity() throws Throwable {
    MethodHandle hello = (MethodHandle) Predefined.fun("hello", MyCallable.class);
View Full Code Here

  }

  @Test
  public void test_fun_no_arity() throws Throwable {
    MethodHandle hello = (MethodHandle) Predefined.fun("hello", MyCallable.class);
    assertThat((String) hello.invoke(), is("Hello!"));
  }

  @Test(expectedExceptions = NoSuchMethodException.class)
  public void test_fun_fail() throws Throwable {
    MethodHandle hello = (MethodHandle) Predefined.fun("helloz", MyCallable.class, 0);
View Full Code Here

    }
  }

  private static void callRun(Class<?> klass, String[] arguments) throws Throwable {
    MethodHandle main = publicLookup().findStatic(klass, "main", methodType(void.class, String[].class));
    main.invoke(arguments);
  }

  private static void run(RunCommand golo) throws Throwable {
    try {
      golo.classpath.add(".");
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.