Java Code Examples for org.springframework.expression.TypeConverter#canConvert()

The following examples show how to use org.springframework.expression.TypeConverter#canConvert() . 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: OpPlus.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Convert operand value to string using registered converter or using
 * {@code toString} method.
 * @param value typed value to be converted
 * @param state expression state
 * @return {@code TypedValue} instance converted to {@code String}
 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
	TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
	TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
	if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
		return String.valueOf(typeConverter.convertValue(value.getValue(),
				value.getTypeDescriptor(), typeDescriptor));
	}
	return String.valueOf(value.getValue());
}
 
Example 2
Source File: ReflectionHelper.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
@Nullable
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		// The user may supply null - and that will be ok unless a primitive is expected
		if (suppliedArg == null) {
			if (expectedArg.isPrimitive()) {
				match = null;
			}
		}
		else if (!expectedArg.equals(suppliedArg))  {
			if (suppliedArg.isAssignableTo(expectedArg)) {
				if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
					match = ArgumentsMatchKind.CLOSE;
				}
			}
			else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
				match = ArgumentsMatchKind.REQUIRES_CONVERSION;
			}
			else {
				match = null;
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 3
Source File: OpPlus.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Convert operand value to string using registered converter or using
 * {@code toString} method.
 * @param value typed value to be converted
 * @param state expression state
 * @return {@code TypedValue} instance converted to {@code String}
 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
	TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
	TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
	if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
		return String.valueOf(typeConverter.convertValue(value.getValue(),
				value.getTypeDescriptor(), typeDescriptor));
	}
	return String.valueOf(value.getValue());
}
 
Example 4
Source File: ReflectionHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
@Nullable
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		// The user may supply null - and that will be ok unless a primitive is expected
		if (suppliedArg == null) {
			if (expectedArg.isPrimitive()) {
				match = null;
			}
		}
		else if (!expectedArg.equals(suppliedArg))  {
			if (suppliedArg.isAssignableTo(expectedArg)) {
				if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
					match = ArgumentsMatchKind.CLOSE;
				}
			}
			else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
				match = ArgumentsMatchKind.REQUIRES_CONVERSION;
			}
			else {
				match = null;
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 5
Source File: OpPlus.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert operand value to string using registered converter or using
 * {@code toString} method.
 * @param value typed value to be converted
 * @param state expression state
 * @return {@code TypedValue} instance converted to {@code String}
 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
	TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
	TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
	if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
		return String.valueOf(typeConverter.convertValue(value.getValue(),
				value.getTypeDescriptor(), typeDescriptor));
	}
	return String.valueOf(value.getValue());
}
 
Example 6
Source File: ReflectionHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 7
Source File: OpPlus.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Convert operand value to string using registered converter or using
 * {@code toString} method.
 * @param value typed value to be converted
 * @param state expression state
 * @return {@code TypedValue} instance converted to {@code String}
 */
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
	TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
	TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
	if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
		return String.valueOf(typeConverter.convertValue(value.getValue(),
				value.getTypeDescriptor(), typeDescriptor));
	}
	return String.valueOf(value.getValue());
}
 
Example 8
Source File: ReflectionHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}