Пример #1
0
 private static MethodHandle unbox(Wrapper wrap, boolean cast) {
   EnumMap<Wrapper, MethodHandle> cache = UNBOX_CONVERSIONS[(cast ? 1 : 0)];
   MethodHandle mh = cache.get(wrap);
   if (mh != null) {
     return mh;
   }
   // slow path
   switch (wrap) {
     case OBJECT:
       mh = IDENTITY;
       break;
     case VOID:
       mh = IGNORE;
       break;
   }
   if (mh != null) {
     cache.put(wrap, mh);
     return mh;
   }
   // look up the method
   String name = "unbox" + wrap.wrapperSimpleName();
   MethodType type = unboxType(wrap);
   try {
     mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
   } catch (ReflectiveOperationException ex) {
     mh = null;
   }
   if (mh != null) {
     mh = MethodHandles.insertArguments(mh, 1, cast);
     cache.put(wrap, mh);
     return mh;
   }
   throw new IllegalArgumentException(
       "cannot find unbox adapter for " + wrap + (cast ? " (cast)" : ""));
 }
Пример #2
0
 /**
  * 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;
 }