/** * Obtain a {@link EncodableValue} given {@code value}. * * @param value the value to encode, may be {@literal null}. * @return the {@link EncodableValue} for {@code value}. */ @SuppressWarnings("unchecked") public static EncodableValue create(@Nullable Object value) { if (value instanceof byte[]) { return new BinaryValue((byte[]) value); } if (value instanceof UUID) { return new UuidValue((UUID) value); } if (value instanceof Collection) { Collection<?> collection = (Collection<?>) value; Class<?> commonElement = CollectionUtils.findCommonElementType(collection); if (commonElement != null) { if (UUID.class.isAssignableFrom(commonElement)) { return new UuidCollection((Collection<UUID>) value); } if (byte[].class.isAssignableFrom(commonElement)) { return new BinaryCollectionValue((Collection<byte[]>) value); } } } return new ObjectValue(value); }
protected void verifyResultCollectionConsistsOfMessages(Collection<?> elements) { Class<?> commonElementType = CollectionUtils.findCommonElementType(elements); Assert.isAssignable(Message.class, commonElementType, "The expected collection of Messages contains non-Message element: " + commonElementType); }
/** * Returns the type of the expression target based on the given root. * * @param root must not be {@literal null}. * @return */ public Class<?> getType(Object root) { Assert.notNull(root, "Root object must not be null!"); try { return expression.getValueType(CONTEXT, root); } catch (SpelEvaluationException o_O) { if (!SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS.equals(o_O.getMessageCode())) { throw o_O; } Object collectionOrArray = getParent().getValue(root); if (Collection.class.isInstance(collectionOrArray)) { return CollectionUtils.findCommonElementType(Collection.class.cast(collectionOrArray)); } } throw new IllegalArgumentException(String.format("Cannot obtain type for path %s on %s!", path, root)); }
/** * Obtain a {@link EncodableValue} given {@code value}. * * @param value the value to encode, may be {@literal null}. * @return the {@link EncodableValue} for {@code value}. */ @SuppressWarnings("unchecked") public static EncodableValue create(@Nullable Object value) { if (value instanceof byte[]) { return new BinaryValue((byte[]) value); } if (value instanceof UUID) { return new UuidValue((UUID) value); } if (value instanceof Collection) { Collection<?> collection = (Collection<?>) value; Class<?> commonElement = CollectionUtils.findCommonElementType(collection); if (commonElement != null) { if (UUID.class.isAssignableFrom(commonElement)) { return new UuidCollection((Collection<UUID>) value); } if (byte[].class.isAssignableFrom(commonElement)) { return new BinaryCollectionValue((Collection<byte[]>) value); } } } return new ObjectValue(value); }
protected void verifyResultCollectionConsistsOfMessages(Collection<?> elements) { Class<?> commonElementType = CollectionUtils.findCommonElementType(elements); Assert.isAssignable(Message.class, commonElementType, "The expected collection of Messages contains non-Message element: " + commonElementType); }
/** * Returns the type of the expression target based on the given root. * * @param root must not be {@literal null}. * @return */ public Class<?> getType(Object root) { Assert.notNull(root, "Root object must not be null!"); try { return expression.getValueType(CONTEXT, root); } catch (SpelEvaluationException o_O) { if (!SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS.equals(o_O.getMessageCode())) { throw o_O; } Object collectionOrArray = getParent().getValue(root); if (Collection.class.isInstance(collectionOrArray)) { return CollectionUtils.findCommonElementType(Collection.class.cast(collectionOrArray)); } } throw new IllegalArgumentException(String.format("Cannot obtain type for path %s on %s!", path, root)); }