本文整理汇总了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);
*/
}
示例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;
};
}
示例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;
}
示例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
}
示例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);
}
}
示例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);
}
示例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);
}
示例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;
};
}