Package java.lang.invoke.MethodHandles

Examples of java.lang.invoke.MethodHandles.Lookup


     * It will make an up-call to this method.  (Do not change the name or signature.)
     */
    static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind,
                                                 Class<?> defc, String name, Object type) {
        try {
            Lookup lookup = IMPL_LOOKUP.in(callerClass);
            return lookup.linkMethodHandleConstant(refKind, defc, name, type);
        } catch (ReflectiveOperationException ex) {
            Error err = new IncompatibleClassChangeError();
            err.initCause(ex);
            throw err;
        }
View Full Code Here


  }

  public static Object fallback(FunctionCallSite callSite, Object[] args) throws Throwable {
    String functionName = callSite.name;
    MethodType type = callSite.type();
    Lookup caller = callSite.callerLookup;
    Class<?> callerClass = caller.lookupClass();

    MethodHandle handle = null;
    Object result = findStaticMethodOrField(callerClass, functionName, args);
    if (result == null) {
      result = findClassWithStaticMethodOrField(callerClass, functionName, args);
    }
    if (result == null) {
      result = findClassWithStaticMethodOrFieldFromImports(callerClass, functionName, args);
    }
    if (result == null) {
      result = findClassWithConstructor(callerClass, functionName, args);
    }
    if (result == null) {
      result = findClassWithConstructorFromImports(callerClass, functionName, args);
    }
    if (result == null) {
      throw new NoSuchMethodError(functionName);
    }

    Class[] types = null;
    if (result instanceof Method) {
      Method method = (Method) result;
      checkLocalFunctionCallFromSameModuleAugmentation(method, callerClass.getName());
      types = method.getParameterTypes();
      if (method.isVarArgs() && isLastArgumentAnArray(types.length, args)) {
        handle = caller.unreflect(method).asFixedArity().asType(type);
      } else {
        handle = caller.unreflect(method).asType(type);
      }
    } else if (result instanceof Constructor) {
      Constructor constructor = (Constructor) result;
      types = constructor.getParameterTypes();
      if (constructor.isVarArgs() && isLastArgumentAnArray(types.length, args)) {
        handle = caller.unreflectConstructor(constructor).asFixedArity().asType(type);
      } else {
        handle = caller.unreflectConstructor(constructor).asType(type);
      }
    } else {
      Field field = (Field) result;
      handle = caller.unreflectGetter(field).asType(type);
    }
    handle = insertSAMFilter(handle, types, 0);

    callSite.setTarget(handle);
    return handle.invokeWithArguments(args);
View Full Code Here

    }
  }

  @Test
  public void check_bootstrapFunctionInvocation_on_local_static_method() throws Throwable {
    Lookup lookup = lookup();
    MethodType type = MethodType.methodType(Object.class, Object.class);
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, "echo", type);
    assertThat((String) callSite.dynamicInvoker().invokeWithArguments("Hey!"), is("Hey!"));
  }
View Full Code Here

    assertThat((String) callSite.dynamicInvoker().invokeWithArguments("Hey!"), is("Hey!"));
  }

  @Test
  public void check_bootstrapFunctionInvocation_on_class_static_method() throws Throwable {
    Lookup lookup = lookup();
    MethodType type = MethodType.methodType(Object.class);
    String name = "fr#insalyon#citi#golo#runtime#FunctionCallSupportTest$Foo#someInt";
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, name, type);
    assertThat((Integer) callSite.dynamicInvoker().invokeWithArguments(), is(42));
  }
View Full Code Here

    assertThat((Integer) callSite.dynamicInvoker().invokeWithArguments(), is(42));
  }

  @Test
  public void check_bootstrapFunctionInvocation_on_static_field() throws Throwable {
    Lookup lookup = lookup();
    MethodType type = MethodType.methodType(Object.class);
    String name = "fr#insalyon#citi#golo#runtime#FunctionCallSupportTest$Foo#FOO";
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, name, type);
    assertThat((String) callSite.dynamicInvoker().invokeWithArguments(), is("Foo"));
  }
View Full Code Here

  }


  @Test(expectedExceptions = NoSuchMethodError.class)
  public void check_bootstrapFunctionInvocation_on_unexisting_method() throws Throwable {
    Lookup lookup = lookup();
    MethodType type = MethodType.methodType(Object.class, Object.class);
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, "echoz", type);
    callSite.dynamicInvoker().invokeWithArguments("foo");
  }
