@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); }
.getTypeConverter() .convertValue(value, TypeDescriptor.forObject(value),
@Override @SuppressWarnings("unchecked") public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Object payload = message.getPayload(); if (Properties.class.isAssignableFrom(parameter.getParameterType())) { Map<String, Object> map = message.getHeaders(); if (!parameter.hasParameterAnnotation(Headers.class)) { if (payload instanceof Map) { map = (Map<String, Object>) payload; } else if (payload instanceof String && ((String) payload).contains("=")) { return getEvaluationContext() .getTypeConverter() .convertValue(payload, TypeDescriptor.valueOf(String.class), PROPERTIES_TYPE); } } Properties properties = new Properties(); properties.putAll(map); return properties; } else { if (!parameter.hasParameterAnnotation(Headers.class) && payload instanceof Map) { return payload; } else { return message.getHeaders(); } } }
.getTypeConverter() .convertValue(payloads, TypeDescriptor.forObject(payloads),
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)); }
@SuppressWarnings("unchecked") private T processInternal(ParametersWrapper parameters) throws Exception { if (!this.initialized) { initialize(); } HandlerMethod candidate = this.findHandlerMethodForParameters(parameters); if (candidate == null) { candidate = this.defaultHandlerMethod; } Assert.notNull(candidate, "No candidate methods found for messages."); if (!candidate.initialized) { initializeHandler(candidate); } Expression expression = candidate.expression; T result; if (this.useSpelInvoker || candidate.spelOnly) { result = invokeExpression(expression, parameters); } else { result = invokeHandlerMethod(candidate, parameters); } if (result != null && this.expectedType != null) { return (T) getEvaluationContext(true) .getTypeConverter() .convertValue(result, TypeDescriptor.forObject(result), this.expectedType); } else { return result; } }
@Test public void testEvaluationContext() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, new RootBeanDefinition(IntegrationEvaluationContextFactoryBean.class)); context.registerBeanDefinition(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, new RootBeanDefinition(ConversionServiceFactoryBean.class)); context.refresh(); StandardEvaluationContext evalContext = ExpressionUtils.createStandardEvaluationContext(context); assertNotNull(evalContext.getBeanResolver()); assertNotNull(evalContext.getTypeConverter()); IntegrationEvaluationContextFactoryBean factory = context.getBean("&" + IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, IntegrationEvaluationContextFactoryBean.class); assertSame(evalContext.getTypeConverter(), TestUtils.getPropertyValue(factory, "typeConverter")); }
@Test public void testEvaluationContextNoBeanFactory() { StandardEvaluationContext evalContext = ExpressionUtils.createStandardEvaluationContext(); assertNull(evalContext.getBeanResolver()); TypeConverter typeConverter = evalContext.getTypeConverter(); assertNotNull(typeConverter); assertSame(DefaultConversionService.getSharedInstance(), TestUtils.getPropertyValue(typeConverter, "conversionService")); } }
@Test public void testEvaluationContextDefaultTypeConverter() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, new RootBeanDefinition(IntegrationEvaluationContextFactoryBean.class)); context.refresh(); StandardEvaluationContext evalContext = ExpressionUtils.createStandardEvaluationContext(context); assertNotNull(evalContext.getBeanResolver()); TypeConverter typeConverter = evalContext.getTypeConverter(); assertNotNull(typeConverter); assertSame(DefaultConversionService.getSharedInstance(), TestUtils.getPropertyValue(typeConverter, "conversionService")); }
@Test public void testEvaluationContextNoFactoryBean() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, new RootBeanDefinition(ConversionServiceFactoryBean.class)); context.refresh(); StandardEvaluationContext evalContext = ExpressionUtils.createStandardEvaluationContext(context); assertNotNull(evalContext.getBeanResolver()); TypeConverter typeConverter = evalContext.getTypeConverter(); assertNotNull(typeConverter); assertNotSame(DefaultConversionService.getSharedInstance(), TestUtils.getPropertyValue(typeConverter, "conversionService")); assertSame(context.getBean(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME), TestUtils.getPropertyValue(typeConverter, "conversionService")); }
.getTypeConverter() .convertValue(value, TypeDescriptor.forObject(value),
@Override @SuppressWarnings("unchecked") public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { Object payload = message.getPayload(); if (Properties.class.isAssignableFrom(parameter.getParameterType())) { Map<String, Object> map = message.getHeaders(); if (!parameter.hasParameterAnnotation(Headers.class)) { if (payload instanceof Map) { map = (Map<String, Object>) payload; } else if (payload instanceof String && ((String) payload).contains("=")) { return getEvaluationContext() .getTypeConverter() .convertValue(payload, TypeDescriptor.valueOf(String.class), PROPERTIES_TYPE); } } Properties properties = new Properties(); properties.putAll(map); return properties; } else { if (!parameter.hasParameterAnnotation(Headers.class) && payload instanceof Map) { return payload; } else { return message.getHeaders(); } } }
.getTypeConverter() .convertValue(payloads, TypeDescriptor.forObject(payloads),
private void prepareEvaluationContext(StandardEvaluationContext context, Object method, Class<? extends Annotation> annotationType) { Class<?> targetType = AopUtils.getTargetClass(this.targetObject); if (method instanceof Method) { context.registerMethodFilter(targetType, new FixedMethodFilter((Method) method)); if (expectedType != null) { Assert.state( context.getTypeConverter().canConvert( TypeDescriptor.valueOf(((Method) method).getReturnType()), TypeDescriptor.valueOf(expectedType)), "Cannot convert to expected type (" + expectedType + ") from " + method); } } else if (method == null || method instanceof String) { AnnotatedMethodFilter filter = new AnnotatedMethodFilter(annotationType, (String) method, this.requiresReply); Assert.state(canReturnExpectedType(filter, targetType, context.getTypeConverter()), "Cannot convert to expected type (" + expectedType + ") from " + method); context.registerMethodFilter(targetType, filter); } context.setVariable("target", targetObject); }
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)); }
private void prepareEvaluationContext(StandardEvaluationContext context, Object method, Class<? extends Annotation> annotationType) { Class<?> targetType = AopUtils.getTargetClass(this.targetObject); if (method instanceof Method) { context.registerMethodFilter(targetType, new FixedMethodFilter((Method) method)); if (expectedType != null) { Assert.state( context.getTypeConverter().canConvert( TypeDescriptor.valueOf(((Method) method).getReturnType()), TypeDescriptor.valueOf(expectedType)), "Cannot convert to expected type (" + expectedType + ") from " + method); } } else if (method == null || method instanceof String) { AnnotatedMethodFilter filter = new AnnotatedMethodFilter(annotationType, (String) method, this.requiresReply); Assert.state(canReturnExpectedType(filter, targetType, context.getTypeConverter()), "Cannot convert to expected type (" + expectedType + ") from " + method); context.registerMethodFilter(targetType, filter); } context.setVariable("target", targetObject); }
@SuppressWarnings("unchecked") private T processInternal(ParametersWrapper parameters) throws Exception { if (!this.initialized) { initialize(); } HandlerMethod candidate = this.findHandlerMethodForParameters(parameters); if (candidate == null) { candidate = this.defaultHandlerMethod; } Assert.notNull(candidate, "No candidate methods found for messages."); if (!candidate.initialized) { initializeHandler(candidate); } Expression expression = candidate.expression; T result; if (this.useSpelInvoker || candidate.spelOnly) { result = invokeExpression(expression, parameters); } else { result = invokeHandlerMethod(candidate, parameters); } if (result != null && this.expectedType != null) { return (T) getEvaluationContext(true) .getTypeConverter() .convertValue(result, TypeDescriptor.forObject(result), this.expectedType); } else { return result; } }
@PostConstruct public void init() { if (evalContext == null) { evalContext = new StandardEvaluationContext(); } if (evalContext instanceof StandardEvaluationContext) { StandardEvaluationContext standardEvalContext = (StandardEvaluationContext)evalContext; // PropertyAccessor used when the model is a BeanFactory. standardEvalContext.addPropertyAccessor(new BeanFactoryAccessor()); if (beanFactory != null) { if (standardEvalContext.getBeanResolver() == null) { standardEvalContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } if (standardEvalContext.getTypeLocator() == null) { standardEvalContext.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader())); } if (standardEvalContext.getTypeConverter() == null) { ConversionService conversionService = beanFactory.getConversionService(); if (conversionService != null) { standardEvalContext.setTypeConverter(new StandardTypeConverter(conversionService)); } } } } }