@Test public void testSetParameterizedList() throws Exception { StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); Expression e = parser.parseExpression("listOfInteger.size()"); assertEquals(0,e.getValue(context,Integer.class).intValue()); context.setTypeConverter(new TypeConvertorUsingConversionService()); // Assign a List<String> to the List<Integer> field - the component elements should be converted parser.parseExpression("listOfInteger").setValue(context,listOfString); assertEquals(3,e.getValue(context,Integer.class).intValue()); // size now 3 Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer assertEquals(Integer.class,clazz); }
ConversionService conversionService = evalContext.getBeanFactory().getConversionService(); if (conversionService != null) { sec.setTypeConverter(new StandardTypeConverter(conversionService));
ConversionService conversionService = evalContext.getBeanFactory().getConversionService(); if (conversionService != null) { sec.setTypeConverter(new StandardTypeConverter(conversionService));
@Ignore @Test public void repro() throws Exception { AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy(); BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter(); StandardEvaluationContext context = new StandardEvaluationContext(); context.setTypeConverter(converter); List<Foo> arguments = new ArrayList<>(); // !!!! With the below line commented you'll get NPE. Uncomment and everything is OK! //arguments.add(new Foo()); List<TypeDescriptor> paramDescriptors = new ArrayList<>(); Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class); paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, 0))); List<TypeDescriptor> argumentTypes = new ArrayList<>(); argumentTypes.add(TypeDescriptor.forObject(arguments)); ReflectiveMethodResolver resolver = new ReflectiveMethodResolver(); MethodExecutor executor = resolver.resolve(context, target, "checkCompleteness", argumentTypes); Object result = executor.execute(context, target, arguments); System.out.println("Result: " + result); }
private EvaluationContext createEvaluationContext(PageContext pageContext) { StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new JspPropertyAccessor(pageContext)); context.addPropertyAccessor(new MapAccessor()); context.addPropertyAccessor(new EnvironmentAccessor()); context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext())); ConversionService conversionService = getConversionService(pageContext); if (conversionService != null) { context.setTypeConverter(new StandardTypeConverter(conversionService)); } return context; }
@Test public void test_binaryPlusWithTimeConverted() { SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH); GenericConversionService conversionService = new GenericConversionService(); conversionService.addConverter(Time.class, String.class, format::format); StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext(); evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService)); ExpressionState expressionState = new ExpressionState(evaluationContextConverter); Time time = new Time(new Date().getTime()); VariableReference var = new VariableReference("timeVar", -1, -1); var.setValue(expressionState, time); StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\""); OpPlus o = new OpPlus(-1, -1, var, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals(format.format(time) + " is now", value.getValue()); }
private EvaluationContext createEvaluationContext(PageContext pageContext) { StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new JspPropertyAccessor(pageContext)); context.addPropertyAccessor(new MapAccessor()); context.addPropertyAccessor(new EnvironmentAccessor()); context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext())); ConversionService conversionService = getConversionService(pageContext); if (conversionService != null) { context.setTypeConverter(new StandardTypeConverter(conversionService)); } return context; }
@Test public void testConvertAndHandleNull() { // SPR-9445 // without null conversion evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean"); evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean"); evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean"); evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean"); // with null conversion (null -> false) GenericConversionService conversionService = new GenericConversionService() { @Override protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) { return targetType.getType() == Boolean.class ? false : null; } }; context.setTypeConverter(new StandardTypeConverter(conversionService)); evaluate("null or true", Boolean.TRUE, Boolean.class, false); evaluate("null and true", Boolean.FALSE, Boolean.class, false); evaluate("!null", Boolean.TRUE, Boolean.class, false); evaluate("null ? 'foo' : 'bar'", "bar", String.class, false); }
/** * Set a bean resolver for runtime SpEL expressions. Also configures the evaluation * context with a standard type converter and map accessor. * @param beanResolver the resolver. * @since 2.0 */ public void setBeanResolver(BeanResolver beanResolver) { this.evaluationContext.setBeanResolver(beanResolver); this.evaluationContext.setTypeConverter(new StandardTypeConverter()); this.evaluationContext.addPropertyAccessor(new MapAccessor()); }
@Override public StandardEvaluationContext getObject() throws Exception { StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); if (this.typeLocator != null) { evaluationContext.setTypeLocator(this.typeLocator); } evaluationContext.setBeanResolver(this.beanResolver); evaluationContext.setTypeConverter(getTypeConverter()); for (PropertyAccessor propertyAccessor : getPropertyAccessors().values()) { evaluationContext.addPropertyAccessor(propertyAccessor); } evaluationContext.addPropertyAccessor(new MapAccessor()); for (Entry<String, Method> functionEntry : getFunctions().entrySet()) { evaluationContext.registerFunction(functionEntry.getKey(), functionEntry.getValue()); } return evaluationContext; }
/** * Emits a WARN log if the beanFactory field is null, unless the argument is false. * @param beanFactoryRequired set to false to suppress the warning. * @return The evaluation context. */ protected final StandardEvaluationContext getEvaluationContext(boolean beanFactoryRequired) { if (this.evaluationContext == null) { if (this.beanFactory == null && !beanFactoryRequired) { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(); } else { this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory); } this.evaluationContext.setTypeConverter(this.typeConverter); if (this.beanFactory != null) { ConversionService conversionService = IntegrationUtils.getConversionService(this.beanFactory); if (conversionService != null) { this.typeConverter.setConversionService(conversionService); } } } return this.evaluationContext; }
evaluationContext.addPropertyAccessor(new MapAccessor()); if (conversionService != null) { evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService));
/** * Creates an evaluation context to use in the spring class descriptor. Adds the default converters from the default * conversion service. * * @param object to supply to the evaluation context * * @return evalutation context */ protected EvaluationContext createEvaluationContext(final Object object) { final StandardEvaluationContext context = new StandardEvaluationContext(object); context.setTypeConverter(typeConverter); return context; }
private void configureTypeConverter(EvaluationContext evaluationContext, BeanFactory beanFactory) { Optional.ofNullable(evaluationContext) .filter(evalContext -> evalContext instanceof StandardEvaluationContext) .ifPresent(evalContext -> Optional.ofNullable(beanFactory) .filter(it -> it instanceof ConfigurableBeanFactory) .map(it -> ((ConfigurableBeanFactory) it).getConversionService()) .ifPresent(conversionService -> ((StandardEvaluationContext) evalContext).setTypeConverter( new StandardTypeConverter(conversionService))) ); }
private void configureTypeConverter(EvaluationContext evaluationContext, BeanFactory beanFactory) { Optional.ofNullable(evaluationContext) .filter(evalContext -> evalContext instanceof StandardEvaluationContext) .ifPresent(evalContext -> Optional.ofNullable(beanFactory) .filter(it -> it instanceof ConfigurableBeanFactory) .map(it -> ((ConfigurableBeanFactory) it).getConversionService()) .ifPresent(conversionService -> ((StandardEvaluationContext) evalContext).setTypeConverter( new StandardTypeConverter(conversionService))) ); }
@Override public EvaluationContext createContext(Object rootObject) { StandardEvaluationContext context = new StandardEvaluationContext(rootObject); context.setVariables(getVariableValues(rootObject)); context.setTypeConverter(new StandardTypeConverter(conversionService)); context.getPropertyAccessors().addAll(propertyAccessors); return context; }
@Override public EvaluationContext createContext(Object rootObject) { StandardEvaluationContext context = new StandardEvaluationContext(rootObject); context.setVariables(getVariableValues(rootObject)); context.setTypeConverter(new StandardTypeConverter(conversionService)); context.getPropertyAccessors().addAll(propertyAccessors); return context; }
/** * Set a bean resolver for runtime SpEL expressions. Also configures the evaluation * context with a standard type converter and map accessor. * @param beanResolver the resolver. * @since 1.6 */ public void setBeanResolver(BeanResolver beanResolver) { this.evalContext.setBeanResolver(beanResolver); this.evalContext.setTypeConverter(new StandardTypeConverter()); this.evalContext.addPropertyAccessor(new MapAccessor()); }
/** * Set a bean resolver for runtime SpEL expressions. Also configures the evaluation * context with a standard type converter and map accessor. * @param beanResolver the resolver. * @since 1.6 */ public void setBeanResolver(BeanResolver beanResolver) { this.evalContext.setBeanResolver(beanResolver); this.evalContext.setTypeConverter(new StandardTypeConverter()); this.evalContext.addPropertyAccessor(new MapAccessor()); }
private EvaluationContext createEvaluationContext(PageContext pageContext) { StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new JspPropertyAccessor(pageContext)); context.addPropertyAccessor(new MapAccessor()); context.addPropertyAccessor(new EnvironmentAccessor()); context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext())); ConversionService conversionService = getConversionService(pageContext); if (conversionService != null) { context.setTypeConverter(new StandardTypeConverter(conversionService)); } return context; }