1 /*
  2  *  Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  *  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  *  This code is free software; you can redistribute it and/or modify it
  6  *  under the terms of the GNU General Public License version 2 only, as
  7  *  published by the Free Software Foundation.  Oracle designates this
  8  *  particular file as subject to the "Classpath" exception as provided
  9  *  by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  *  This code is distributed in the hope that it will be useful, but WITHOUT
 12  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  *  version 2 for more details (a copy is included in the LICENSE file that
 15  *  accompanied this code).
 16  *
 17  *  You should have received a copy of the GNU General Public License version
 18  *  2 along with this work; if not, write to the Free Software Foundation,
 19  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  *  or visit www.oracle.com if you need additional information or have any
 23  *  questions.
 24  *
 25  */
 26 package jdk.incubator.foreign;
 27 
 28 import jdk.internal.access.JavaLangInvokeAccess;
 29 import jdk.internal.access.SharedSecrets;
 30 import sun.invoke.util.Wrapper;
 31 
 32 import java.lang.invoke.MethodHandle;
 33 import java.lang.invoke.MethodHandles;
 34 import java.lang.invoke.MethodType;
 35 import java.lang.invoke.VarHandle;
 36 import java.util.List;
 37 import java.util.Objects;
 38 
 39 /**
 40  * This class defines several factory methods for constructing and combining memory access var handles.
 41  * Memory access var handles can be obtained using {@link MemoryHandles#varHandle(ValueLayout)}. The provided value layout
 42  * determines the type, as well as the alignment constraint and the byte order associated with the memory access var handle.
 43  * <p>
 44  * The resulting memory access var handle can then be combined in various ways
 45  * to emulate different addressing modes. The var handles created by this class feature a <em>mandatory</em> coordinate type
 46  * (of type {@link MemorySegment}), and one {@code long} coordinate type, which represents the offset, in bytes, relative
 47  * to the segment, at which dereference should occur.
 48  * <p>
 49  * As an example, consider the memory layout expressed by a {@link GroupLayout} instance constructed as follows:
 50  * <blockquote><pre>{@code
 51 GroupLayout seq = MemoryLayout.structLayout(
 52         MemoryLayout.paddingLayout(32),
 53         ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN).withName("value")
 54 );
 55  * }</pre></blockquote>
 56  * To access the member layout named {@code value}, we can construct a memory access var handle as follows:
 57  * <blockquote><pre>{@code
 58 VarHandle handle = MemoryHandles.varHandle(ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN)); //(MemorySegment, long) -> int
 59 handle = MemoryHandles.insertCoordinates(handle, 1, 4); //(MemorySegment) -> int
 60  * }</pre></blockquote>
 61  *
 62  * <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null}
 63  * elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p>
 64  *
 65  * <h2><a id="memaccess-mode"></a>Alignment and access modes</h2>
 66  *
 67  * A memory access var handle is associated with an access size {@code S} and an alignment constraint {@code B}
 68  * (both expressed in bytes). We say that a memory access operation is <em>fully aligned</em> if it occurs
 69  * at a memory address {@code A} which is compatible with both alignment constraints {@code S} and {@code B}.
 70  * If access is fully aligned then following access modes are supported and are
 71  * guaranteed to support atomic access:
 72  * <ul>
 73  * <li>read write access modes for all {@code T}, with the exception of
 74  *     access modes {@code get} and {@code set} for {@code long} and
 75  *     {@code double} on 32-bit platforms.
 76  * <li>atomic update access modes for {@code int}, {@code long},
 77  *     {@code float}, {@code double} or {@link MemoryAddress}.
 78  *     (Future major platform releases of the JDK may support additional
 79  *     types for certain currently unsupported access modes.)
 80  * <li>numeric atomic update access modes for {@code int}, {@code long} and {@link MemoryAddress}.
 81  *     (Future major platform releases of the JDK may support additional
 82  *     numeric types for certain currently unsupported access modes.)
 83  * <li>bitwise atomic update access modes for {@code int}, {@code long} and {@link MemoryAddress}.
 84  *     (Future major platform releases of the JDK may support additional
 85  *     numeric types for certain currently unsupported access modes.)
 86  * </ul>
 87  *
 88  * If {@code T} is {@code float}, {@code double} or {@link MemoryAddress} then atomic
 89  * update access modes compare values using their bitwise representation
 90  * (see {@link Float#floatToRawIntBits},
 91  * {@link Double#doubleToRawLongBits} and {@link MemoryAddress#toRawLongValue()}, respectively).
 92  * <p>
 93  * Alternatively, a memory access operation is <em>partially aligned</em> if it occurs at a memory address {@code A}
 94  * which is only compatible with the alignment constraint {@code B}; in such cases, access for anything other than the
 95  * {@code get} and {@code set} access modes will result in an {@code IllegalStateException}. If access is partially aligned,
 96  * atomic access is only guaranteed with respect to the largest power of two that divides the GCD of {@code A} and {@code S}.
 97  * <p>
 98  * Finally, in all other cases, we say that a memory access operation is <em>misaligned</em>; in such cases an
 99  * {@code IllegalStateException} is thrown, irrespective of the access mode being used.
100  */
101 public final class MemoryHandles {
102 
103     private static final JavaLangInvokeAccess JLI = SharedSecrets.getJavaLangInvokeAccess();
104 
105     private MemoryHandles() {
106         //sorry, just the one!
107     }
108 
109     private static final MethodHandle INT_TO_BYTE;
110     private static final MethodHandle BYTE_TO_UNSIGNED_INT;
111     private static final MethodHandle INT_TO_SHORT;
112     private static final MethodHandle SHORT_TO_UNSIGNED_INT;
113     private static final MethodHandle LONG_TO_BYTE;
114     private static final MethodHandle BYTE_TO_UNSIGNED_LONG;
115     private static final MethodHandle LONG_TO_SHORT;
116     private static final MethodHandle SHORT_TO_UNSIGNED_LONG;
117     private static final MethodHandle LONG_TO_INT;
118     private static final MethodHandle INT_TO_UNSIGNED_LONG;
119 
120     static {
121         try {
122             INT_TO_BYTE = MethodHandles.explicitCastArguments(MethodHandles.identity(byte.class),
123                     MethodType.methodType(byte.class, int.class));
124             BYTE_TO_UNSIGNED_INT = MethodHandles.lookup().findStatic(Byte.class, "toUnsignedInt",
125                     MethodType.methodType(int.class, byte.class));
126             INT_TO_SHORT = MethodHandles.explicitCastArguments(MethodHandles.identity(short.class),
127                     MethodType.methodType(short.class, int.class));
128             SHORT_TO_UNSIGNED_INT = MethodHandles.lookup().findStatic(Short.class, "toUnsignedInt",
129                     MethodType.methodType(int.class, short.class));
130             LONG_TO_BYTE = MethodHandles.explicitCastArguments(MethodHandles.identity(byte.class),
131                     MethodType.methodType(byte.class, long.class));
132             BYTE_TO_UNSIGNED_LONG = MethodHandles.lookup().findStatic(Byte.class, "toUnsignedLong",
133                     MethodType.methodType(long.class, byte.class));
134             LONG_TO_SHORT = MethodHandles.explicitCastArguments(MethodHandles.identity(short.class),
135                     MethodType.methodType(short.class, long.class));
136             SHORT_TO_UNSIGNED_LONG = MethodHandles.lookup().findStatic(Short.class, "toUnsignedLong",
137                     MethodType.methodType(long.class, short.class));
138             LONG_TO_INT = MethodHandles.explicitCastArguments(MethodHandles.identity(int.class),
139                     MethodType.methodType(int.class, long.class));
140             INT_TO_UNSIGNED_LONG = MethodHandles.lookup().findStatic(Integer.class, "toUnsignedLong",
141                     MethodType.methodType(long.class, int.class));
142         } catch (Throwable ex) {
143             throw new ExceptionInInitializerError(ex);
144         }
145     }
146 
147     /**
148      * Creates a memory access var handle from given value layout. The provided layout will specify the
149      * {@linkplain ValueLayout#carrier() carrier type}, the {@linkplain ValueLayout#byteSize() the byte size},
150      * the {@linkplain ValueLayout#byteAlignment() byte alignment} and the {@linkplain ValueLayout#order() byte order}
151      * associated to the returned var handle.
152      *
153      * The returned var handle's type is {@code carrier} and the list of coordinate types is
154      * {@code (MemorySegment, long)}, where the {@code long} coordinate type corresponds to byte offset into
155      * a given memory segment. The returned var handle accesses bytes at an offset in a given
156      * memory segment, composing bytes to or from a value of the type {@code carrier} according to the given endianness;
157      * the alignment constraint (in bytes) for the resulting memory access var handle is given by {@code alignmentBytes}.
158      *
159      * @apiNote the resulting var handle features certain <a href="#memaccess-mode">access mode restrictions</a>,
160      * which are common to all memory access var handles.
161      *
162      * @param layout the value layout for which a memory access handle is to be obtained.
163      * @return the new memory access var handle.
164      * @throws IllegalArgumentException if an illegal carrier type is used, or if {@code alignmentBytes} is not a power of two.
165      */
166     public static VarHandle varHandle(ValueLayout layout) {
167         Objects.requireNonNull(layout);
168         return layout.accessHandle();
169     }
170 
171     /**
172      * Adapts a target var handle by narrowing incoming values and widening
173      * outgoing values, to and from the given type, respectively.
174      * <p>
175      * The returned var handle can be used to conveniently treat unsigned
176      * primitive data types as if they were a wider signed primitive type. For
177      * example, it is often convenient to model an <i>unsigned short</i> as a
178      * Java {@code int} to avoid dealing with negative values, which would be
179      * the case if modeled as a Java {@code short}. This is illustrated in the following example:
180      * <blockquote><pre>{@code
181     MemorySegment segment = MemorySegment.allocateNative(2, ResourceScope.newImplicitScope());
182     VarHandle SHORT_VH = ValueLayout.JAVA_SHORT.varHandle();
183     VarHandle INT_VH = MemoryHandles.asUnsigned(SHORT_VH, int.class);
184     SHORT_VH.set(segment, (short)-1);
185     INT_VH.get(segment); // returns 65535
186      * }</pre></blockquote>
187      * <p>
188      * When calling e.g. {@link VarHandle#set(Object...)} on the resulting var
189      * handle, the incoming value (of type {@code adaptedType}) is converted by a
190      * <i>narrowing primitive conversion</i> and then passed to the {@code
191      * target} var handle. A narrowing primitive conversion may lose information
192      * about the overall magnitude of a numeric value. Conversely, when calling
193      * e.g. {@link VarHandle#get(Object...)} on the resulting var handle, the
194      * returned value obtained from the {@code target} var handle is converted
195      * by a <i>unsigned widening conversion</i> before being returned to the
196      * caller. In an unsigned widening conversion the high-order bits greater
197      * than that of the {@code target} carrier type are zero, and the low-order
198      * bits (equal to the width of the {@code target} carrier type) are equal to
199      * the bits of the value obtained from the {@code target} var handle.
200      * <p>
201      * The returned var handle will feature the variable type {@code adaptedType},
202      * and the same access coordinates, the same access modes (see {@link
203      * java.lang.invoke.VarHandle.AccessMode}), and the same atomic access
204      * guarantees, as those featured by the {@code target} var handle.
205      *
206      * @param target the memory access var handle to be adapted
207      * @param adaptedType the adapted type
208      * @return the adapted var handle.
209      * @throws IllegalArgumentException if the carrier type of {@code target}
210      * is not one of {@code byte}, {@code short}, or {@code int}; if {@code
211      * adaptedType} is not one of {@code int}, or {@code long}; if the bit width
212      * of the {@code adaptedType} is not greater than that of the {@code target}
213      * carrier type.
214      *
215      * @jls 5.1.3 Narrowing Primitive Conversion
216      */
217     public static VarHandle asUnsigned(VarHandle target, final Class<?> adaptedType) {
218         Objects.requireNonNull(target);
219         Objects.requireNonNull(adaptedType);
220         final Class<?> carrier = target.varType();
221         checkWidenable(carrier);
222         checkNarrowable(adaptedType);
223         checkTargetWiderThanCarrier(carrier, adaptedType);
224 
225         if (adaptedType == int.class && carrier == byte.class) {
226             return filterValue(target, INT_TO_BYTE, BYTE_TO_UNSIGNED_INT);
227         } else if (adaptedType == int.class && carrier == short.class) {
228             return filterValue(target, INT_TO_SHORT, SHORT_TO_UNSIGNED_INT);
229         } else if (adaptedType == long.class && carrier == byte.class) {
230             return filterValue(target, LONG_TO_BYTE, BYTE_TO_UNSIGNED_LONG);
231         } else if (adaptedType == long.class && carrier == short.class) {
232             return filterValue(target, LONG_TO_SHORT, SHORT_TO_UNSIGNED_LONG);
233         } else if (adaptedType == long.class && carrier == int.class) {
234             return filterValue(target, LONG_TO_INT, INT_TO_UNSIGNED_LONG);
235         } else {
236             throw new InternalError("should not reach here");
237         }
238     }
239 
240     /**
241      * Adapts a target var handle by pre-processing incoming and outgoing values using a pair of filter functions.
242      * <p>
243      * When calling e.g. {@link VarHandle#set(Object...)} on the resulting var handle, the incoming value (of type {@code T}, where
244      * {@code T} is the <em>last</em> parameter type of the first filter function) is processed using the first filter and then passed
245      * to the target var handle.
246      * Conversely, when calling e.g. {@link VarHandle#get(Object...)} on the resulting var handle, the return value obtained from
247      * the target var handle (of type {@code T}, where {@code T} is the <em>last</em> parameter type of the second filter function)
248      * is processed using the second filter and returned to the caller. More advanced access mode types, such as
249      * {@link java.lang.invoke.VarHandle.AccessMode#COMPARE_AND_EXCHANGE} might apply both filters at the same time.
250      * <p>
251      * For the boxing and unboxing filters to be well-formed, their types must be of the form {@code (A... , S) -> T} and
252      * {@code (A... , T) -> S}, respectively, where {@code T} is the type of the target var handle. If this is the case,
253      * the resulting var handle will have type {@code S} and will feature the additional coordinates {@code A...} (which
254      * will be appended to the coordinates of the target var handle).
255      * <p>
256      * If the boxing and unboxing filters throw any checked exceptions when invoked, the resulting var handle will
257      * throw an {@link IllegalStateException}.
258      * <p>
259      * The resulting var handle will feature the same access modes (see {@link java.lang.invoke.VarHandle.AccessMode}) and
260      * atomic access guarantees as those featured by the target var handle.
261      *
262      * @param target the target var handle
263      * @param filterToTarget a filter to convert some type {@code S} into the type of {@code target}
264      * @param filterFromTarget a filter to convert the type of {@code target} to some type {@code S}
265      * @return an adapter var handle which accepts a new type, performing the provided boxing/unboxing conversions.
266      * @throws IllegalArgumentException if {@code filterFromTarget} and {@code filterToTarget} are not well-formed, that is, they have types
267      * other than {@code (A... , S) -> T} and {@code (A... , T) -> S}, respectively, where {@code T} is the type of the target var handle,
268      * or if it's determined that either {@code filterFromTarget} or {@code filterToTarget} throws any checked exceptions.
269      */
270     public static VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
271         return JLI.filterValue(target, filterToTarget, filterFromTarget);
272     }
273 
274     /**
275      * Adapts a target var handle by pre-processing incoming coordinate values using unary filter functions.
276      * <p>
277      * When calling e.g. {@link VarHandle#get(Object...)} on the resulting var handle, the incoming coordinate values
278      * starting at position {@code pos} (of type {@code C1, C2 ... Cn}, where {@code C1, C2 ... Cn} are the return type
279      * of the unary filter functions) are transformed into new values (of type {@code S1, S2 ... Sn}, where {@code S1, S2 ... Sn} are the
280      * parameter types of the unary filter functions), and then passed (along with any coordinate that was left unaltered
281      * by the adaptation) to the target var handle.
282      * <p>
283      * For the coordinate filters to be well-formed, their types must be of the form {@code S1 -> T1, S2 -> T1 ... Sn -> Tn},
284      * where {@code T1, T2 ... Tn} are the coordinate types starting at position {@code pos} of the target var handle.
285      * <p>
286      * If any of the filters throws a checked exception when invoked, the resulting var handle will
287      * throw an {@link IllegalStateException}.
288      * <p>
289      * The resulting var handle will feature the same access modes (see {@link java.lang.invoke.VarHandle.AccessMode}) and
290      * atomic access guarantees as those featured by the target var handle.
291      *
292      * @param target the target var handle
293      * @param pos the position of the first coordinate to be transformed
294      * @param filters the unary functions which are used to transform coordinates starting at position {@code pos}
295      * @return an adapter var handle which accepts new coordinate types, applying the provided transformation
296      * to the new coordinate values.
297      * @throws IllegalArgumentException if the handles in {@code filters} are not well-formed, that is, they have types
298      * other than {@code S1 -> T1, S2 -> T2, ... Sn -> Tn} where {@code T1, T2 ... Tn} are the coordinate types starting
299      * at position {@code pos} of the target var handle, if {@code pos} is not between 0 and the target var handle coordinate arity, inclusive,
300      * or if more filters are provided than the actual number of coordinate types available starting at {@code pos},
301      * or if it's determined that any of the filters throws any checked exceptions.
302      */
303     public static VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
304         return JLI.filterCoordinates(target, pos, filters);
305     }
306 
307     /**
308      * Provides a target var handle with one or more <em>bound coordinates</em>
309      * in advance of the var handle's invocation. As a consequence, the resulting var handle will feature less
310      * coordinate types than the target var handle.
311      * <p>
312      * When calling e.g. {@link VarHandle#get(Object...)} on the resulting var handle, incoming coordinate values
313      * are joined with bound coordinate values, and then passed to the target var handle.
314      * <p>
315      * For the bound coordinates to be well-formed, their types must be {@code T1, T2 ... Tn },
316      * where {@code T1, T2 ... Tn} are the coordinate types starting at position {@code pos} of the target var handle.
317      * <p>
318      * The resulting var handle will feature the same access modes (see {@link java.lang.invoke.VarHandle.AccessMode}) and
319      * atomic access guarantees as those featured by the target var handle.
320      *
321      * @param target the var handle to invoke after the bound coordinates are inserted
322      * @param pos the position of the first coordinate to be inserted
323      * @param values the series of bound coordinates to insert
324      * @return an adapter var handle which inserts an additional coordinates,
325      *         before calling the target var handle
326      * @throws IllegalArgumentException if {@code pos} is not between 0 and the target var handle coordinate arity, inclusive,
327      * or if more values are provided than the actual number of coordinate types available starting at {@code pos}.
328      * @throws ClassCastException if the bound coordinates in {@code values} are not well-formed, that is, they have types
329      * other than {@code T1, T2 ... Tn }, where {@code T1, T2 ... Tn} are the coordinate types starting at position {@code pos}
330      * of the target var handle.
331      */
332     public static VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
333         return JLI.insertCoordinates(target, pos, values);
334     }
335 
336     /**
337      * Provides a var handle which adapts the coordinate values of the target var handle, by re-arranging them
338      * so that the new coordinates match the provided ones.
339      * <p>
340      * The given array controls the reordering.
341      * Call {@code #I} the number of incoming coordinates (the value
342      * {@code newCoordinates.size()}), and call {@code #O} the number
343      * of outgoing coordinates (the number of coordinates associated with the target var handle).
344      * Then the length of the reordering array must be {@code #O},
345      * and each element must be a non-negative number less than {@code #I}.
346      * For every {@code N} less than {@code #O}, the {@code N}-th
347      * outgoing coordinate will be taken from the {@code I}-th incoming
348      * coordinate, where {@code I} is {@code reorder[N]}.
349      * <p>
350      * No coordinate value conversions are applied.
351      * The type of each incoming coordinate, as determined by {@code newCoordinates},
352      * must be identical to the type of the corresponding outgoing coordinate
353      * in the target var handle.
354      * <p>
355      * The reordering array need not specify an actual permutation.
356      * An incoming coordinate will be duplicated if its index appears
357      * more than once in the array, and an incoming coordinate will be dropped
358      * if its index does not appear in the array.
359      * <p>
360      * The resulting var handle will feature the same access modes (see {@link java.lang.invoke.VarHandle.AccessMode}) and
361      * atomic access guarantees as those featured by the target var handle.
362      * @param target the var handle to invoke after the coordinates have been reordered
363      * @param newCoordinates the new coordinate types
364      * @param reorder an index array which controls the reordering
365      * @return an adapter var handle which re-arranges the incoming coordinate values,
366      * before calling the target var handle
367      * @throws IllegalArgumentException if the index array length is not equal to
368      * the number of coordinates of the target var handle, or if any index array element is not a valid index for
369      * a coordinate of {@code newCoordinates}, or if two corresponding coordinate types in
370      * the target var handle and in {@code newCoordinates} are not identical.
371      */
372     public static VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
373         return JLI.permuteCoordinates(target, newCoordinates, reorder);
374     }
375 
376     /**
377      * Adapts a target var handle by pre-processing
378      * a sub-sequence of its coordinate values with a filter (a method handle).
379      * The pre-processed coordinates are replaced by the result (if any) of the
380      * filter function and the target var handle is then called on the modified (usually shortened)
381      * coordinate list.
382      * <p>
383      * If {@code R} is the return type of the filter (which cannot be void), the target var handle must accept a value of
384      * type {@code R} as its coordinate in position {@code pos}, preceded and/or followed by
385      * any coordinate not passed to the filter.
386      * No coordinates are reordered, and the result returned from the filter
387      * replaces (in order) the whole subsequence of coordinates originally
388      * passed to the adapter.
389      * <p>
390      * The argument types (if any) of the filter
391      * replace zero or one coordinate types of the target var handle, at position {@code pos},
392      * in the resulting adapted var handle.
393      * The return type of the filter must be identical to the
394      * coordinate type of the target var handle at position {@code pos}, and that target var handle
395      * coordinate is supplied by the return value of the filter.
396      * <p>
397      * If any of the filters throws a checked exception when invoked, the resulting var handle will
398      * throw an {@link IllegalStateException}.
399      * <p>
400      * The resulting var handle will feature the same access modes (see {@link java.lang.invoke.VarHandle.AccessMode}) and
401      * atomic access guarantees as those featured by the target var handle.
402      *
403      * @param target the var handle to invoke after the coordinates have been filtered
404      * @param pos the position of the coordinate to be filtered
405      * @param filter the filter method handle
406      * @return an adapter var handle which filters the incoming coordinate values,
407      * before calling the target var handle
408      * @throws IllegalArgumentException if the return type of {@code filter}
409      * is void, or it is not the same as the {@code pos} coordinate of the target var handle,
410      * if {@code pos} is not between 0 and the target var handle coordinate arity, inclusive,
411      * if the resulting var handle's type would have <a href="MethodHandle.html#maxarity">too many coordinates</a>,
412      * or if it's determined that {@code filter} throws any checked exceptions.
413      */
414     public static VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
415         return JLI.collectCoordinates(target, pos, filter);
416     }
417 
418     /**
419      * Returns a var handle which will discard some dummy coordinates before delegating to the
420      * target var handle. As a consequence, the resulting var handle will feature more
421      * coordinate types than the target var handle.
422      * <p>
423      * The {@code pos} argument may range between zero and <i>N</i>, where <i>N</i> is the arity of the
424      * target var handle's coordinate types. If {@code pos} is zero, the dummy coordinates will precede
425      * the target's real arguments; if {@code pos} is <i>N</i> they will come after.
426      * <p>
427      * The resulting var handle will feature the same access modes (see {@link java.lang.invoke.VarHandle.AccessMode}) and
428      * atomic access guarantees as those featured by the target var handle.
429      *
430      * @param target the var handle to invoke after the dummy coordinates are dropped
431      * @param pos position of first coordinate to drop (zero for the leftmost)
432      * @param valueTypes the type(s) of the coordinate(s) to drop
433      * @return an adapter var handle which drops some dummy coordinates,
434      *         before calling the target var handle
435      * @throws IllegalArgumentException if {@code pos} is not between 0 and the target var handle coordinate arity, inclusive.
436      */
437     public static VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
438         return JLI.dropCoordinates(target, pos, valueTypes);
439     }
440 
441     private static void checkWidenable(Class<?> carrier) {
442         if (!(carrier == byte.class || carrier == short.class || carrier == int.class)) {
443             throw new IllegalArgumentException("illegal carrier:" + carrier.getSimpleName());
444         }
445     }
446 
447     private static void checkNarrowable(Class<?> type) {
448         if (!(type == int.class || type == long.class)) {
449             throw new IllegalArgumentException("illegal adapter type: " + type.getSimpleName());
450         }
451     }
452 
453     private static void checkTargetWiderThanCarrier(Class<?> carrier, Class<?> target) {
454         if (Wrapper.forPrimitiveType(target).bitWidth() <= Wrapper.forPrimitiveType(carrier).bitWidth()) {
455             throw new IllegalArgumentException(
456                     target.getSimpleName() + " is not wider than: " + carrier.getSimpleName());
457         }
458     }
459 }