Examples of MethodHandle


Examples of java.lang.invoke.MethodHandle

    if (value != null && String.class.isAssignableFrom(valueOfClass)) {
      return (VT) value.toString();
    }
    final Class<?> clazz = PRIMS.containsKey(valueOfClass) ? PRIMS
        .get(valueOfClass) : valueOfClass;
    MethodHandle mh1 = null;
    try {
      mh1 = MethodHandles.lookup().findStatic(clazz, "valueOf",
          MethodType.methodType(clazz, String.class));
    } catch (final Throwable t) {
      // class doesn't support it- do nothing
    }
    if (mh1 != null) {
      try {
        return (VT) mh1.invoke(value);
      } catch (final Throwable t) {
        throw new IllegalArgumentException(String.format(
            "Unable to invoke valueOf on %1$s using %2$s", value,
            valueOfClass), t);
      }
View Full Code Here

Examples of java.lang.invoke.MethodHandle

  }

  public static void timeUnReflectionCached(IndyDemo indyDemo) throws Throwable {
    long start = System.currentTimeMillis();
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle someMethod = lookup.unreflect(IndyDemo.class.getDeclaredMethod("someMethod"));
    for (int i = 0; i < TIMES; i++) {
      int result = (int) someMethod.invokeExact(indyDemo);
    }
    System.out.println("unreflection cached: " + (System.currentTimeMillis() - start));
  }
View Full Code Here

Examples of java.lang.invoke.MethodHandle

    long start = System.currentTimeMillis();
    int REFLECTION_TIMES = 10000;
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType type = MethodType.methodType(Integer.TYPE);
    for (int i = 0; i < REFLECTION_TIMES; i++) {
      MethodHandle someMethod = lookup.findVirtual(IndyDemo.class, "someMethod", type);
      int result = (int) someMethod.invokeExact(indyDemo);
    }
    System.out.println("methodhandle: " + (TIMES/ REFLECTION_TIMES)*(System.currentTimeMillis() - start));
  }
View Full Code Here

Examples of java.lang.invoke.MethodHandle

  public static void timeMHCached(IndyDemo indyDemo) throws Throwable {
    long start = System.currentTimeMillis();
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType type = MethodType.methodType(Integer.TYPE);
    MethodHandle someMethod = lookup.findVirtual(IndyDemo.class, "someMethod", type);
    for (int i = 0; i < TIMES; i++) {
      int result = (int) someMethod.invokeExact(indyDemo);
    }
    System.out.println("methodhandle cached: " + (System.currentTimeMillis() - start));
  }
View Full Code Here

Examples of java.lang.invoke.MethodHandle

                    ScalarFunction scalarFunction = method.getAnnotation(ScalarFunction.class);
                    if (scalarFunction == null) {
                        continue;
                    }
                    checkValidMethod(method);
                    MethodHandle methodHandle = lookup().unreflect(method);
                    String name = scalarFunction.value();
                    if (name.isEmpty()) {
                        name = camelToSnake(method.getName());
                    }
                    String description = getDescription(method);
View Full Code Here

Examples of java.lang.invoke.MethodHandle

            FunctionInfo function = metadata.getFunction(node.getName(), argumentTypes, false);
            // do not optimize non-deterministic functions
            if (optimize && !function.isDeterministic()) {
                return new FunctionCall(node.getName(), node.getWindow().orNull(), node.isDistinct(), toExpressions(argumentValues));
            }
            MethodHandle handle = function.getScalarFunction();
            if (handle.type().parameterCount() > 0 && handle.type().parameterType(0) == Session.class) {
                handle = handle.bindTo(session);
            }
            try {
                return handle.invokeWithArguments(argumentValues);
            }
            catch (Throwable throwable) {
                Throwables.propagateIfInstanceOf(throwable, RuntimeException.class);
                Throwables.propagateIfInstanceOf(throwable, Error.class);
                throw new RuntimeException(throwable.getMessage(), throwable);
View Full Code Here

Examples of java.lang.invoke.MethodHandle

   * multiple attributes over separate classes.
   * <p>Please save instances created by this method in a static final field, because
   * on each call, this does reflection for creating a {@link MethodHandle}.
   */
  public static <A extends AttributeImpl> AttributeFactory getStaticImplementation(AttributeFactory delegate, Class<A> clazz) {
    final MethodHandle constr = findAttributeImplCtor(clazz);
    return new StaticImplementationAttributeFactory<A>(delegate, clazz) {
      @Override
      protected A createInstance() {
        try {
          return (A) constr.invokeExact();
        } catch (Throwable t) {
          rethrow(t);
          throw new AssertionError();
        }
      }
View Full Code Here

Examples of java.lang.invoke.MethodHandle

      // Please note: we have the slight chance that another thread may do the same, but who cares?
      assert cached == null;
      final Class<? extends AttributeImpl> implClazz = findImplClass(attClass);
      // if the attribute impl is from our own ClassLoader, we optimize to use pre-allocated MethodHandle to instantiate the object
      if (useMethodHandles && implClazz.getClassLoader() == myClassLoader) {
        final MethodHandle constr = findAttributeImplCtor(implClazz);
        attClassImplMap.put(attClass, constr);
        return invokeMethodHandle(constr);
      } else {
        // otherwise, to not refer to the class forever (because the MethodHandle strongly
        // references the class), so it can never be unloaded, we use slower reflection:
View Full Code Here

Examples of juzu.impl.common.MethodHandle

    this.type = type;
    this.method = method;
    this.parameterList = Tools.safeUnmodifiableList(parameterList);
    this.parameterMap = Collections.unmodifiableMap(argumentMap);
    this.requiresPrefix = requiresPrefix;
    this.handle = new MethodHandle(method);
  }
View Full Code Here

Examples of org.apache.tapestry5.plastic.MethodHandle

            return plasticMethod.getDescription().methodName;
        }

        public MethodAccess getAccess()
        {
            final MethodHandle handle = plasticMethod.getHandle();

            return new WrapMethodHandleAsMethodAccess(handle);
        }
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.