Browse Source

mapping exclusions

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2073 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Keith Donald 17 years ago
parent
commit
cabf63b8b1
  1. 5
      org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConfiguration.java
  2. 4
      org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapper.java
  3. 27
      org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapping.java
  4. 18
      org.springframework.context/src/test/java/org/springframework/mapping/support/SpelMapperTests.java

5
org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConfiguration.java

@ -50,4 +50,9 @@ public interface MappingConfiguration { @@ -50,4 +50,9 @@ public interface MappingConfiguration {
*/
MappingConfiguration setGenericConverter(GenericConverter converter);
/**
* Configures that this mapping should be excluded (ignored and not executed).
*/
void setExclude();
}

4
org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapper.java

@ -122,7 +122,7 @@ public class SpelMapper implements Mapper<Object, Object> { @@ -122,7 +122,7 @@ public class SpelMapper implements Mapper<Object, Object> {
* @param nestedMapper the nested mapper
*/
public void addNestedMapper(Mapper<?, ?> nestedMapper) {
Class[] typeInfo = getRequiredTypeInfo(nestedMapper);
Class<?>[] typeInfo = getRequiredTypeInfo(nestedMapper);
addNestedMapper(typeInfo[0], typeInfo[1], nestedMapper);
}
@ -132,7 +132,7 @@ public class SpelMapper implements Mapper<Object, Object> { @@ -132,7 +132,7 @@ public class SpelMapper implements Mapper<Object, Object> {
* @param targetType the target nested property type
* @param nestedMapper the nested mapper
*/
public void addNestedMapper(Class sourceType, Class targetType, Mapper<?, ?> nestedMapper) {
public void addNestedMapper(Class<?> sourceType, Class<?> targetType, Mapper<?, ?> nestedMapper) {
this.conversionService.addGenericConverter(sourceType, targetType, new MapperConverter(nestedMapper));
}

27
org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapping.java

@ -38,18 +38,14 @@ class SpelMapping implements MappingConfiguration { @@ -38,18 +38,14 @@ class SpelMapping implements MappingConfiguration {
private GenericConverter converter;
private boolean exclude;
public SpelMapping(Expression source, Expression target) {
this.source = source;
this.target = target;
}
public String getSourceExpressionString() {
return this.source.getExpressionString();
}
public String getTargetExpressionString() {
return this.target.getExpressionString();
}
// implementing MappingConfiguration
public MappingConfiguration setConverter(Converter<?, ?> converter) {
return setGenericConverter(new ConverterGenericConverter(converter));
@ -64,8 +60,25 @@ class SpelMapping implements MappingConfiguration { @@ -64,8 +60,25 @@ class SpelMapping implements MappingConfiguration {
return this;
}
public void setExclude() {
this.exclude = true;
}
// public methods
public String getSourceExpressionString() {
return this.source.getExpressionString();
}
public String getTargetExpressionString() {
return this.target.getExpressionString();
}
public void map(EvaluationContext sourceContext, EvaluationContext targetContext,
Collection<MappingFailure> failures) {
if (exclude) {
return;
}
try {
Object value = this.source.getValue(sourceContext);
if (this.converter != null) {

18
org.springframework.context/src/test/java/org/springframework/mapping/support/SpelMapperTests.java

@ -81,6 +81,23 @@ public class SpelMapperTests { @@ -81,6 +81,23 @@ public class SpelMapperTests {
assertEquals(31, target.age);
}
@Test
public void mapAutomaticWithExclusions() {
Map<String, Object> source = new HashMap<String, Object>();
source.put("name", "Keith");
source.put("test", "3");
source.put("favoriteSport", "FOOTBALL");
Person target = new Person();
mapper.addMapping("test").setExclude();
mapper.map(source, target);
assertEquals("Keith", target.name);
assertEquals(0, target.age);
assertEquals(Sport.FOOTBALL, target.favoriteSport);
}
@Test
public void mapSameSourceFieldToMultipleTargets() {
Map<String, Object> source = new HashMap<String, Object>();
@ -270,7 +287,6 @@ public class SpelMapperTests { @@ -270,7 +287,6 @@ public class SpelMapperTests {
return names[0] + " P. " + names[1];
}
});
mapper.map(source, target);
assertEquals("Keith P. Donald", target.name);

Loading…
Cancel
Save