src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Cdiff src/java.base/share/classes/java/lang/invoke/MethodHandles.java

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Print this page
rev 10592 : 8050052: Small cleanups in java.lang.invoke code
Reviewed-by: ?
rev 10593 : 8050053: Improve caching of different invokers
Reviewed-by: vlivanov, ?
Contributed-by: john.r.rose@oracle.com
rev 10594 : 8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
Reviewed-by: vlivanov, ?
Contributed-by: john.r.rose@oracle.com
rev 10595 : 8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
Reviewed-by: vlivanov, ?
Contributed-by: john.r.rose@oracle.com
rev 10596 : [mq]: 07.8050173.viewAsType.1
rev 10600 : imported patch 11.8050877.conv
rev 10601 : imported patch 12.8050884.identity
rev 10602 : imported patch 13.8050887.zero
rev 10603 : 8057654: Extract checks performed during MethodHandle construction into separate methods
Reviewed-by: vlivanov, ?
Contributed-by: john.r.rose@oracle.com

*** 2506,2541 **** * or if the resulting method handle's type would have * <a href="MethodHandle.html#maxarity">too many parameters</a> */ public static MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) { ! MethodType targetType = target.type(); MethodHandle adapter = target; - MethodType adapterType = null; - assert((adapterType = targetType) != null); - int maxPos = targetType.parameterCount(); - if (pos + filters.length > maxPos) - throw newIllegalArgumentException("too many filters"); int curPos = pos-1; // pre-incremented for (MethodHandle filter : filters) { curPos += 1; if (filter == null) continue; // ignore null elements of filters adapter = filterArgument(adapter, curPos, filter); - assert((adapterType = adapterType.changeParameterType(curPos, filter.type().parameterType(0))) != null); } - assert(adapterType.equals(adapter.type())); return adapter; } /*non-public*/ static MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) { MethodType targetType = target.type(); MethodType filterType = filter.type(); if (filterType.parameterCount() != 1 || filterType.returnType() != targetType.parameterType(pos)) throw newIllegalArgumentException("target and filter types do not match", targetType, filterType); - return MethodHandleImpl.makeCollectArguments(target, filter, pos, false); } /** * Adapts a target method handle by pre-processing * a sub-sequence of its arguments with a filter (another method handle). --- 2506,2545 ---- * or if the resulting method handle's type would have * <a href="MethodHandle.html#maxarity">too many parameters</a> */ public static MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) { ! filterArgumentsCheckArity(target, pos, filters); MethodHandle adapter = target; int curPos = pos-1; // pre-incremented for (MethodHandle filter : filters) { curPos += 1; if (filter == null) continue; // ignore null elements of filters adapter = filterArgument(adapter, curPos, filter); } return adapter; } /*non-public*/ static MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) { + filterArgumentChecks(target, pos, filter); + return MethodHandleImpl.makeCollectArguments(target, filter, pos, false); + } + + private static void filterArgumentsCheckArity(MethodHandle target, int pos, MethodHandle[] filters) { + MethodType targetType = target.type(); + int maxPos = targetType.parameterCount(); + if (pos + filters.length > maxPos) + throw newIllegalArgumentException("too many filters"); + } + + private static void filterArgumentChecks(MethodHandle target, int pos, MethodHandle filter) throws RuntimeException { MethodType targetType = target.type(); MethodType filterType = filter.type(); if (filterType.parameterCount() != 1 || filterType.returnType() != targetType.parameterType(pos)) throw newIllegalArgumentException("target and filter types do not match", targetType, filterType); } /** * Adapts a target method handle by pre-processing * a sub-sequence of its arguments with a filter (another method handle).
*** 2711,2729 **** */ public static MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) { MethodType targetType = target.type(); MethodType filterType = filter.type(); Class<?> rtype = targetType.returnType(); int filterValues = filterType.parameterCount(); if (filterValues == 0 ? (rtype != void.class) : (rtype != filterType.parameterType(0))) ! throw newIllegalArgumentException("target and filter types do not match", target, filter); ! // result = fold( lambda(retval, arg...) { filter(retval) }, ! // lambda( arg...) { target(arg...) } ) ! return MethodHandleImpl.makeCollectArguments(filter, target, 0, false); } /** * Adapts a target method handle by pre-processing * some of its arguments, and then calling the target with --- 2715,2735 ---- */ public static MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) { MethodType targetType = target.type(); MethodType filterType = filter.type(); + filterReturnValueChecks(targetType, filterType); + return MethodHandleImpl.makeCollectArguments(filter, target, 0, false); + } + + private static void filterReturnValueChecks(MethodType targetType, MethodType filterType) throws RuntimeException { Class<?> rtype = targetType.returnType(); int filterValues = filterType.parameterCount(); if (filterValues == 0 ? (rtype != void.class) : (rtype != filterType.parameterType(0))) ! throw newIllegalArgumentException("target and filter types do not match", targetType, filterType); } /** * Adapts a target method handle by pre-processing * some of its arguments, and then calling the target with
*** 2800,2827 **** * (skipping one matching the {@code combiner}'s return type) * are not identical with the argument types of {@code combiner} */ public static MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) { ! int pos = 0; MethodType targetType = target.type(); MethodType combinerType = combiner.type(); ! int foldPos = pos; int foldArgs = combinerType.parameterCount(); ! int foldVals = combinerType.returnType() == void.class ? 0 : 1; int afterInsertPos = foldPos + foldVals; boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs); if (ok && !(combinerType.parameterList() .equals(targetType.parameterList().subList(afterInsertPos, afterInsertPos + foldArgs)))) ok = false; ! if (ok && foldVals != 0 && !combinerType.returnType().equals(targetType.parameterType(0))) ok = false; if (!ok) throw misMatchedTypes("target and combiner types", targetType, combinerType); ! MethodType newType = targetType.dropParameterTypes(foldPos, afterInsertPos); ! return MethodHandleImpl.makeCollectArguments(target, combiner, foldPos, true); } /** * Makes a method handle which adapts a target method handle, * by guarding it with a test, a boolean-valued method handle. --- 2806,2837 ---- * (skipping one matching the {@code combiner}'s return type) * are not identical with the argument types of {@code combiner} */ public static MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) { ! int foldPos = 0; MethodType targetType = target.type(); MethodType combinerType = combiner.type(); ! Class<?> rtype = foldArgumentChecks(foldPos, targetType, combinerType); ! return MethodHandleImpl.makeCollectArguments(target, combiner, foldPos, true); ! } ! ! private static Class<?> foldArgumentChecks(int foldPos, MethodType targetType, MethodType combinerType) { int foldArgs = combinerType.parameterCount(); ! Class<?> rtype = combinerType.returnType(); ! int foldVals = rtype == void.class ? 0 : 1; int afterInsertPos = foldPos + foldVals; boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs); if (ok && !(combinerType.parameterList() .equals(targetType.parameterList().subList(afterInsertPos, afterInsertPos + foldArgs)))) ok = false; ! if (ok && foldVals != 0 && combinerType.returnType() != targetType.parameterType(0)) ok = false; if (!ok) throw misMatchedTypes("target and combiner types", targetType, combinerType); ! return rtype; } /** * Makes a method handle which adapts a target method handle, * by guarding it with a test, a boolean-valued method handle.
src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File