Java Code Examples for org.springframework.util.ObjectUtils#isArray()

The following examples show how to use org.springframework.util.ObjectUtils#isArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultBindingErrorProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: DefaultBindingErrorProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: FixedAuthoritiesExtractor.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
private String asAuthorities(Object object) {
	List<Object> authorities = new ArrayList<>();
	if (object instanceof Collection) {
		Collection<?> collection = (Collection<?>) object;
		object = collection.toArray(new Object[0]);
	}
	if (ObjectUtils.isArray(object)) {
		Object[] array = (Object[]) object;
		for (Object value : array) {
			if (value instanceof String) {
				authorities.add(value);
			}
			else if (value instanceof Map) {
				authorities.add(asAuthority((Map<?, ?>) value));
			}
			else {
				authorities.add(value);
			}
		}
		return StringUtils.collectionToCommaDelimitedString(authorities);
	}
	return object.toString();
}
 
Example 4
Source File: ParameterMetadataProvider.java    From spring-data-ebean with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the given argument as {@link Collection} which means it will return it as is if it's a
 * {@link Collections}, turn an array into an {@link ArrayList} or simply wrap any other value into a single element
 * {@link Collections}.
 *
 * @param value
 * @return
 */
public static Collection<?> toCollection(Object value) {
    if (value == null) {
        return null;
    }

    if (value instanceof Collection) {
        return (Collection<?>) value;
    }

    if (ObjectUtils.isArray(value)) {
        return Arrays.asList(ObjectUtils.toObjectArray(value));
    }

    return Collections.singleton(value);
}
 
Example 5
Source File: StringQuery.java    From spring-data-ebean with Apache License 2.0 6 votes vote down vote up
@Override
public Object prepare(Object value) {

    if (!ObjectUtils.isArray(value)) {
        return value;
    }

    int length = Array.getLength(value);
    Collection<Object> result = new ArrayList<Object>(length);

    for (int i = 0; i < length; i++) {
        result.add(Array.get(value, i));
    }

    return result;
}
 
Example 6
Source File: FixedAuthoritiesExtractor.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
private String asAuthorities(Object object) {
    List<Object> authorities = new ArrayList<Object>();
    if (object instanceof Collection) {
        Collection<?> collection = (Collection<?>) object;
        object = collection.toArray(new Object[0]);
    }
    if (ObjectUtils.isArray(object)) {
        Object[] array = (Object[]) object;
        for (Object value : array) {
            if (value instanceof String) {
                authorities.add(value);
            } else if (value instanceof Map) {
                authorities.add(asAuthority((Map<?, ?>) value));
            } else {
                authorities.add(value);
            }
        }
        return StringUtils.collectionToCommaDelimitedString(authorities);
    }
    return object.toString();
}
 
Example 7
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether we have a custom conversion registered for the given value into an
 * arbitrary simple Vault type. Returns the converted value if so. If not, we perform
 * special enum handling or simply return the value as is.
 * @param value the value to write.
 * @return the converted value. Can be {@literal null}.
 */
@Nullable
private Object getPotentiallyConvertedSimpleWrite(@Nullable Object value) {

	if (value == null) {
		return null;
	}

	Optional<Class<?>> customTarget = this.conversions.getCustomWriteTarget(value.getClass());

	if (customTarget.isPresent()) {
		return this.conversionService.convert(value, customTarget.get());
	}

	if (ObjectUtils.isArray(value)) {

		if (value instanceof byte[]) {
			return value;
		}
		return asCollection(value);
	}

	return Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value;
}
 
Example 8
Source File: StringQuery.java    From spring-data-mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public Object prepare(@Nullable Object value) {

	if (!ObjectUtils.isArray(value)) {
		return value;
	}

	int length = Array.getLength(value);
	Collection<Object> result = new ArrayList<>(length);

	for (int i = 0; i < length; i++) {
		result.add(Array.get(value, i));
	}

	return result;
}
 
Example 9
Source File: HierarchicalUriComponents.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 10
Source File: StringQuery.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public Object prepare(@Nullable Object value) {
    if (!ObjectUtils.isArray(value))
        return value;

    int length = Array.getLength(value);
    Collection<Object> result = new ArrayList<>(length);

    for (int i = 0; i < length; i++)
        result.add(Array.get(value, i));

    return result;
}
 
Example 11
Source File: StringQuery.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public Object prepare(@Nullable Object value) {
    if (!ObjectUtils.isArray(value))
        return value;

    int length = Array.getLength(value);
    Collection<Object> result = new ArrayList<>(length);

    for (int i = 0; i < length; i++)
        result.add(Array.get(value, i));

    return result;
}
 
Example 12
Source File: KotlinLambdaToFunctionAutoConfiguration.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public Object apply(Object input) {
	if (ObjectUtils.isEmpty(input)) {
		return this.invoke();
	}
	else if (ObjectUtils.isArray(input)) {
		return null;
	}
	else {
		return this.invoke(input);
	}
}
 
Example 13
Source File: FieldValueCounterSinkConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
protected void processValue(String counterName, Object value) {
	if ((value instanceof Collection) || ObjectUtils.isArray(value)) {
		Collection<?> c = (value instanceof Collection) ? (Collection<?>) value
				: Arrays.asList(ObjectUtils.toObjectArray(value));
		for (Object val : c) {
			fieldValueCounterWriter.increment(counterName, val.toString(), 1.0);
		}
	}
	else {
		fieldValueCounterWriter.increment(counterName, value.toString(), 1.0);
	}
}
 
