1056 MethodType needType = mtype.asSpreaderType(arrayType, pos, arrayLength);
1057 if (!fail) return needType;
1058 // elicit an error:
1059 this.asType(needType);
1060 throw newInternalError("should not return");
1061 }
1062
1063 private void spreadArrayChecks(Class<?> arrayType, int arrayLength) {
1064 Class<?> arrayElement = arrayType.getComponentType();
1065 if (arrayElement == null)
1066 throw newIllegalArgumentException("not an array type", arrayType);
1067 if ((arrayLength & 0x7F) != arrayLength) {
1068 if ((arrayLength & 0xFF) != arrayLength)
1069 throw newIllegalArgumentException("array length is not legal", arrayLength);
1070 assert(arrayLength >= 128);
1071 if (arrayElement == long.class ||
1072 arrayElement == double.class)
1073 throw newIllegalArgumentException("array length is not legal for long[] or double[]", arrayLength);
1074 }
1075 }
1076 /**
1077 * Adapts this method handle to be {@linkplain #asVarargsCollector variable arity}
1078 * if the boolean flag is true, else {@linkplain #asFixedArity fixed arity}.
1079 * If the method handle is already of the proper arity mode, it is returned
1080 * unchanged.
1081 * @apiNote
1082 * <p>This method is sometimes useful when adapting a method handle that
1083 * may be variable arity, to ensure that the resulting adapter is also
1084 * variable arity if and only if the original handle was. For example,
1085 * this code changes the first argument of a handle {@code mh} to {@code int} without
1086 * disturbing its variable arity property:
1087 * {@code mh.asType(mh.type().changeParameterType(0,int.class))
1088 * .withVarargs(mh.isVarargsCollector())}
1089 * <p>
1090 * This call is approximately equivalent to the following code:
1091 * <blockquote><pre>{@code
1092 * if (makeVarargs == isVarargsCollector())
1093 * return this;
1094 * else if (makeVarargs)
1095 * return asVarargsCollector(type().lastParameterType());
1096 * else
1097 * return asFixedArity();
1098 * }</pre></blockquote>
1099 * @param makeVarargs true if the return method handle should have variable arity behavior
1100 * @return a method handle of the same type, with possibly adjusted variable arity behavior
1101 * @throws IllegalArgumentException if {@code makeVarargs} is true and
1102 * this method handle does not have a trailing array parameter
1103 * @since 9
1104 * @see #asVarargsCollector
1105 * @see #asFixedArity
1106 */
1107 public MethodHandle withVarargs(boolean makeVarargs) {
1108 assert(!isVarargsCollector()); // subclass responsibility
1109 if (makeVarargs) {
1110 return asVarargsCollector(type().lastParameterType());
1111 } else {
1112 return this;
1113 }
1114 }
1115
1116 /**
1117 * Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing
1118 * positional arguments and collects them into an array argument.
1119 * The new method handle adapts, as its <i>target</i>,
1120 * the current method handle. The type of the adapter will be
1121 * the same as the type of the target, except that a single trailing
1122 * parameter (usually of type {@code arrayType}) is replaced by
1123 * {@code arrayLength} parameters whose type is element type of {@code arrayType}.
1124 * <p>
1125 * If the array type differs from the final argument type on the original target,
|
1056 MethodType needType = mtype.asSpreaderType(arrayType, pos, arrayLength);
1057 if (!fail) return needType;
1058 // elicit an error:
1059 this.asType(needType);
1060 throw newInternalError("should not return");
1061 }
1062
1063 private void spreadArrayChecks(Class<?> arrayType, int arrayLength) {
1064 Class<?> arrayElement = arrayType.getComponentType();
1065 if (arrayElement == null)
1066 throw newIllegalArgumentException("not an array type", arrayType);
1067 if ((arrayLength & 0x7F) != arrayLength) {
1068 if ((arrayLength & 0xFF) != arrayLength)
1069 throw newIllegalArgumentException("array length is not legal", arrayLength);
1070 assert(arrayLength >= 128);
1071 if (arrayElement == long.class ||
1072 arrayElement == double.class)
1073 throw newIllegalArgumentException("array length is not legal for long[] or double[]", arrayLength);
1074 }
1075 }
1076
1077 /**
1078 * Adapts this method handle to be {@linkplain #asVarargsCollector variable arity}
1079 * if the boolean flag is true, else {@linkplain #asFixedArity fixed arity}.
1080 * If the method handle is already of the proper arity mode, it is returned
1081 * unchanged.
1082 * @apiNote
1083 * <p>This method is sometimes useful when adapting a method handle that
1084 * may be variable arity, to ensure that the resulting adapter is also
1085 * variable arity if and only if the original handle was. For example,
1086 * this code changes the first argument of a handle {@code mh} to {@code int} without
1087 * disturbing its variable arity property:
1088 * {@code mh.asType(mh.type().changeParameterType(0,int.class))
1089 * .withVarargs(mh.isVarargsCollector())}
1090 * <p>
1091 * This call is approximately equivalent to the following code:
1092 * <blockquote><pre>{@code
1093 * if (makeVarargs == isVarargsCollector())
1094 * return this;
1095 * else if (makeVarargs)
1096 * return asVarargsCollector(type().lastParameterType());
1097 * else
1098 * return asFixedArity();
1099 * }</pre></blockquote>
1100 * @param makeVarargs true if the return method handle should have variable arity behavior
1101 * @return a method handle of the same type, with possibly adjusted variable arity behavior
1102 * @throws IllegalArgumentException if {@code makeVarargs} is true and
1103 * this method handle does not have a trailing array parameter
1104 * @since 9
1105 * @see #asVarargsCollector
1106 * @see #asFixedArity
1107 */
1108 public MethodHandle withVarargs(boolean makeVarargs) {
1109 assert(!isVarargsCollector()); // subclass responsibility
1110 if (makeVarargs) {
1111 return asVarargsCollector(type().lastParameterType());
1112 } else {
1113 return this;
1114 }
1115 }
1116
1117 /**
1118 * Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing
1119 * positional arguments and collects them into an array argument.
1120 * The new method handle adapts, as its <i>target</i>,
1121 * the current method handle. The type of the adapter will be
1122 * the same as the type of the target, except that a single trailing
1123 * parameter (usually of type {@code arrayType}) is replaced by
1124 * {@code arrayLength} parameters whose type is element type of {@code arrayType}.
1125 * <p>
1126 * If the array type differs from the final argument type on the original target,
|