Refine search
Assert.isTrue(expectedArgTypes.get(expectedArgTypes.size() - 1).isArray(), "Final expected argument should be array type (the varargs parameter)"); TypeDescriptor expectedArg = expectedArgTypes.get(i); if (suppliedArg == null) { if (expectedArg.isPrimitive()) { match = null; if (!expectedArg.equals(suppliedArg)) { if (suppliedArg.isAssignableTo(expectedArg)) { if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) { else if (typeConverter.canConvert(suppliedArg, expectedArg)) { match = ArgumentsMatchKind.REQUIRES_CONVERSION; else if (typeConverter.canConvert(suppliedArg, TypeDescriptor.valueOf(varargsParamType))) { match = ArgumentsMatchKind.REQUIRES_CONVERSION;
@SuppressWarnings("unchecked") private <T> T convertValue(TypeConverter converter, @Nullable Object value, Class<T> targetType) { T result = (T) converter.convertValue( value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(targetType)); if (result == null) { throw new IllegalStateException("Null conversion result for index [" + value + "]"); } return result; }
/** * 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()); }
@Override protected <T> T convertOther( final IExpressionContext context, final Object object, final Class<T> targetClass) { if (object == null) { return null; } final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object); final TypeDescriptor targetTypeDescriptor = TypeDescriptor.valueOf(targetClass); final TypeConverter typeConverter = getSpringConversionService(context); if (typeConverter == null || !typeConverter.canConvert(objectTypeDescriptor, targetTypeDescriptor)) { return super.convertOther(context, object, targetClass); } return (T) typeConverter.convertValue(object, objectTypeDescriptor, targetTypeDescriptor); }
@SuppressWarnings("unchecked") private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Class<T> targetType) { Object result = typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType)); if (result == null) { throw new IllegalStateException("Null conversion result for value [" + typedValue.getValue() + "]"); } return (T) result; }
if (varargsPosition == null) { for (int i = 0; i < arguments.length; i++) { TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forExecutable(executable, i)); Object argument = arguments[i]; arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); conversionOccurred |= (argument != arguments[i]); TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forExecutable(executable, i)); Object argument = arguments[i]; arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); conversionOccurred |= (argument != arguments[i]); Object argument = arguments[varargsPosition]; TypeDescriptor sourceType = TypeDescriptor.forObject(argument); arguments[varargsPosition] = converter.convertValue(argument, sourceType, targetType); for (int i = varargsPosition; i < arguments.length; i++) { Object argument = arguments[i]; arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); conversionOccurred |= (argument != arguments[i]);
private boolean canReturnExpectedType(AnnotatedMethodFilter filter, Class<?> targetType, TypeConverter typeConverter) { if (this.expectedType == null) { return true; } List<Method> methods = filter.filter(Arrays.asList(ReflectionUtils.getAllDeclaredMethods(targetType))); for (Method candidate : methods) { if (typeConverter.canConvert(TypeDescriptor.valueOf(candidate.getReturnType()), this.expectedType)) { return true; } } return false; }
@Test public void testCoercionToCollectionOfPrimitive() throws Exception { class TestTarget { @SuppressWarnings("unused") public int sum(Collection<Integer> numbers) { int total = 0; for (int i : numbers) { total += i; } return total; } } StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); TypeDescriptor collectionType = new TypeDescriptor(new MethodParameter(TestTarget.class.getDeclaredMethod( "sum", Collection.class), 0)); // The type conversion is possible assertTrue(evaluationContext.getTypeConverter() .canConvert(TypeDescriptor.valueOf(String.class), collectionType)); // ... and it can be done successfully assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString()); evaluationContext.setVariable("target", new TestTarget()); // OK up to here, so the evaluation should be fine... // ... but this fails int result = (Integer) parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4"); assertEquals("Wrong result: " + result, 10, result); }
public Object convertValue(Object value, TypeDescriptor targetTypeDescriptor) throws EvaluationException { Object result = this.relatedContext.getTypeConverter().convertValue( value, TypeDescriptor.forObject(value), targetTypeDescriptor); if (result == null) { throw new IllegalStateException("Null conversion result for value [" + value + "]"); } return result; }
@Override public void setValue(@Nullable Object newValue) { growCollectionIfNecessary(); if (this.collection instanceof List) { List list = (List) this.collection; if (this.collectionEntryDescriptor.getElementTypeDescriptor() != null) { newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue), this.collectionEntryDescriptor.getElementTypeDescriptor()); } list.set(this.index, newValue); } else { throw new SpelEvaluationException(getStartPosition(), SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, this.collectionEntryDescriptor.toString()); } }
@Override protected String convertToString( final IExpressionContext context, final Object object) { if (object == null) { return null; } final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object); final TypeConverter typeConverter = getSpringConversionService(context); if (typeConverter == null || !typeConverter.canConvert(objectTypeDescriptor, TYPE_STRING)) { return super.convertToString(context, object); } return (String) typeConverter.convertValue(object, objectTypeDescriptor, TYPE_STRING); }
@Override public void setValue(@Nullable Object newValue) { if (this.mapEntryDescriptor.getMapValueTypeDescriptor() != null) { newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue), this.mapEntryDescriptor.getMapValueTypeDescriptor()); } this.map.put(this.key, newValue); }
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;
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { return targetType.isAssignableTo(BOOLEAN_TYPE) && ( sourceType.isAssignableTo(NUMBER_TYPE) || sourceType.isCollection() || sourceType.isArray() ) || decorated.canConvert(sourceType, targetType); }
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) { if (targetType.isAssignableTo(BOOLEAN_TYPE)) { if (value instanceof Number) { return ((Number) value).intValue() != 0; } if (sourceType.isCollection()) { return ! ((Collection) value).isEmpty(); } if (sourceType.isArray()) { return ((Object[]) value).length != 0; } return value; } else { return decorated.convertValue(value, sourceType, targetType); } } }
@Override protected <T> T convertOther( final IExpressionContext context, final Object object, final Class<T> targetClass) { if (object == null) { return null; } final TypeDescriptor objectTypeDescriptor = TypeDescriptor.forObject(object); final TypeDescriptor targetTypeDescriptor = TypeDescriptor.valueOf(targetClass); final TypeConverter typeConverter = getSpringConversionService(context); if (typeConverter == null || !typeConverter.canConvert(objectTypeDescriptor, targetTypeDescriptor)) { return super.convertOther(context, object, targetClass); } return (T) typeConverter.convertValue(object, objectTypeDescriptor, targetTypeDescriptor); }
/** * Determines if there is a type converter available in the specified context and * attempts to use it to convert the supplied value to the specified type. Throws an * exception if conversion is not possible. * @param context the evaluation context that may define a type converter * @param typedValue the value to convert and a type descriptor describing it * @param targetType the type to attempt conversion to * @return the converted value * @throws EvaluationException if there is a problem during conversion or conversion * of the value to the specified type is not supported */ @SuppressWarnings("unchecked") @Nullable public static <T> T convertTypedValue( @Nullable EvaluationContext context, TypedValue typedValue, @Nullable Class<T> targetType) { Object value = typedValue.getValue(); if (targetType == null) { return (T) value; } if (context != null) { return (T) context.getTypeConverter().convertValue( value, typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType)); } if (ClassUtils.isAssignableValue(targetType, value)) { return (T) value; } throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'"); }
if (varargsPosition == null) { for (int i = 0; i < arguments.length; i++) { TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forExecutable(executable, i)); Object argument = arguments[i]; arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); conversionOccurred |= (argument != arguments[i]); TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forExecutable(executable, i)); Object argument = arguments[i]; arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); conversionOccurred |= (argument != arguments[i]); Object argument = arguments[varargsPosition]; TypeDescriptor sourceType = TypeDescriptor.forObject(argument); arguments[varargsPosition] = converter.convertValue(argument, sourceType, targetType); for (int i = varargsPosition; i < arguments.length; i++) { Object argument = arguments[i]; arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); conversionOccurred |= (argument != arguments[i]);
private void prepareEvaluationContext() throws Exception { StandardEvaluationContext context = getEvaluationContext(false); Class<?> targetType = AopUtils.getTargetClass(this.targetObject); if (this.method != null) { context.registerMethodFilter(targetType, new FixedMethodFilter(this.method)); if (this.expectedType != null) { Assert.state(context.getTypeConverter() .canConvert(TypeDescriptor.valueOf((this.method).getReturnType()), this.expectedType), () -> "Cannot convert to expected type (" + this.expectedType + ") from " + this.method); } } else { AnnotatedMethodFilter filter = new AnnotatedMethodFilter(this.annotationType, this.methodName, this.requiresReply); Assert.state(canReturnExpectedType(filter, targetType, context.getTypeConverter()), () -> "Cannot convert to expected type (" + this.expectedType + ") from " + this.method); context.registerMethodFilter(targetType, filter); } context.setVariable("target", this.targetObject); context.registerFunction("requiredHeader", ParametersWrapper.class.getDeclaredMethod("getHeader", Map.class, String.class)); }
@Nullable public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException { Object val = value.getValue(); return this.relatedContext.getTypeConverter().convertValue( val, TypeDescriptor.forObject(val), targetTypeDescriptor); }