use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class TryCastFunction method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
Type fromType = boundVariables.getTypeVariable("F");
Type toType = boundVariables.getTypeVariable("T");
Class<?> returnType = Primitives.wrap(toType.getJavaType());
List<Boolean> nullableArguments;
MethodHandle tryCastHandle;
if (fromType.equals(UNKNOWN)) {
nullableArguments = ImmutableList.of(true);
tryCastHandle = dropArguments(constant(returnType, null), 0, Void.class);
} else {
// the resulting method needs to return a boxed type
Signature signature = functionRegistry.getCoercion(fromType, toType);
ScalarFunctionImplementation implementation = functionRegistry.getScalarFunctionImplementation(signature);
nullableArguments = implementation.getNullableArguments();
MethodHandle coercion = implementation.getMethodHandle();
coercion = coercion.asType(methodType(returnType, coercion.type()));
MethodHandle exceptionHandler = dropArguments(constant(returnType, null), 0, RuntimeException.class);
tryCastHandle = catchException(coercion, RuntimeException.class, exceptionHandler);
}
return new ScalarFunctionImplementation(true, nullableArguments, tryCastHandle, isDeterministic());
}
use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class ZipFunction method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
List<Type> types = this.typeParameters.stream().map(boundVariables::getTypeVariable).collect(toImmutableList());
List<Boolean> nullableArguments = types.stream().map(type -> false).collect(toImmutableList());
List<Class<?>> javaArgumentTypes = types.stream().map(type -> Block.class).collect(toImmutableList());
MethodHandle methodHandle = METHOD_HANDLE.bindTo(types).asVarargsCollector(Block[].class).asType(methodType(Block.class, javaArgumentTypes));
return new ScalarFunctionImplementation(false, nullableArguments, methodHandle, isDeterministic());
}
use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class RowToJsonCast method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
checkArgument(arity == 1, "Expected arity to be 1");
Type type = boundVariables.getTypeVariable("T");
MethodHandle methodHandle = METHOD_HANDLE.bindTo(type);
return new ScalarFunctionImplementation(false, ImmutableList.of(false), methodHandle, isDeterministic());
}
use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class Definition method addMethodInternal.
private void addMethodInternal(String struct, String name, boolean augmentation, Type rtn, Type[] args) {
final Struct owner = structsMap.get(struct);
if (owner == null) {
throw new IllegalArgumentException("Owner struct [" + struct + "] not defined" + " for method [" + name + "].");
}
if (!name.matches("^[_a-zA-Z][_a-zA-Z0-9]*$")) {
throw new IllegalArgumentException("Invalid method name" + " [" + name + "] with the struct [" + owner.name + "].");
}
MethodKey methodKey = new MethodKey(name, args.length);
if (owner.constructors.containsKey(methodKey)) {
throw new IllegalArgumentException("Constructors and methods" + " may not have the same signature [" + methodKey + "] within the same struct" + " [" + owner.name + "].");
}
if (owner.staticMethods.containsKey(methodKey) || owner.methods.containsKey(methodKey)) {
throw new IllegalArgumentException("Duplicate method signature [" + methodKey + "] found within the struct [" + owner.name + "].");
}
final Class<?> implClass;
final Class<?>[] params;
if (augmentation == false) {
implClass = owner.clazz;
params = new Class<?>[args.length];
for (int count = 0; count < args.length; ++count) {
params[count] = args[count].clazz;
}
} else {
implClass = Augmentation.class;
params = new Class<?>[args.length + 1];
params[0] = owner.clazz;
for (int count = 0; count < args.length; ++count) {
params[count + 1] = args[count].clazz;
}
}
final java.lang.reflect.Method reflect;
try {
reflect = implClass.getMethod(name, params);
} catch (NoSuchMethodException exception) {
throw new IllegalArgumentException("Method [" + name + "] not found for class [" + implClass.getName() + "]" + " with arguments " + Arrays.toString(params) + ".");
}
if (!reflect.getReturnType().equals(rtn.clazz)) {
throw new IllegalArgumentException("Specified return type class [" + rtn.clazz + "]" + " does not match the found return type class [" + reflect.getReturnType() + "] for the" + " method [" + name + "]" + " within the struct [" + owner.name + "].");
}
final org.objectweb.asm.commons.Method asm = org.objectweb.asm.commons.Method.getMethod(reflect);
MethodHandle handle;
try {
handle = MethodHandles.publicLookup().in(implClass).unreflect(reflect);
} catch (final IllegalAccessException exception) {
throw new IllegalArgumentException("Method [" + name + "]" + " not found for class [" + implClass.getName() + "]" + " with arguments " + Arrays.toString(params) + ".");
}
final int modifiers = reflect.getModifiers();
final Method method = new Method(name, owner, augmentation, rtn, Arrays.asList(args), asm, modifiers, handle);
if (augmentation == false && java.lang.reflect.Modifier.isStatic(modifiers)) {
owner.staticMethods.put(methodKey, method);
} else {
owner.methods.put(methodKey, method);
}
}
use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.
the class Def method lookupReferenceInternal.
/** Returns a method handle to an implementation of clazz, given method reference signature. */
private static MethodHandle lookupReferenceInternal(Lookup lookup, Definition.Type clazz, String type, String call, Class<?>... captures) throws Throwable {
final FunctionRef ref;
if ("this".equals(type)) {
// user written method
Method interfaceMethod = clazz.struct.getFunctionalMethod();
if (interfaceMethod == null) {
throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " + "to [" + clazz.name + "], not a functional interface");
}
int arity = interfaceMethod.arguments.size() + captures.length;
final MethodHandle handle;
try {
MethodHandle accessor = lookup.findStaticGetter(lookup.lookupClass(), getUserFunctionHandleFieldName(call, arity), MethodHandle.class);
handle = (MethodHandle) accessor.invokeExact();
} catch (NoSuchFieldException | IllegalAccessException e) {
// because the arity does not match the expected interface type.
if (call.contains("$")) {
throw new IllegalArgumentException("Incorrect number of parameters for [" + interfaceMethod.name + "] in [" + clazz.clazz + "]");
}
throw new IllegalArgumentException("Unknown call [" + call + "] with [" + arity + "] arguments.");
}
ref = new FunctionRef(clazz, interfaceMethod, handle, captures.length);
} else {
// whitelist lookup
ref = new FunctionRef(clazz, type, call, captures.length);
}
final CallSite callSite;
if (ref.needsBridges()) {
callSite = LambdaMetafactory.altMetafactory(lookup, ref.invokedName, ref.invokedType, ref.samMethodType, ref.implMethod, ref.samMethodType, LambdaMetafactory.FLAG_BRIDGES, 1, ref.interfaceMethodType);
} else {
callSite = LambdaMetafactory.altMetafactory(lookup, ref.invokedName, ref.invokedType, ref.samMethodType, ref.implMethod, ref.samMethodType, 0);
}
return callSite.dynamicInvoker().asType(MethodType.methodType(clazz.clazz, captures));
}
Aggregations