< prev index next >
src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Print this page
rev 14211 : 8154751: MethodHandles.countedLoop does not accept empty bodies
*** 4474,4489 ****
* @throws IllegalArgumentException if any argument has a type inconsistent with the loop structure
*
* @since 9
*/
public static MethodHandle countedLoop(MethodHandle start, MethodHandle end, MethodHandle init, MethodHandle body) {
! MethodHandle returnVar = dropArguments(init == null || init.type().returnType() == void.class ?
! zero(void.class) : identity(init.type().returnType()), 0, int.class, int.class);
MethodHandle[] indexVar = {start, MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_countedLoopStep)};
MethodHandle[] loopLimit = {end, null, MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_countedLoopPred), returnVar};
MethodHandle[] bodyClause = {init,
! filterArgument(dropArguments(body, 1, int.class), 0,
MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_decrementCounter))};
return loop(indexVar, loopLimit, bodyClause);
}
/**
--- 4474,4493 ----
* @throws IllegalArgumentException if any argument has a type inconsistent with the loop structure
*
* @since 9
*/
public static MethodHandle countedLoop(MethodHandle start, MethodHandle end, MethodHandle init, MethodHandle body) {
! boolean voidReturn = init == null || init.type().returnType() == void.class;
! MethodHandle adaptedBody = body == null ?
! dropArguments(voidReturn ? zero(void.class) : identity(init.type().returnType()), 0, int.class) :
! body;
! MethodHandle returnVar = dropArguments(voidReturn ? zero(void.class) : identity(init.type().returnType()),
! 0, int.class, int.class);
MethodHandle[] indexVar = {start, MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_countedLoopStep)};
MethodHandle[] loopLimit = {end, null, MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_countedLoopPred), returnVar};
MethodHandle[] bodyClause = {init,
! filterArgument(dropArguments(adaptedBody, 1, int.class), 0,
MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_decrementCounter))};
return loop(indexVar, loopLimit, bodyClause);
}
/**
< prev index next >