spel mapping, polish
This commit is contained in:
@@ -44,6 +44,7 @@ public class MapperConverter implements GenericConverter {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
// TODO - could detect cyclical reference here if had a mapping context? (source should not equal currently mapped object)
|
||||
Object target = this.mappingTargetFactory.createTarget(source, sourceType, targetType);
|
||||
return this.mapper.map(source, target);
|
||||
}
|
||||
|
||||
@@ -22,11 +22,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.core.convert.support.GenericConverter;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
@@ -55,7 +53,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull
|
||||
| SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize);
|
||||
|
||||
private final Set<Mapping> mappings = new LinkedHashSet<Mapping>();
|
||||
private final Set<SpelMapping> mappings = new LinkedHashSet<SpelMapping>();
|
||||
|
||||
private boolean autoMappingEnabled = true;
|
||||
|
||||
@@ -65,10 +63,6 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
this.autoMappingEnabled = autoMappingEnabled;
|
||||
}
|
||||
|
||||
public void setConversionService(ConversionService conversionService) {
|
||||
this.conversionService.setParent(conversionService);
|
||||
}
|
||||
|
||||
public MappingConfiguration addMapping(String fieldExpression) {
|
||||
return addMapping(fieldExpression, fieldExpression);
|
||||
}
|
||||
@@ -92,7 +86,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
throw new IllegalArgumentException("The mapping target '" + targetFieldExpression
|
||||
+ "' is not a parseable property expression", e);
|
||||
}
|
||||
Mapping mapping = new Mapping(sourceExp, targetExp);
|
||||
SpelMapping mapping = new SpelMapping(sourceExp, targetExp);
|
||||
this.mappings.add(mapping);
|
||||
return mapping;
|
||||
}
|
||||
@@ -101,11 +95,11 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
EvaluationContext sourceContext = getMappingContext(source);
|
||||
EvaluationContext targetContext = getMappingContext(target);
|
||||
List<MappingFailure> failures = new LinkedList<MappingFailure>();
|
||||
for (Mapping mapping : this.mappings) {
|
||||
for (SpelMapping mapping : this.mappings) {
|
||||
mapping.map(sourceContext, targetContext, failures);
|
||||
}
|
||||
Set<Mapping> autoMappings = getAutoMappings(sourceContext, targetContext);
|
||||
for (Mapping mapping : autoMappings) {
|
||||
Set<SpelMapping> autoMappings = getAutoMappings(sourceContext, targetContext);
|
||||
for (SpelMapping mapping : autoMappings) {
|
||||
mapping.map(sourceContext, targetContext, failures);
|
||||
}
|
||||
if (!failures.isEmpty()) {
|
||||
@@ -118,9 +112,9 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
return mappableTypeFactory.getMappableType(object).getEvaluationContext(object, this.conversionService);
|
||||
}
|
||||
|
||||
private Set<Mapping> getAutoMappings(EvaluationContext sourceContext, EvaluationContext targetContext) {
|
||||
private Set<SpelMapping> getAutoMappings(EvaluationContext sourceContext, EvaluationContext targetContext) {
|
||||
if (this.autoMappingEnabled) {
|
||||
Set<Mapping> autoMappings = new LinkedHashSet<Mapping>();
|
||||
Set<SpelMapping> autoMappings = new LinkedHashSet<SpelMapping>();
|
||||
Set<String> sourceFields = getMappableFields(sourceContext.getRootObject().getValue());
|
||||
for (String field : sourceFields) {
|
||||
if (!explicitlyMapped(field)) {
|
||||
@@ -140,7 +134,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
}
|
||||
try {
|
||||
if (targetExpression.isWritable(targetContext)) {
|
||||
autoMappings.add(new Mapping(sourceExpression, targetExpression));
|
||||
autoMappings.add(new SpelMapping(sourceExpression, targetExpression));
|
||||
}
|
||||
} catch (EvaluationException e) {
|
||||
|
||||
@@ -158,7 +152,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
}
|
||||
|
||||
private boolean explicitlyMapped(String field) {
|
||||
for (Mapping mapping : this.mappings) {
|
||||
for (SpelMapping mapping : this.mappings) {
|
||||
if (mapping.getSourceExpressionString().startsWith(field)) {
|
||||
return true;
|
||||
}
|
||||
@@ -166,16 +160,11 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
return false;
|
||||
}
|
||||
|
||||
private class MappingConversionService extends GenericConversionService {
|
||||
|
||||
public MappingConversionService() {
|
||||
setParent(new DefaultConversionService());
|
||||
}
|
||||
private static class MappingConversionService extends DefaultConversionService {
|
||||
|
||||
@Override
|
||||
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
GenericConverter converter = super.getConverter(sourceType, targetType);
|
||||
return converter != null ? converter : new MapperConverter(new SpelMapper());
|
||||
protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return new MapperConverter(new SpelMapper());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.mapping.support;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
import org.springframework.core.convert.support.ConverterFactoryGenericConverter;
|
||||
import org.springframework.core.convert.support.ConverterGenericConverter;
|
||||
import org.springframework.core.convert.support.GenericConverter;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.mapping.MappingFailure;
|
||||
|
||||
/**
|
||||
* An individual mapping definition between two fields.
|
||||
* @author Keith Donald
|
||||
*/
|
||||
class SpelMapping implements MappingConfiguration {
|
||||
|
||||
private Expression source;
|
||||
|
||||
private Expression target;
|
||||
|
||||
private GenericConverter converter;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public MappingConfiguration setConverter(Converter<?, ?> converter) {
|
||||
return setGenericConverter(new ConverterGenericConverter(converter));
|
||||
}
|
||||
|
||||
public MappingConfiguration setConverterFactory(ConverterFactory<?, ?> converter) {
|
||||
return setGenericConverter(new ConverterFactoryGenericConverter(converter));
|
||||
}
|
||||
|
||||
public MappingConfiguration setGenericConverter(GenericConverter converter) {
|
||||
this.converter = converter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void map(EvaluationContext sourceContext, EvaluationContext targetContext,
|
||||
Collection<MappingFailure> failures) {
|
||||
try {
|
||||
Object value = this.source.getValue(sourceContext);
|
||||
if (this.converter != null) {
|
||||
value = this.converter.convert(value, this.source.getValueTypeDescriptor(sourceContext), this.target
|
||||
.getValueTypeDescriptor(targetContext));
|
||||
}
|
||||
this.target.setValue(targetContext, value);
|
||||
} catch (Exception e) {
|
||||
failures.add(new MappingFailure(e));
|
||||
}
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return getSourceExpressionString().hashCode() + getTargetExpressionString().hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof SpelMapping)) {
|
||||
return false;
|
||||
}
|
||||
SpelMapping m = (SpelMapping) o;
|
||||
return getSourceExpressionString().equals(m.getSourceExpressionString())
|
||||
&& getTargetExpressionString().equals(m.getTargetExpressionString());
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "[Mapping<" + getSourceExpressionString() + " -> " + getTargetExpressionString() + ">]";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user