Example 14
Source File: HierarchicalUriComponents.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getValue(String name) {
	Object value = this.delegate.getValue(name);
	if (ObjectUtils.isArray(value)) {
		value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value));
	}
	return value;
}
 
Example 15
Source File: HierarchicalUriComponents.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 16
Source File: SimpleFunctionRegistry.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private Object convertOutputValueIfNecessary(Object value, Function<Message, Message> enricher, String... acceptedOutputMimeTypes) {
	logger.debug("Applying type conversion on output value");
	Object convertedValue = null;
	if (FunctionTypeUtils.isMultipleArgumentsHolder(value)) {
		int outputCount = FunctionTypeUtils.getOutputCount(this.functionType);
		Object[] convertedInputArray = new Object[outputCount];
		for (int i = 0; i < outputCount; i++) {
			Expression parsed = new SpelExpressionParser().parseExpression("getT" + (i + 1) + "()");
			Object outputArgument = parsed.getValue(value);
			try {
				convertedInputArray[i] = outputArgument instanceof Publisher
					? this
					.convertOutputPublisherIfNecessary((Publisher<?>) outputArgument, enricher, acceptedOutputMimeTypes[i])
					: this.convertOutputValueIfNecessary(outputArgument, enricher, acceptedOutputMimeTypes[i]);
			}
			catch (ArrayIndexOutOfBoundsException e) {
				throw new IllegalStateException("The number of 'acceptedOutputMimeTypes' for function '" + this.functionDefinition
					+ "' is (" + acceptedOutputMimeTypes.length
					+ "), which does not match the number of actual outputs of this function which is (" + outputCount + ").", e);
			}

		}
		convertedValue = Tuples.fromArray(convertedInputArray);
	}
	else {
		List<MimeType> acceptedContentTypes = MimeTypeUtils
			.parseMimeTypes(acceptedOutputMimeTypes[0].toString());
		if (CollectionUtils.isEmpty(acceptedContentTypes)) {
			convertedValue = value;
		}
		else {
			for (int i = 0; i < acceptedContentTypes.size() && convertedValue == null; i++) {
				MimeType acceptedContentType = acceptedContentTypes.get(i);
				/*
				 * We need to treat Iterables differently since they may represent collection of Messages
				 * which should be converted individually
				 */
				boolean convertIndividualItem = false;
				if (value instanceof Iterable || (ObjectUtils.isArray(value) && !(value instanceof byte[]))) {
					Type outputType = FunctionTypeUtils.getOutputType(functionType, 0);
					if (outputType instanceof ParameterizedType) {
						convertIndividualItem = FunctionTypeUtils.isMessage(FunctionTypeUtils.getImmediateGenericType(outputType, 0));
					}
					else if (outputType instanceof GenericArrayType) {
						convertIndividualItem = FunctionTypeUtils.isMessage(((GenericArrayType) outputType).getGenericComponentType());
					}
				}

				if (convertIndividualItem) {
					if (ObjectUtils.isArray(value)) {
						value = Arrays.asList((Object[]) value);
					}
					AtomicReference<List<Message>> messages = new AtomicReference<List<Message>>(new ArrayList<>());
					((Iterable) value).forEach(element ->
						messages.get()
							.add((Message) convertOutputValueIfNecessary(element, enricher, acceptedContentType
								.toString())));
					convertedValue = messages.get();
				}
				else {
					convertedValue = this.convertValueToMessage(value, enricher, acceptedContentType);
				}
			}
		}
	}

	if (convertedValue == null) {
		throw new MessageConversionException(COULD_NOT_CONVERT_OUTPUT);
	}
	return convertedValue;
}
 
Example 17
Source File: AbstractDynamoDBQueryCreator.java    From spring-data-dynamodb with Apache License 2.0 4 votes vote down vote up
protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part, Iterator<Object> iterator) {
	if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS))
		throw new UnsupportedOperationException("Case insensitivity not supported");

	Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType();
	
	PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
	String leafNodePropertyName = leafNodePropertyPath.toDotPath();
	if (leafNodePropertyName.indexOf(".") != -1)
	{
		int index = leafNodePropertyName.lastIndexOf(".");
		leafNodePropertyName = leafNodePropertyName.substring(index);
	}

	switch (part.getType()) {
	
	case IN:
		Object in = iterator.next();
		Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '"
				+ leafNodePropertyName + "'");
		boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class);
		boolean isArray = ObjectUtils.isArray(in);
		Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters");
		Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in));
		return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType);
	case CONTAINING:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS,
				iterator.next(), leafNodePropertyType);
	case STARTING_WITH:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH,
				iterator.next(), leafNodePropertyType);
	case BETWEEN:
		Object first = iterator.next();
		Object second = iterator.next();
		return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType);
	case AFTER:
	case GREATER_THAN:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(),
				leafNodePropertyType);
	case BEFORE:
	case LESS_THAN:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(),
				leafNodePropertyType);
	case GREATER_THAN_EQUAL:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(),
				leafNodePropertyType);
	case LESS_THAN_EQUAL:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(),
				leafNodePropertyType);
	case IS_NULL:
		return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL);
	case IS_NOT_NULL:
		return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL);
	case TRUE:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE,
				leafNodePropertyType);
	case FALSE:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE,
				leafNodePropertyType);
	case SIMPLE_PROPERTY:
		return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType);
	case NEGATING_SIMPLE_PROPERTY:
		return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(),
				leafNodePropertyType);
	default:
		throw new IllegalArgumentException("Unsupported keyword " + part.getType());
	}

}