/** * Register a custom {@link ConversionService}. * <p>By default a {@link StandardTypeConverter} backed by a * {@link org.springframework.core.convert.support.DefaultConversionService} is used. * @see #withTypeConverter * @see StandardTypeConverter#StandardTypeConverter(ConversionService) */ public Builder withConversionService(ConversionService conversionService) { this.typeConverter = new StandardTypeConverter(conversionService); return this; } /**
@Override public TypeConverter getTypeConverter() { if (this.typeConverter == null) { this.typeConverter = new StandardTypeConverter(); } return this.typeConverter; }
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers, @Nullable TypeConverter converter, @Nullable TypedValue rootObject) { this.propertyAccessors = accessors; this.methodResolvers = resolvers; this.typeConverter = (converter != null ? converter : new StandardTypeConverter()); this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL); }
@Override public TypeConverter getTypeConverter() { if (this.typeConverter == null) { this.typeConverter = new StandardTypeConverter(); } return this.typeConverter; }
/** * Register a custom {@link ConversionService}. * <p>By default a {@link StandardTypeConverter} backed by a * {@link org.springframework.core.convert.support.DefaultConversionService} is used. * @see #withTypeConverter * @see StandardTypeConverter#StandardTypeConverter(ConversionService) */ public Builder withConversionService(ConversionService conversionService) { this.typeConverter = new StandardTypeConverter(conversionService); return this; } /**
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers, @Nullable TypeConverter converter, @Nullable TypedValue rootObject) { this.propertyAccessors = accessors; this.methodResolvers = resolvers; this.typeConverter = (converter != null ? converter : new StandardTypeConverter()); this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL); }
@Test public void testReflectionHelperCompareArguments_NotAMatch() { StandardTypeConverter typeConverter = new StandardTypeConverter(); // Passing (Super,String) on call to foo(Sub,String) is not a match checkMatch(new Class<?>[] {Super.class,String.class}, new Class<?>[] {Sub.class,String.class}, typeConverter, null); }
@Test public void testStandardTypeConverter() throws EvaluationException { TypeConverter tc = new StandardTypeConverter(); tc.convertValue(3, TypeDescriptor.forObject(3), TypeDescriptor.valueOf(Double.class)); }
@Test public void testReflectionHelperCompareArguments_ExactMatching() { StandardTypeConverter tc = new StandardTypeConverter(); // Calling foo(String) with (String) is exact match checkMatch(new Class<?>[] {String.class}, new Class<?>[] {String.class}, tc, ReflectionHelper.ArgumentsMatchKind.EXACT); // Calling foo(String,Integer) with (String,Integer) is exact match checkMatch(new Class<?>[] {String.class, Integer.class}, new Class<?>[] {String.class, Integer.class}, tc, ArgumentsMatchKind.EXACT); }
ConversionService conversionService = evalContext.getBeanFactory().getConversionService(); if (conversionService != null) { sec.setTypeConverter(new StandardTypeConverter(conversionService));
@Test public void testReflectionHelperCompareArguments_CloseMatching() { StandardTypeConverter tc = new StandardTypeConverter(); // Calling foo(List) with (ArrayList) is close match (no conversion required) checkMatch(new Class<?>[] {ArrayList.class}, new Class<?>[] {List.class}, tc, ArgumentsMatchKind.CLOSE); // Passing (Sub,String) on call to foo(Super,String) is close match checkMatch(new Class<?>[] {Sub.class, String.class}, new Class<?>[] {Super.class, String.class}, tc, ArgumentsMatchKind.CLOSE); // Passing (String,Sub) on call to foo(String,Super) is close match checkMatch(new Class<?>[] {String.class, Sub.class}, new Class<?>[] {String.class, Super.class}, tc, ArgumentsMatchKind.CLOSE); }
@Test public void testReflectionHelperCompareArguments_RequiresConversionMatching() { StandardTypeConverter tc = new StandardTypeConverter(); // Calling foo(String,int) with (String,Integer) requires boxing conversion of argument one checkMatch(new Class<?>[] {String.class, Integer.TYPE}, new Class<?>[] {String.class,Integer.class},tc, ArgumentsMatchKind.CLOSE); // Passing (int,String) on call to foo(Integer,String) requires boxing conversion of argument zero checkMatch(new Class<?>[] {Integer.TYPE, String.class}, new Class<?>[] {Integer.class, String.class},tc, ArgumentsMatchKind.CLOSE); // Passing (int,Sub) on call to foo(Integer,Super) requires boxing conversion of argument zero checkMatch(new Class<?>[] {Integer.TYPE, Sub.class}, new Class<?>[] {Integer.class, Super.class}, tc, ArgumentsMatchKind.CLOSE); // Passing (int,Sub,boolean) on call to foo(Integer,Super,Boolean) requires boxing conversion of arguments zero and two // TODO checkMatch(new Class<?>[] {Integer.TYPE, Sub.class, Boolean.TYPE}, new Class<?>[] {Integer.class, Super.class, Boolean.class}, tc, ArgsMatchKind.REQUIRES_CONVERSION); }
ConversionService conversionService = evalContext.getBeanFactory().getConversionService(); if (conversionService != null) { sec.setTypeConverter(new StandardTypeConverter(conversionService));
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 testConvertArguments() throws Exception { StandardTypeConverter tc = new StandardTypeConverter(); Method oneArg = TestInterface.class.getMethod("oneArg", String.class); Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class); // basic conversion int>String Object[] args = new Object[] {3}; ReflectionHelper.convertArguments(tc, args, oneArg, null); checkArguments(args, "3"); // varargs but nothing to convert args = new Object[] {3}; ReflectionHelper.convertArguments(tc, args, twoArg, 1); checkArguments(args, "3"); // varargs with nothing needing conversion args = new Object[] {3, "abc", "abc"}; ReflectionHelper.convertArguments(tc, args, twoArg, 1); checkArguments(args, "3", "abc", "abc"); // varargs with conversion required args = new Object[] {3, false ,3.0d}; ReflectionHelper.convertArguments(tc, args, twoArg, 1); checkArguments(args, "3", "false", "3.0"); }
@Test public void testConvertArguments2() throws Exception { StandardTypeConverter tc = new StandardTypeConverter(); Method oneArg = TestInterface.class.getMethod("oneArg", String.class); Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class); // Simple conversion: int to string Object[] args = new Object[] {3}; ReflectionHelper.convertAllArguments(tc, args, oneArg); checkArguments(args, "3"); // varargs conversion args = new Object[] {3, false, 3.0f}; ReflectionHelper.convertAllArguments(tc, args, twoArg); checkArguments(args, "3", "false", "3.0"); // varargs conversion but no varargs args = new Object[] {3}; ReflectionHelper.convertAllArguments(tc, args, twoArg); checkArguments(args, "3"); // null value args = new Object[] {3, null, 3.0f}; ReflectionHelper.convertAllArguments(tc, args, twoArg); checkArguments(args, "3", null, "3.0"); }
@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); }
@Test public void testReflectionHelperCompareArguments_Varargs_ExactMatching() { StandardTypeConverter tc = new StandardTypeConverter();