本文整理匯總了Java中java.lang.invoke.MethodHandles.identity方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.identity方法的具體用法?Java MethodHandles.identity怎麽用?Java MethodHandles.identity使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.identity方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testInterfaceCast0
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
public void testInterfaceCast0() throws Throwable {
if (CAN_SKIP_WORKING) return;
startTest("interfaceCast");
assert( (((Object)"foo") instanceof CharSequence));
assert(!(((Object)"foo") instanceof Iterable));
for (MethodHandle mh : new MethodHandle[]{
MethodHandles.identity(String.class),
MethodHandles.identity(CharSequence.class),
MethodHandles.identity(Iterable.class)
}) {
if (verbosity > 0) System.out.println("-- mh = "+mh);
for (Class<?> ctype : new Class<?>[]{
Object.class, String.class, CharSequence.class,
Number.class, Iterable.class
}) {
if (verbosity > 0) System.out.println("---- ctype = "+ctype.getName());
// doret docast
testInterfaceCast(mh, ctype, false, false);
testInterfaceCast(mh, ctype, true, false);
testInterfaceCast(mh, ctype, false, true);
testInterfaceCast(mh, ctype, true, true);
}
}
}
示例2: negativeTestData
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@DataProvider
static Object[][] negativeTestData() {
MethodHandle intid = MethodHandles.identity(int.class);
MethodHandle intco = MethodHandles.constant(int.class, 0);
MethodHandle errTarget = MethodHandles.dropArguments(intco, 0, int.class, double.class, String.class, int.class);
MethodHandle errCleanup = MethodHandles.dropArguments(MethodHandles.constant(int.class, 0), 0, Throwable.class,
int.class, double.class, Object.class);
MethodHandle voidTarget = TryFinally.MH_voidTarget;
MethodHandle voidICleanup = MethodHandles.dropArguments(TryFinally.MH_voidCleanup, 1, int.class);
return new Object[][]{
{intid, MethodHandles.identity(double.class),
"target and return types must match: double != int"},
{intid, MethodHandles.dropArguments(intid, 0, String.class),
"cleanup first argument and Throwable must match: (String,int)int != class java.lang.Throwable"},
{intid, MethodHandles.dropArguments(intid, 0, Throwable.class, double.class),
"cleanup second argument and target return type must match: (Throwable,double,int)int != int"},
{errTarget, errCleanup,
"cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
errCleanup.type() + " != " + errTarget.type()},
{voidTarget, voidICleanup,
"cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
voidICleanup.type() + " != " + voidTarget.type()}
};
}
示例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: identity
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
示例5: testTryFinally0
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
public void testTryFinally0() throws Throwable {
if (CAN_SKIP_WORKING) return;
startTest("tryFinally");
String inputMessage = "returned";
String augmentedMessage = "augmented";
String thrownMessage = "thrown";
String rethrownMessage = "rethrown";
// Test these cases:
// * target returns, cleanup passes through
// * target returns, cleanup augments
// * target throws, cleanup augments and returns
// * target throws, cleanup augments and rethrows
MethodHandle target = MethodHandles.identity(String.class);
MethodHandle targetThrow = MethodHandles.dropArguments(
MethodHandles.throwException(String.class, Exception.class).bindTo(new Exception(thrownMessage)), 0, String.class);
MethodHandle cleanupPassThrough = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0,
Throwable.class, String.class);
MethodHandle cleanupAugment = MethodHandles.dropArguments(MethodHandles.constant(String.class, augmentedMessage),
0, Throwable.class, String.class, String.class);
MethodHandle cleanupCatch = MethodHandles.dropArguments(MethodHandles.constant(String.class, thrownMessage), 0,
Throwable.class, String.class, String.class);
MethodHandle cleanupThrow = MethodHandles.dropArguments(MethodHandles.throwException(String.class, Exception.class).
bindTo(new Exception(rethrownMessage)), 0, Throwable.class, String.class, String.class);
testTryFinally(target, cleanupPassThrough, inputMessage, inputMessage, false);
testTryFinally(target, cleanupAugment, inputMessage, augmentedMessage, false);
testTryFinally(targetThrow, cleanupCatch, inputMessage, thrownMessage, true);
testTryFinally(targetThrow, cleanupThrow, inputMessage, rethrownMessage, true);
// Test the same cases as above for void targets and cleanups.
MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> C = this.getClass();
MethodType targetType = methodType(void.class, String[].class);
MethodType cleanupType = methodType(void.class, Throwable.class, String[].class);
MethodHandle vtarget = lookup.findStatic(C, "vtarget", targetType);
MethodHandle vtargetThrow = lookup.findStatic(C, "vtargetThrow", targetType);
MethodHandle vcleanupPassThrough = lookup.findStatic(C, "vcleanupPassThrough", cleanupType);
MethodHandle vcleanupAugment = lookup.findStatic(C, "vcleanupAugment", cleanupType);
MethodHandle vcleanupCatch = lookup.findStatic(C, "vcleanupCatch", cleanupType);
MethodHandle vcleanupThrow = lookup.findStatic(C, "vcleanupThrow", cleanupType);
testTryFinally(vtarget, vcleanupPassThrough, inputMessage, inputMessage, false);
testTryFinally(vtarget, vcleanupAugment, inputMessage, augmentedMessage, false);
testTryFinally(vtargetThrow, vcleanupCatch, inputMessage, thrownMessage, true);
testTryFinally(vtargetThrow, vcleanupThrow, inputMessage, rethrownMessage, true);
}