polish
This commit is contained in:
parent
a4476e5e0e
commit
a73324bc24
|
|
@ -24,6 +24,7 @@ import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.springframework.core.GenericTypeResolver;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||||
|
|
@ -115,6 +116,16 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||||
return mapping;
|
return mapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a Mapper to apply to complex nested property mappings of a specific sourceType/targetType pair.
|
||||||
|
* The source and target types are determined by introspecting the parameterized types on the implementation's Mapper generic interface.
|
||||||
|
* @param nestedMapper the nested mapper
|
||||||
|
*/
|
||||||
|
public void addNestedMapper(Mapper<?, ?> nestedMapper) {
|
||||||
|
Class[] typeInfo = getRequiredTypeInfo(nestedMapper);
|
||||||
|
addNestedMapper(typeInfo[0], typeInfo[1], nestedMapper);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a Mapper to apply to complex nested property mappings of a specific sourceType/targetType pair.
|
* Adds a Mapper to apply to complex nested property mappings of a specific sourceType/targetType pair.
|
||||||
* @param sourceType the source nested property type
|
* @param sourceType the source nested property type
|
||||||
|
|
@ -159,6 +170,10 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||||
|
|
||||||
// internal helpers
|
// internal helpers
|
||||||
|
|
||||||
|
private Class[] getRequiredTypeInfo(Mapper mapper) {
|
||||||
|
return GenericTypeResolver.resolveTypeArguments(mapper.getClass(), Mapper.class);
|
||||||
|
}
|
||||||
|
|
||||||
private EvaluationContext getEvaluationContext(Object object) {
|
private EvaluationContext getEvaluationContext(Object object) {
|
||||||
return mappableTypeFactory.getMappableType(object).getEvaluationContext(object, this.conversionService);
|
return mappableTypeFactory.getMappableType(object).getEvaluationContext(object, this.conversionService);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
import org.springframework.mapping.Mapper;
|
||||||
import org.springframework.mapping.MappingException;
|
import org.springframework.mapping.MappingException;
|
||||||
|
|
||||||
public class SpelMapperTests {
|
public class SpelMapperTests {
|
||||||
|
|
@ -147,7 +148,7 @@ public class SpelMapperTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mapBeanNestedCustomMapper() {
|
public void mapBeanNestedCustomNestedMapper() {
|
||||||
PersonDto source = new PersonDto();
|
PersonDto source = new PersonDto();
|
||||||
NestedDto nested = new NestedDto();
|
NestedDto nested = new NestedDto();
|
||||||
nested.foo = "bar";
|
nested.foo = "bar";
|
||||||
|
|
@ -171,6 +172,31 @@ public class SpelMapperTests {
|
||||||
assertEquals("bar and baz", target.nested.foo);
|
assertEquals("bar and baz", target.nested.foo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mapBeanNestedCustomNestedMapperHandCoded() {
|
||||||
|
PersonDto source = new PersonDto();
|
||||||
|
NestedDto nested = new NestedDto();
|
||||||
|
nested.foo = "bar";
|
||||||
|
source.setNested(nested);
|
||||||
|
|
||||||
|
Person target = new Person();
|
||||||
|
|
||||||
|
Mapper nestedMapper = new Mapper<NestedDto, Nested>() {
|
||||||
|
public Object map(NestedDto source, Nested target) {
|
||||||
|
target.foo = source.foo + " and baz";
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
mapper.addNestedMapper(nestedMapper);
|
||||||
|
|
||||||
|
mapper.setAutoMappingEnabled(false);
|
||||||
|
mapper.addMapping("nested");
|
||||||
|
mapper.map(source, target);
|
||||||
|
|
||||||
|
assertEquals("bar and baz", target.nested.foo);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mapList() {
|
public void mapList() {
|
||||||
PersonDto source = new PersonDto();
|
PersonDto source = new PersonDto();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue