Package java.lang.invoke

Examples of java.lang.invoke.MethodHandle


    Object[] astConstants = jvmClass.astConstants();
    if (astConstants.length == 0)
      return this;
    MethodType type = MethodType.genericMethodType(astConstants.length)
        .changeReturnType(void.class);
    MethodHandle findStatic;
    try {
      findStatic = MethodHandles.lookup().findStatic(current.javaClass,
          "_astinit", type);
      findStatic.invokeWithArguments(astConstants);
    } catch (Throwable e) {
      throw new RuntimeException(e);
    }
    return this;
  }
View Full Code Here


      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

     * @throws NoSuchMethodException
     *             thrown when an accessor cannot be found for the field
     */
    protected static MethodHandle buildAccessorWithLikelyPrefixes(
        final Object target, final String fieldName) {
      final MethodHandle mh = buildAccessor(target, fieldName, "get",
          "is", "has", "use");
      if (mh == null) {
        // throw new NoSuchMethodException(fieldName + " on " + target);
        throw new IllegalArgumentException(fieldName + " on " + target);
      }
View Full Code Here

     * @return the setter {@link MethodHandle}
     */
    protected static MethodHandle buildSetter(final MethodHandle accessor,
        final Object target, final String fieldName) {
      try {
        final MethodHandle mh1 = MethodHandles
            .lookup()
            .findVirtual(
                target.getClass(),
                buildMethodName("set", fieldName),
                MethodType.methodType(void.class, accessor
View Full Code Here

      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

    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

  }

  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

    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

  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

                    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

TOP

Related Classes of java.lang.invoke.MethodHandle

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.