當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodHandle.bindTo方法代碼示例

本文整理匯總了Java中java.lang.invoke.MethodHandle.bindTo方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandle.bindTo方法的具體用法?Java MethodHandle.bindTo怎麽用?Java MethodHandle.bindTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.invoke.MethodHandle的用法示例。


在下文中一共展示了MethodHandle.bindTo方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
public static void main(String[] args) throws Throwable {
  MethodHandles.Lookup lookup = MethodHandles.lookup();
  // mt is (char,char)String
  MethodType mt = MethodType.methodType(void.class, Object.class);
  MethodHandle mh = lookup.findVirtual(MethodHandleTest.class, "print", mt);
  mh = mh.bindTo(new MethodHandleTest());

  mh.invoke("Hello World");

  /*
   * Consumer cs = new PartTest_test_FuncIfImpl_0(mh);
   *
   * ArrayList list = new ArrayList(); list.add("Hello1"); list.add("Hello2");
   *
   * list.stream().forEach(cs);
   */

}
 
開發者ID:Samsung,項目名稱:MeziLang,代碼行數:19,代碼來源:MethodHandleTest.java

示例2: createGenericReflectionSupplierNoArgs

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
/**
 * Generic method which converts a method signature without arguments to a Supplier instance
 * Uses MethodHandles instead of Reflection API
 *
 * @param obj
 * @param methodName
 * @param type
 * @param <T>
 * @return
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws IllegalAccessException
 */
public static <T> Supplier<T> createGenericReflectionSupplierNoArgs(Object obj, String methodName, Class<? extends Object> type) throws NoSuchMethodException, SecurityException, IllegalAccessException {
	MethodHandle methodHandle = MethodHandles.lookup().findVirtual(obj.getClass(), methodName, MethodType.methodType(type));
	MethodHandle ready = methodHandle.bindTo(obj);
	return () -> {
		T t = null;
		try {
			return (T)type.cast(ready.invoke());
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return t;
	};
}
 
開發者ID:striderarun,項目名稱:parallel-execution-engine,代碼行數:27,代碼來源:SupplierFactory.java

示例3: InjectableMethod

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
private <D> InjectableMethod(@Nullable TypeLiteral<D> targetType, @Nullable D target, Method method, @Nullable T result) {
    final Errors errors = new Errors(method);

    if(Members.isStatic(method)) {
        checkArgument(target == null);
    } else {
        checkArgument(target != null);
    }

    targetType = targetType(targetType, target, method);

    checkArgument(method.getDeclaringClass().isAssignableFrom(targetType.getRawType()));

    this.method = method;
    this.dependencies = ImmutableSet.copyOf(InjectionPoint.forMethod(method, targetType).getDependencies());

    if(result != null) {
        this.result = result;
        this.providedKey = Keys.forInstance(result);
    } else {
        final TypeLiteral<T> returnType = (TypeLiteral<T>) targetType.getReturnType(method);
        if(!Void.class.equals(returnType.getRawType())) {
            final Annotation qualifier = Annotations.findBindingAnnotation(errors, method, method.getAnnotations());
            this.result = null;
            this.providedKey = Keys.get(returnType, qualifier);
        } else {
            this.result = (T) this;
            this.providedKey = Keys.forInstance(this.result);
        }
    }

    this.scope = Annotations.findScopeAnnotation(errors, method.getAnnotations());

    MethodHandle handle = MethodHandleUtils.privateUnreflect(method);
    if(target != null) {
        handle = handle.bindTo(target);
    }
    this.handle = handle;
}
 
開發者ID:OvercastNetwork,項目名稱:ProjectAres,代碼行數:40,代碼來源:InjectableMethod.java

示例4: testExtendCustomizedBMH

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
static void testExtendCustomizedBMH() throws Exception {
    // Construct BMH
    MethodHandle mh = MethodHandleHelper.IMPL_LOOKUP.findVirtual(String.class, "concat",
            MethodType.methodType(String.class, String.class))
            .bindTo("a");
    MethodHandleHelper.customize(mh);
    mh.bindTo("b"); // Try to extend customized BMH
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:CustomizedLambdaFormTest.java

示例5: testVarargsCollector0

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
public void testVarargsCollector0() throws Throwable {
    if (CAN_SKIP_WORKING)  return;
    startTest("varargsCollector");
    MethodHandle vac0 = PRIVATE.findStatic(MethodHandlesTest.class, "called",
                           MethodType.methodType(Object.class, String.class, Object[].class));
    vac0 = vac0.bindTo("vac");
    MethodHandle vac = vac0.asVarargsCollector(Object[].class);
    testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
    testConvert(true, vac.asType(MethodType.genericMethodType(0)), null, "vac");
    for (Class<?> at : new Class<?>[] { Object.class, String.class, Integer.class }) {
        testConvert(true, vac.asType(MethodType.genericMethodType(1)), null, "vac", at);
        testConvert(true, vac.asType(MethodType.genericMethodType(2)), null, "vac", at, at);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:MethodHandlesTest.java

示例6: bind

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
private static MethodHandle bind(VarHandle vh, MethodHandle mh, MethodType emt) {
    assertEquals(mh.type(), emt.insertParameterTypes(0, VarHandle.class),
                 "MethodHandle type differs from access mode type");

    MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
    assertEquals(info.getMethodType(), emt,
                 "MethodHandleInfo method type differs from access mode type");

    return mh.bindTo(vh);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:VarHandleBaseTest.java

示例7: bindTo

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
@Override
public MethodHandle bindTo(final MethodHandle handle, final Object x) {
    final MethodHandle mh = handle.bindTo(x);
    return debug(mh, "bindTo", handle, x);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:6,代碼來源:MethodHandleFactory.java

示例8: createGenericReflectionSupplierWithArgs

import java.lang.invoke.MethodHandle; //導入方法依賴的package包/類
/**
 * Generic method which converts a method signature with arguments to a Supplier instance
 * Uses MethodHandles instead of Reflection API
 *
 * @param obj
 * @param methodName
 * @param type
 * @param args
 * @param argTypes
 * @param <T>
 * @param <E>
 * @return
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws IllegalAccessException
 */
public static <T,E> Supplier<T> createGenericReflectionSupplierWithArgs(Object obj, String methodName, Class<? extends Object> type, List<? extends Object> args, Class<E>... argTypes) throws NoSuchMethodException, SecurityException, IllegalAccessException {
	MethodHandle methodHandle = MethodHandles.lookup().findVirtual(obj.getClass(), methodName, MethodType.methodType(type, argTypes));
	MethodHandle ready = methodHandle.bindTo(obj);
	return () -> {
		T t = null;
		try {
			return (T)type.cast(ready.invokeWithArguments(args));
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return t;
	};
}
 
開發者ID:striderarun,項目名稱:parallel-execution-engine,代碼行數:30,代碼來源:SupplierFactory.java


注:本文中的java.lang.invoke.MethodHandle.bindTo方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。