8255374: Add a dropReturn MethodHandle combinator

Reviewed-by: redestad
This commit is contained in:
Jorn Vernee 2020-11-03 12:10:48 +00:00
parent 1d0bd50624
commit b8d4e02ce7
2 changed files with 91 additions and 0 deletions

View file

@ -62,6 +62,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.invoke.LambdaForm.BasicType.V_TYPE;
import static java.lang.invoke.MethodHandleImpl.Intrinsic;
import static java.lang.invoke.MethodHandleNatives.Constants.*;
import static java.lang.invoke.MethodHandleStatics.newIllegalArgumentException;
@ -5205,6 +5206,28 @@ assertEquals("xy", h3.invoke("x", "y", 1, "a", "b", "c"));
return dropArgumentsToMatch(target, skip, newTypes, pos, false);
}
/**
* Drop the return value of the target handle (if any).
* The returned method handle will have a {@code void} return type.
*
* @param target the method handle to adapt
* @return a possibly adapted method handle
* @throws NullPointerException if {@code target} is null
* @since 16
*/
public static MethodHandle dropReturn(MethodHandle target) {
Objects.requireNonNull(target);
MethodType oldType = target.type();
Class<?> oldReturnType = oldType.returnType();
if (oldReturnType == void.class)
return target;
MethodType newType = oldType.changeReturnType(void.class);
BoundMethodHandle result = target.rebind();
LambdaForm lform = result.editor().filterReturnForm(V_TYPE, true);
result = result.copyWith(newType, lform);
return result;
}
/**
* Adapts a target method handle by pre-processing
* one or more of its arguments, each with its own unary filter function,