本文整理了Java中java.lang.invoke.MethodHandle.bindTo()
方法的一些代码示例,展示了MethodHandle.bindTo()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MethodHandle.bindTo()
方法的具体详情如下:
包路径:java.lang.invoke.MethodHandle
类名称:MethodHandle
方法名:bindTo
[英]Binds a value x to the first argument of a method handle, without invoking it. The new method handle adapts, as its target, the current method handle by binding it to the given argument. The type of the bound handle will be the same as the type of the target, except that a single leading reference parameter will be omitted.
When called, the bound handle inserts the given value xas a new leading argument to the target. The other arguments are also passed unchanged. What the target eventually returns is returned unchanged by the bound handle.
The reference x must be convertible to the first parameter type of the target.
(Note: Because method handles are immutable, the target method handle retains its original type and behavior.)
[中]将值x绑定到方法句柄的第一个参数,而不调用它。新方法句柄通过将当前方法句柄绑定到给定的参数来将其作为目标进行调整。绑定句柄的类型将与目标的类型相同,只是将省略一个前导引用参数。
调用时,绑定句柄将给定值xa作为新的前导参数插入目标。其他参数也会原封不动地传递。目标最终返回的内容由绑定句柄原封不动地返回。
参考x必须可转换为目标的第一个参数类型。
(*注意:*因为方法句柄是不可变的,所以目标方法句柄保留其原始类型和行为。)
代码示例来源:origin: square/retrofit
@Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object,
@Nullable Object... args) throws Throwable {
// Because the service interface might not be public, we need to use a MethodHandle lookup
// that ignores the visibility of the declaringClass.
Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class);
constructor.setAccessible(true);
return constructor.newInstance(declaringClass, -1 /* trusted */)
.unreflectSpecial(method, declaringClass)
.bindTo(object)
.invokeWithArguments(args);
}
代码示例来源:origin: apache/kafka
private static MethodHandle unmapJava9(MethodHandles.Lookup lookup) throws ReflectiveOperationException {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
MethodHandle unmapper = lookup.findVirtual(unsafeClass, "invokeCleaner",
methodType(void.class, ByteBuffer.class));
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
Object theUnsafe = f.get(null);
return unmapper.bindTo(theUnsafe);
}
代码示例来源:origin: prestodb/presto
private MethodHandle handle(String name)
{
List<Method> methods = Arrays.stream(getClass().getMethods())
.filter(method -> method.getName().equals(name))
.collect(toList());
checkArgument(!methods.isEmpty(), "no matching methods: %s", name);
checkArgument(methods.size() == 1, "multiple matching methods: %s", methods);
return methodHandle(methods.get(0)).bindTo(this);
}
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
List<Type> types = this.typeParameters.stream().map(boundVariables::getTypeVariable).collect(toImmutableList());
List<ArgumentProperty> argumentProperties = nCopies(types.size(), valueTypeArgumentProperty(RETURN_NULL_ON_NULL));
List<Class<?>> javaArgumentTypes = nCopies(types.size(), Block.class);
MethodHandle methodHandle = METHOD_HANDLE.bindTo(types).asVarargsCollector(Block[].class).asType(methodType(Block.class, javaArgumentTypes));
return new ScalarFunctionImplementation(false, argumentProperties, methodHandle, isDeterministic());
}
代码示例来源:origin: prestodb/presto
public Procedure getProcedure()
{
return new Procedure(
"runtime",
"kill_query",
ImmutableList.<Argument>builder()
.add(new Argument("query_id", VARCHAR))
.add(new Argument("message", VARCHAR))
.build(),
KILL_QUERY.bindTo(this));
}
代码示例来源:origin: prestodb/presto
private static MethodHandle bindInstanceFactory(MethodHandle method, ScalarFunctionImplementation implementation)
{
if (!implementation.getInstanceFactory().isPresent()) {
return method;
}
try {
return method.bindTo(implementation.getInstanceFactory().get().invoke());
}
catch (Throwable throwable) {
throw propagate(throwable);
}
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type keyType = boundVariables.getTypeVariable("K");
Type valueType = boundVariables.getTypeVariable("V");
MethodHandle keyHashCodeFunction = functionRegistry.getScalarFunctionImplementation(internalOperator(HASH_CODE, BIGINT, ImmutableList.of(keyType))).getMethodHandle();
MethodHandle valueHashCodeFunction = functionRegistry.getScalarFunctionImplementation(internalOperator(HASH_CODE, BIGINT, ImmutableList.of(valueType))).getMethodHandle();
MethodHandle method = METHOD_HANDLE.bindTo(keyHashCodeFunction).bindTo(valueHashCodeFunction).bindTo(keyType).bindTo(valueType);
return new ScalarFunctionImplementation(
false,
ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
method,
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type type = boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
false,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE.bindTo(type).bindTo(getMethodHandles((RowType) type, functionRegistry, LESS_THAN)),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type type = boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
false,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE.bindTo(type).bindTo(getMethodHandles((RowType) type, functionRegistry, LESS_THAN)),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type type = boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
false,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE.bindTo(type).bindTo(getMethodHandles((RowType) type, functionRegistry, GREATER_THAN)),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type type = boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
false,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE.bindTo(type).bindTo(getMethodHandles((RowType) type, functionRegistry, GREATER_THAN)),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type elementType = boundVariables.getTypeVariable("E");
Type arrayType = typeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType.getTypeSignature())));
MethodHandle methodHandle = METHOD_HANDLE.bindTo(elementType).bindTo(arrayType);
return new ScalarFunctionImplementation(
false,
ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
methodHandle,
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
RowType type = (RowType) boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
true,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE
.bindTo(type)
.bindTo(RowEqualOperator.resolveFieldEqualOperators(type, functionRegistry)),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
RowType type = (RowType) boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
true,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE
.bindTo(type)
.bindTo(resolveFieldEqualOperators(type, functionRegistry)),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type type = boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
false,
ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE.bindTo(type),
isDeterministic());
}
代码示例来源:origin: prestodb/presto
public Procedure getDropPartitionProcedure()
{
return new Procedure(
"system",
"drop_range_partition",
ImmutableList.of(new Argument("schema", VARCHAR), new Argument("table", VARCHAR),
new Argument("range_bounds", VARCHAR)),
DROP.bindTo(this));
}
代码示例来源:origin: prestodb/presto
public Procedure getAddPartitionProcedure()
{
return new Procedure(
"system",
"add_range_partition",
ImmutableList.of(new Argument("schema", VARCHAR), new Argument("table", VARCHAR),
new Argument("range_bounds", VARCHAR)),
ADD.bindTo(this));
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
MethodHandle methodHandle = METHOD_HANDLE.bindTo(longUnaryOperator);
return new ScalarFunctionImplementation(false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle, isDeterministic());
}
代码示例来源:origin: prestodb/presto
@Override
public Procedure get()
{
return new Procedure(
"system",
"create_empty_partition",
ImmutableList.of(
new Argument("schema_name", VARCHAR),
new Argument("table_name", VARCHAR),
new Argument("partition_columns", "array(varchar)"),
new Argument("partition_values", "array(varchar)")),
CREATE_EMPTY_PARTITION.bindTo(this));
}
代码示例来源:origin: prestodb/presto
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry)
{
Type type = boundVariables.getTypeVariable("T");
return new ScalarFunctionImplementation(
false,
ImmutableList.of(
valueTypeArgumentProperty(RETURN_NULL_ON_NULL),
valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
METHOD_HANDLE.bindTo(type),
isDeterministic());
}
内容来源于网络,如有侵权,请联系作者删除!