下面列出了java.lang.invoke.MethodHandles#identity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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()}
};
}
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, Metadata metadata)
{
Type type = boundVariables.getTypeVariable("T");
MethodHandle identity = MethodHandles.identity(type.getJavaType());
return new ScalarFunctionImplementation(
false,
ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
identity);
}
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, Metadata metadata)
{
Type parameterType = boundVariables.getTypeVariable("F");
Type type = boundVariables.getTypeVariable("T");
MethodHandle methodHandle = null;
if (parameterType.getJavaType() == type.getJavaType()) {
methodHandle = MethodHandles.identity(parameterType.getJavaType());
}
if (parameterType.getJavaType() == Slice.class) {
if (type.getJavaType() == Block.class) {
methodHandle = READ_BLOCK.bindTo(metadata.getBlockEncodingSerde());
}
else if (type.getJavaType() != Slice.class) {
methodHandle = READ_BLOCK_VALUE.bindTo(metadata.getBlockEncodingSerde()).bindTo(type);
}
}
checkArgument(methodHandle != null,
"Expected type %s to use (or can be converted into) Java type %s, but Java type is %s",
type,
parameterType.getJavaType(),
type.getJavaType());
return new ScalarFunctionImplementation(
false,
ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
methodHandle);
}
private void handleNullWithoutBoolean() {
if (handle != null || args[0] != null) return;
if (staticTargetType.isPrimitive()) {
handle = MethodHandles.insertArguments(GROOVY_CAST_EXCEPTION, 1, staticTargetType);
// need to call here here because we used the static target type
// it won't be done otherwise because handle.type() == callSite.type()
castAndSetGuards();
} else {
handle = MethodHandles.identity(staticSourceType);
}
}
private void handleInstanceCase() {
if (handle != null) return;
if (staticTargetType.isAssignableFrom(args[0].getClass())) {
handle = MethodHandles.identity(staticSourceType);
}
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
public static MethodHandle identity(Class<?> type) {
if (!type.isPrimitive())
// Reference identity has been moved into MethodHandles:
return MethodHandles.identity(type);
return identity(Wrapper.findPrimitiveType(type));
}
public static MethodHandle identity(Class<?> type) {
if (!type.isPrimitive())
// Reference identity has been moved into MethodHandles:
return MethodHandles.identity(type);
return identity(Wrapper.findPrimitiveType(type));
}
@Override
public MethodHandle identity(final Class<?> type) {
final MethodHandle mh = MethodHandles.identity(type);
return debug(mh, "identity", type);
}
private static MethodHandle castChecker(Class<?> castTo) {
MethodHandle handle = MethodHandles.identity(castTo);
return handle.asType(methodType(Void.TYPE, Object.class));
}
private static MethodHandle toObjectArray(List<Class<?>> types) {
MethodHandle mh = MethodHandles.identity(Object[].class);
mh = mh.asCollector(Object[].class, types.size());
return mh.asType(MethodType.methodType(Object[].class, types));
}