code: public static <T> T castSafely(@Nullable Object o, @NotNull Class<T> type) {
      checkNotNull(type);
      if (type.isPrimitive()) {
        return getWrapperType(type).cast(o);
      }
      return type.cast(o);
    }
docs: Casts the given object to the given type. This method handles primitive types properly.
  
  @param o    the object to cast.
  @param type the type to cast the given object to.
  @return the given object casted to the given type.