private MethodHandle getMethodHandle(Method method) { MethodHandle methodHandle; try { methodHandle = lookup().unreflect(method); } catch (IllegalAccessException e) { throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, e); } if (!isStatic(method.getModifiers())) { // Re-arrange the parameters, so that the "this" parameter is after the meta parameters int[] permutedIndices = new int[methodHandle.type().parameterCount()]; permutedIndices[0] = dependencies.size(); MethodType newType = methodHandle .type() .changeParameterType(dependencies.size(), methodHandle.type().parameterType(0)); for (int i = 0; i < dependencies.size(); i++) { permutedIndices[i + 1] = i; newType = newType.changeParameterType(i, methodHandle.type().parameterType(i + 1)); } for (int i = dependencies.size() + 1; i < permutedIndices.length; i++) { permutedIndices[i] = i; } methodHandle = permuteArguments(methodHandle, newType, permutedIndices); } return methodHandle; }
public MethodType down(MethodType type) { for (int i = 0; i < type.parameterCount(); i++) { type = type.changeParameterType(i, type.parameterArray()[i]); } return type; }