Skip to content

Commit

Permalink
DATACMNS-1282 - Switched to SimpleEvaluationContext in MapDataBinder.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Mar 28, 2018
1 parent 4715675 commit b1a20ae
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/main/java/org/springframework/data/web/MapDataBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,12 @@
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.TypeLocator;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;

Expand Down Expand Up @@ -108,13 +104,6 @@ private static class MapPropertyAccessor extends AbstractPropertyAccessor {

private static final SpelExpressionParser PARSER = new SpelExpressionParser(
new SpelParserConfiguration(false, true));
private static final TypeLocator REJECTING_LOCATOR = new TypeLocator() {

@Override
public Class<?> findType(String typeName) throws EvaluationException {
throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typeName);
}
};

private final @NonNull Class<?> type;
private final @NonNull Map<String, Object> map;
Expand Down Expand Up @@ -172,14 +161,6 @@ public void setPropertyValue(String propertyName, Object value) throws BeansExce
throw new NotWritablePropertyException(type, propertyName);
}

StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new PropertyTraversingMapAccessor(type, conversionService));
context.setTypeConverter(new StandardTypeConverter(conversionService));
context.setTypeLocator(REJECTING_LOCATOR);
context.setRootObject(map);

Expression expression = PARSER.parseExpression(propertyName);

PropertyPath leafProperty = getPropertyPath(propertyName).getLeafProperty();
TypeInformation<?> owningType = leafProperty.getOwningType();
TypeInformation<?> propertyType = owningType.getProperty(leafProperty.getSegment());
Expand All @@ -196,6 +177,15 @@ public void setPropertyValue(String propertyName, Object value) throws BeansExce
value = conversionService.convert(value, TypeDescriptor.forObject(value), typeDescriptor);
}

EvaluationContext context = SimpleEvaluationContext //

.forPropertyAccessors(new PropertyTraversingMapAccessor(type, conversionService)) //
.withConversionService(conversionService) //
.withRootObject(map) //
.build();

Expression expression = PARSER.parseExpression(propertyName);

try {
expression.setValue(context, value);
} catch (SpelEvaluationException o_O) {
Expand Down

0 comments on commit b1a20ae

Please # to comment.