/** * Merge the given array into the given Collection. * @param array the array to merge (may be {@code null}) * @param collection the target Collection to merge the array into */ @SuppressWarnings("unchecked") public static <E> void mergeArrayIntoCollection(@Nullable Object array, Collection<E> collection) { Object[] arr = ObjectUtils.toObjectArray(array); for (Object elem : arr) { collection.add((E) elem); } }
private Object[] extractArgs(Method method, Object[] args) { if (!method.isVarArgs()) { return args; } Object[] varArgs = ObjectUtils.toObjectArray(args[args.length - 1]); Object[] combinedArgs = new Object[args.length - 1 + varArgs.length]; System.arraycopy(args, 0, combinedArgs, 0, args.length - 1); System.arraycopy(varArgs, 0, combinedArgs, args.length - 1, varArgs.length); return combinedArgs; }
/** * Convert the supplied array into a List. A primitive array gets converted * into a List of the appropriate wrapper type. * <p><b>NOTE:</b> Generally prefer the standard {@link Arrays#asList} method. * This {@code arrayToList} method is just meant to deal with an incoming Object * value that might be an {@code Object[]} or a primitive array at runtime. * <p>A {@code null} source value will be converted to an empty List. * @param source the (potentially primitive) array * @return the converted List result * @see ObjectUtils#toObjectArray(Object) * @see Arrays#asList(Object[]) */ @SuppressWarnings("rawtypes") public static List arrayToList(@Nullable Object source) { return Arrays.asList(ObjectUtils.toObjectArray(source)); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType); }
private Object[] extractArgs(Method method, Object[] args) { if (!method.isVarArgs()) { return args; } Object[] varArgs = ObjectUtils.toObjectArray(args[args.length - 1]); Object[] combinedArgs = new Object[args.length - 1 + varArgs.length]; System.arraycopy(args, 0, combinedArgs, 0, args.length - 1); System.arraycopy(varArgs, 0, combinedArgs, args.length - 1, varArgs.length); return combinedArgs; }
/** * Merge the given array into the given Collection. * @param array the array to merge (may be {@code null}) * @param collection the target Collection to merge the array into */ @SuppressWarnings("unchecked") public static <E> void mergeArrayIntoCollection(@Nullable Object array, Collection<E> collection) { Object[] arr = ObjectUtils.toObjectArray(array); for (Object elem : arr) { collection.add((E) elem); } }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType); }
@Override public String getAsText() { return StringUtils.arrayToDelimitedString(ObjectUtils.toObjectArray(getValue()), this.separator); }
@Override public Object getValue(@Nullable String name) { Object value = this.delegate.getValue(name); if (ObjectUtils.isArray(value)) { value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value)); } return value; } }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (this.conversionService instanceof GenericConversionService) { TypeDescriptor targetElement = targetType.getElementTypeDescriptor(); if (targetElement != null && ((GenericConversionService) this.conversionService).canBypassConvert( sourceType.getElementTypeDescriptor(), targetElement)) { return source; } } List<Object> sourceList = Arrays.asList(ObjectUtils.toObjectArray(source)); return this.helperConverter.convert(sourceList, sourceType, targetType); }
protected void handleResult(Object result) { if (result.getClass().isArray()) { Object[] events = ObjectUtils.toObjectArray(result); for (Object event : events) { publishEvent(event); } } else if (result instanceof Collection<?>) { Collection<?> events = (Collection<?>) result; for (Object event : events) { publishEvent(event); } } else { publishEvent(result); } }
/** * Determine the order value for the given object. * <p>The default implementation checks against the given {@link OrderSourceProvider} * using {@link #findOrder} and falls back to a regular {@link #getOrder(Object)} call. * @param obj the object to check * @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback */ private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) { Integer order = null; if (obj != null && sourceProvider != null) { Object orderSource = sourceProvider.getOrderSource(obj); if (orderSource != null) { if (orderSource.getClass().isArray()) { Object[] sources = ObjectUtils.toObjectArray(orderSource); for (Object source : sources) { order = findOrder(source); if (order != null) { break; } } } else { order = findOrder(orderSource); } } } return (order != null ? order : getOrder(obj)); }
return styleArray(ObjectUtils.toObjectArray(value));
@Override public Object getValue(@Nullable String name) { Object value = this.delegate.getValue(name); if (ObjectUtils.isArray(value)) { value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value)); } return value; } }
@Test public void toObjectArrayWithNonArrayType() { exception.expect(IllegalArgumentException.class); ObjectUtils.toObjectArray("Not an []"); }
@Test public void toObjectArrayWithNonPrimitiveArray() { String[] source = new String[] {"Bingo"}; assertArrayEquals(source, ObjectUtils.toObjectArray(source)); }
@Test public void toObjectArray() { int[] a = new int[] {1, 2, 3, 4, 5}; Integer[] wrapper = (Integer[]) ObjectUtils.toObjectArray(a); assertTrue(wrapper.length == 5); for (int i = 0; i < wrapper.length; i++) { assertEquals(a[i], wrapper[i].intValue()); } }
@Test public void toObjectArrayWithEmptyPrimitiveArray() { Object[] objects = ObjectUtils.toObjectArray(new byte[] {}); assertNotNull(objects); assertEquals(0, objects.length); }
@Test public void toObjectArrayWithNull() { Object[] objects = ObjectUtils.toObjectArray(null); assertNotNull(objects); assertEquals(0, objects.length); }
@Override public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { // Create field error with the exceptions's code, e.g. "typeMismatch". String field = ex.getPropertyName(); Assert.state(field != null, "No field in exception"); String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); Object rejectedValue = ex.getValue(); if (ObjectUtils.isArray(rejectedValue)) { rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue)); } FieldError error = new FieldError(bindingResult.getObjectName(), field, rejectedValue, true, codes, arguments, ex.getLocalizedMessage()); error.wrap(ex); bindingResult.addError(error); }