本文整理汇总了Java中java.lang.invoke.MethodHandles.filterReturnValue方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.filterReturnValue方法的具体用法?Java MethodHandles.filterReturnValue怎么用?Java MethodHandles.filterReturnValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.filterReturnValue方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: dynamicCast
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/** Looks up generic method, with a dynamic cast to the specified type. (explicit assignment) */
public static MethodHandle dynamicCast(MethodHandle target, Class<?> desired) {
// adapt dynamic cast to the generic method
desired = MethodType.methodType(desired).wrap().returnType();
// bind to the boxed type
MethodHandle cast = DYNAMIC_CAST.bindTo(desired);
return MethodHandles.filterReturnValue(target, cast);
}
示例2: TestCase
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
private TestCase(Class<T> rtype, Function<Object, T> cast,
ThrowMode throwMode, Throwable thrown)
throws NoSuchMethodException, IllegalAccessException {
this.cast = cast;
filter = MethodHandles.lookup().findVirtual(
Function.class,
"apply",
MethodType.methodType(Object.class, Object.class))
.bindTo(cast);
this.rtype = rtype;
this.throwMode = throwMode;
this.throwableClass = thrown.getClass();
switch (throwMode) {
case NOTHING:
this.thrown = null;
break;
case ADAPTER:
case UNCAUGHT:
this.thrown = new Error("do not catch this");
break;
default:
this.thrown = thrown;
}
MethodHandle throwOrReturn = THROW_OR_RETURN;
if (throwMode == ThrowMode.ADAPTER) {
MethodHandle fakeIdentity = FAKE_IDENTITY.bindTo(this);
for (int i = 0; i < 10; ++i) {
throwOrReturn = MethodHandles.filterReturnValue(
throwOrReturn, fakeIdentity);
}
}
thrower = throwOrReturn.asType(MethodType.genericMethodType(2));
}
示例3: testFilterReturnValue
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testFilterReturnValue(int nargs, Class<?> rtype) throws Throwable {
countTest();
MethodHandle target = varargsList(nargs, rtype);
MethodHandle filter;
if (List.class.isAssignableFrom(rtype) || rtype.isAssignableFrom(List.class))
filter = varargsList(1); // add another layer of list-ness
else
filter = MethodHandles.identity(rtype);
filter = filter.asType(MethodType.methodType(target.type().returnType(), rtype));
Object[] argsToPass = randomArgs(nargs, Object.class);
if (verbosity >= 3)
System.out.println("filter "+target+" to "+rtype.getSimpleName()+" with "+filter);
MethodHandle target2 = MethodHandles.filterReturnValue(target, filter);
if (verbosity >= 4)
System.out.println("filtered target: "+target2);
// Simulate expected effect of filter on return value:
Object unfiltered = target.invokeWithArguments(argsToPass);
Object expected = filter.invokeWithArguments(unfiltered);
if (verbosity >= 4)
System.out.println("unfiltered: "+unfiltered+" : "+unfiltered.getClass().getSimpleName());
if (verbosity >= 4)
System.out.println("expected: "+expected+" : "+expected.getClass().getSimpleName());
Object result = target2.invokeWithArguments(argsToPass);
if (verbosity >= 3)
System.out.println("result: "+result+" : "+result.getClass().getSimpleName());
if (!expected.equals(result))
System.out.println("*** fail at n/rt = "+nargs+"/"+rtype.getSimpleName()+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
assertEquals(expected, result);
}
示例4: varargsArray
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
Class<?> elemType = arrayType.getComponentType();
MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
MethodHandle mh = varargsArray(nargs);
if (arrayType != Object[].class)
mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType));
return mh.asType(vaType);
}
示例5: filterReturnValue
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle filterReturnValue(final MethodHandle target, final MethodHandle filter) {
final MethodHandle mh = MethodHandles.filterReturnValue(target, filter);
return debug(mh, "filterReturnValue", target, filter);
}
示例6: filter
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public MethodHandle filter(MethodHandle target) {
return MethodHandles.filterReturnValue(target, filter);
}
示例7: getCatcher
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public MethodHandle getCatcher(List<Class<?>> classes) {
return MethodHandles.filterReturnValue(Helper.AS_LIST.asType(
MethodType.methodType(Object.class, classes)),
CATCHER
);
}
示例8: getCatcher
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public MethodHandle getCatcher(List<Class<?>> classes) {
return MethodHandles.filterReturnValue(Helper.AS_LIST.asType(
MethodType.methodType(Object.class, classes)),
CATCHER
);
}