/** * Return a method that casts its sole argument (an Object) to the given type and returns it as * the given type. */ public static MethodHandle cast(Class<?> type) { if (type.isPrimitive()) throw new IllegalArgumentException("cannot cast primitive type " + type); MethodHandle mh; Wrapper wrap = null; EnumMap<Wrapper, MethodHandle> cache = null; if (Wrapper.isWrapperType(type)) { wrap = Wrapper.forWrapperType(type); cache = WRAPPER_CASTS[0]; mh = cache.get(wrap); if (mh != null) return mh; } mh = MethodHandles.insertArguments(CAST_REFERENCE, 0, type); if (cache != null) cache.put(wrap, mh); return mh; }