本文整理匯總了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;
};
}