Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2052 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
7 changed files with 151 additions and 6 deletions
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/* |
||||
* 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 org.springframework.beans.BeanUtils; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
/** |
||||
* Creates a mapping target by calling its default constructor. |
||||
* @author Keith Donald |
||||
* @see BeanUtils#instantiate(Class) |
||||
*/ |
||||
class DefaultMapperTargetFactory implements MapperTargetFactory { |
||||
|
||||
public Object createTarget(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
||||
return BeanUtils.instantiate(targetType.getType()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
/* |
||||
* 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 org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.core.convert.support.GenericConverter; |
||||
import org.springframework.mapping.Mapper; |
||||
|
||||
/** |
||||
* Adapts a Mapper to a Converter, allowing the conversion between two object types to be completed by a Mapper. |
||||
* Delegates to a {@link MapperTargetFactory} to construct the conversion target object that will be mapped. |
||||
* The default MapperTargetFactory instantiates a target by calling its default constructor. |
||||
* @author Keith Donald |
||||
*/ |
||||
public class MapperConverter implements GenericConverter { |
||||
|
||||
private Mapper mapper; |
||||
|
||||
private MapperTargetFactory mappingTargetFactory; |
||||
|
||||
public MapperConverter(Mapper mapper) { |
||||
this(mapper, new DefaultMapperTargetFactory()); |
||||
} |
||||
|
||||
public MapperConverter(Mapper mapper, MapperTargetFactory mappingTargetFactory) { |
||||
this.mapper = mapper; |
||||
this.mappingTargetFactory = mappingTargetFactory; |
||||
} |
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
||||
if (source == null) { |
||||
return null; |
||||
} |
||||
Object target = this.mappingTargetFactory.createTarget(source, sourceType, targetType); |
||||
return this.mapper.map(source, target); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* 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 org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.mapping.Mapper; |
||||
|
||||
/** |
||||
* A factory for customizing how the target of a map operation is constructed. |
||||
* Used by a {@link MapperConverter} when executing a type conversion. |
||||
* @author Keith Donald |
||||
* @see MapperConverter |
||||
* @see Mapper#map(Object, Object) |
||||
*/ |
||||
public interface MapperTargetFactory { |
||||
|
||||
/** |
||||
* Create the target object to be mapped to. |
||||
* @param source the source object to map from |
||||
* @param sourceType the source object type descriptor |
||||
* @param targetType the target object type descriptor |
||||
* @return the target |
||||
*/ |
||||
public Object createTarget(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); |
||||
} |
||||
Loading…
Reference in new issue