本文整理汇总了Java中java.lang.invoke.LambdaMetafactory.metafactory方法的典型用法代码示例。如果您正苦于以下问题:Java LambdaMetafactory.metafactory方法的具体用法?Java LambdaMetafactory.metafactory怎么用?Java LambdaMetafactory.metafactory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.LambdaMetafactory
的用法示例。
在下文中一共展示了LambdaMetafactory.metafactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
l = MethodHandles.lookup();
h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
MethodType X = mt(int.class, LambdaReceiver.class);
MethodType A = mt(int.class, LambdaReceiver_A.class);
MethodType mti = mt(IA.class);
CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
IA p = (IA)cs.dynamicInvoker().invoke();
LambdaReceiver_A lra = new LambdaReceiver_A();
try {
p.m(lra);
} catch (ClassCastException cce) {
return;
}
throw new AssertionError("CCE expected");
}
示例2: accessInstanceMethodWithFunction
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
@Test
public void accessInstanceMethodWithFunction() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType getter = MethodType.methodType(Object.class);
MethodHandle target = caller.findVirtual(SimpleBean.class, "getObj", getter);
MethodType func = target.type();
System.out.println(func);
System.out.println(func.generic());
CallSite site = LambdaMetafactory.metafactory(caller, "apply", MethodType.methodType(Function.class), func.generic(), target, func);
MethodHandle factory = site.getTarget();
Function r = (Function) factory.invoke();
assertEquals("myCustomObject", r.apply(simpleBeanInstance));
}
示例3: accessObjectInstanceMethod
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
@Test
public void accessObjectInstanceMethod() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getObj");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, "get", MethodType.methodType(Supplier.class, SimpleBean.class),
MethodType.methodType(Object.class), methodHandle, MethodType.methodType(Object.class));
MethodHandle factory = site.getTarget();
factory = factory.bindTo(simpleBeanInstance);
Supplier r = (Supplier) factory.invoke();
assertEquals(simpleBeanInstance.getObj(), r.get());
}
示例4: mf
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private static boolean mf(MethodHandles.Lookup l) {
try {
LambdaMetafactory.metafactory(l, "close",
mt(Closeable.class),mt(void.class),h,mt(void.class));
} catch(LambdaConversionException e) {
return true;
}
return false;
}
示例5: mf
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private static boolean mf(Class<?> k) {
try {
LambdaMetafactory.metafactory(l, "m",
mt(I.class),mt(k),h,mt(void.class));
} catch(LambdaConversionException e) {
return true;
}
return false;
}
示例6: mf
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private static boolean mf(MethodType mti, MethodType mtf) {
try {
LambdaMetafactory.metafactory(l, "m", mti,mtf,h,mtf);
} catch(LambdaConversionException e) {
return true;
}
return false;
}
示例7: testDirectStdNonser
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
public void testDirectStdNonser() throws Throwable {
MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);
// Standard metafactory, non-serializable target: not serializable
CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
"test", MethodType.methodType(Predicate.class),
predicateMT, fooMH, stringPredicateMT);
Predicate<String> p = (Predicate<String>) cs.getTarget().invokeExact();
assertNotSerial(p, fooAsserter);
}
示例8: testDirectStdSer
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
public void testDirectStdSer() throws Throwable {
MethodHandle fooMH = MethodHandles.lookup().findStatic(SerializedLambdaTest.class, "foo", predicateMT);
// Standard metafactory, serializable target: not serializable
CallSite cs = LambdaMetafactory.metafactory(MethodHandles.lookup(),
"test", MethodType.methodType(SerPredicate.class),
predicateMT, fooMH, stringPredicateMT);
assertNotSerial((SerPredicate<String>) cs.getTarget().invokeExact(), fooAsserter);
}
示例9: createCallSite
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private static CallSite createCallSite(String signatureName, MethodHandles.Lookup lookup, MethodHandle methodHandle,
MethodType instantiatedMethodType, MethodType signature, Class<?> interfaceClass) throws LambdaConversionException {
return LambdaMetafactory.metafactory(
lookup,
signatureName,
MethodType.methodType(interfaceClass),
signature,
methodHandle,
instantiatedMethodType);
}
示例10: makeDeserializationFunc
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private ParseFromBytes makeDeserializationFunc(Class<?> type, ParseFromBytes func) {
try {
MethodType originFuncArgs = MethodType.methodType(type, byte[].class);
MethodType targetFuncArgs = MethodType.methodType(Object.class, byte[].class);
MethodType targetClass = MethodType.methodType(ParseFromBytes.class);
MethodHandle originFuncHandle = caller.findStatic(type, "parseFrom", originFuncArgs);
CallSite callSite = LambdaMetafactory.metafactory(caller, "parseFormData", targetClass, targetFuncArgs,
originFuncHandle, originFuncArgs);
MethodHandle factory = callSite.getTarget();
func = (ParseFromBytes) factory.invoke();
} catch (Throwable e) {
log.error("makeDeserializationFunc error ..", e);
}
return func;
}
示例11: getConstructorAs
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
protected <T, R> T getConstructorAs(Class<T> targetClass, String methodName, Class<? extends R> clazz, Class<?>... args) {
try {
MethodType methodType = methodType(clazz, args);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle handle = lookup.findConstructor(clazz, methodType(void.class, args));
MethodType targetType = methodType(targetClass);
CallSite callSite = LambdaMetafactory.metafactory(lookup, methodName, targetType, methodType.generic(), handle, methodType);
return (T) callSite.getTarget().invoke();
} catch (Throwable t) {
throw new RuntimeException("Unable to create function for constructor of " + clazz.getName(), t);
}
}
示例12: buildFunction
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private void buildFunction(final MethodHandles.Lookup caller, final MethodHandle methodHandle) {
try {
final MethodType func = methodHandle.type();
final CallSite site = LambdaMetafactory.metafactory(caller, "apply", MethodType.methodType(Function.class),
MethodType.methodType(Object.class, Object.class), methodHandle, MethodType.methodType(func.returnType(), func.parameterArray()));
final MethodHandle factory = site.getTarget();
function = (Function<BEAN, R>) factory.invoke();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
示例13: build
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private void build(final MethodHandles.Lookup caller, final MethodHandle methodHandle) {
try {
final MethodType func = methodHandle.type();
final CallSite site = LambdaMetafactory.metafactory(caller, "apply", MethodType.methodType(BiFunction.class),
MethodType.methodType(Object.class, Object.class, Object.class), methodHandle, MethodType.methodType(func.returnType(), func.parameterArray()));
final MethodHandle factory = site.getTarget();
function = (BiFunction<BEAN, R, BEAN>) factory.invoke();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
示例14: build
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
private void build(final MethodHandles.Lookup caller, final MethodHandle methodHandle) {
try {
final MethodType func = methodHandle.type();
final CallSite site = LambdaMetafactory.metafactory(caller, "accept", MethodType.methodType(BiConsumer.class),
MethodType.methodType(Void.TYPE, Object.class, Object.class), methodHandle, MethodType.methodType(Void.TYPE, func.parameterArray()));
final MethodHandle factory = site.getTarget();
consumer = (BiConsumer<BEAN, R>) factory.invoke();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
示例15: accessStaticMethod
import java.lang.invoke.LambdaMetafactory; //导入方法依赖的package包/类
@Test
public void accessStaticMethod() throws Throwable {
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getStaticObj");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, "get", MethodType.methodType(Supplier.class), MethodType.methodType(Object.class), methodHandle,
MethodType.methodType(Object.class));
MethodHandle factory = site.getTarget();
Supplier r = (Supplier) factory.invoke();
assertEquals(SimpleBean.getStaticObj(), r.get());
}