View Full Code Here

    callSite.dynamicInvoker().invokeWithArguments("foo");
  }

  @Test(expectedExceptions = NoSuchMethodError.class)
  public void check_bootstrapFunctionInvocation_on_method_with_wrong_number_of_parameters() throws Throwable {
    Lookup lookup = lookup();
    MethodType type = MethodType.methodType(Object.class, Object.class, Object.class);
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, "echo", type);
    callSite.dynamicInvoker().invokeWithArguments("foo", "foo");
  }
View Full Code Here

    callSite.dynamicInvoker().invokeWithArguments("foo", "foo");
  }

  @Test
  public void check_varargs() throws Throwable {
    Lookup lookup = lookup();
    String name = "fr#insalyon#citi#golo#runtime#FunctionCallSupportTest$Foo#concat";
    MethodType type = MethodType.methodType(Object.class, Object.class, Object.class, Object.class, Object.class);
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, name, type);
    assertThat((String) callSite.dynamicInvoker().invokeWithArguments("-", "a", "b", "c"), is("a-b-c"));
View Full Code Here

    assertThat((String) callSite.dynamicInvoker().invokeWithArguments("-", new String[]{"a", "b", "c"}), is("a-b-c"));
  }

  @Test
  public void check_varargs_only() throws Throwable {
    Lookup lookup = lookup();
    String name = "fr#insalyon#citi#golo#runtime#FunctionCallSupportTest$Foo#defaultConcat";

    MethodType type = MethodType.methodType(Object.class, Object.class, Object.class, Object.class);
    CallSite callSite = FunctionCallSupport.bootstrap(lookup, name, type);
    assertThat((String) callSite.dynamicInvoker().invokeWithArguments("a", "b", "c"), is("a-b-c"));
View Full Code Here

        CodeRef[] refs = new CodeRef[12];
        String[] snull = null;
        long[][] hnull = new long[0][];
        MethodType mt = MethodType.methodType(void.class, ThreadContext.class,
                CodeRef.class, CallSiteDescriptor.class, Object[].class);
        Lookup l = MethodHandles.lookup();
        try {
            refs[0] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "new_type", mt).bindTo(this),
                    "new_type", "new_type", snull, snull, snull, snull, hnull, (short)0);
            refs[1] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "add_method", mt).bindTo(this),
                    "add_method", "add_method", snull, snull, snull, snull, hnull, (short)0);
            refs[2] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "add_attribute", mt).bindTo(this),
                    "add_attribute", "add_attribute", snull, snull, snull, snull, hnull, (short)0);
            refs[3] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "compose", mt).bindTo(this),
                    "compose", "compose", snull, snull, snull, snull, hnull, (short)0);
            refs[4] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "attributes", mt).bindTo(this),
                    "attributes", "attributes", snull, snull, snull, snull, hnull, (short)0);
            refs[5] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "methods", mt).bindTo(this),
                    "methods", "methods", snull, snull, snull, snull, hnull, (short)0);
            refs[6] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "name", mt).bindTo(this),
                    "name", "name", snull, snull, snull, snull, hnull, (short)0);
            refs[7] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "attr_new", mt).bindTo(this),
                    "new", "attr_new", snull, snull, snull, snull, hnull, (short)0);
            refs[8] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "attr_compose", mt).bindTo(this),
                    "compose", "attr_compose", snull, snull, snull, snull, hnull, (short)0);
            refs[9] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "attr_name", mt).bindTo(this),
                    "name", "attr_name", snull, snull, snull, snull, hnull, (short)0);
            refs[10] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "attr_type", mt).bindTo(this),
                    "type", "attr_type", snull, snull, snull, snull, hnull, (short)0);
            refs[11] = new CodeRef(this, l.findVirtual(KnowHOWMethods.class, "attr_box_target", mt).bindTo(this),
                    "box_target", "attr_box_target", snull, snull, snull, snull, hnull, (short)0);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
View Full Code Here

TOP

Related Classes of java.lang.invoke.MethodHandles.Lookup

Copyright © 2018 www.massapicom. 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.