mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#19 Adding template for Parameter, simplifying PropertyMapping by passing source and target bean via template
This commit is contained in:
parent
a931cff0e5
commit
9e5fc2af8e
@ -25,7 +25,6 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.model.source.Parameter;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
|
||||
/**
|
||||
|
@ -16,16 +16,18 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.model.source;
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import org.mapstruct.ap.model.Type;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.util.Collections;
|
||||
|
||||
/**
|
||||
* A parameter of a mapping method.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class Parameter {
|
||||
public class Parameter extends AbstractModelElement {
|
||||
|
||||
private final String name;
|
||||
private final Type type;
|
||||
@ -57,4 +59,9 @@ public class Parameter {
|
||||
public String toString() {
|
||||
return ( mappingTarget ? "@MappingTarget " : "" ) + type.toString() + " " + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return Collections.asSet( type );
|
||||
}
|
||||
}
|
@ -31,9 +31,6 @@ import java.util.Set;
|
||||
*/
|
||||
public class PropertyMapping extends AbstractModelElement {
|
||||
|
||||
private final String sourceBeanName;
|
||||
private final String targetBeanName;
|
||||
|
||||
private final String sourceName;
|
||||
private final String sourceAccessorName;
|
||||
private final Type sourceType;
|
||||
@ -45,11 +42,9 @@ public class PropertyMapping extends AbstractModelElement {
|
||||
private final MappingMethodReference mappingMethod;
|
||||
private final TypeConversion conversion;
|
||||
|
||||
public PropertyMapping(String sourceBeanName, String targetBeanName, String sourceName, String sourceAccessorName,
|
||||
Type sourceType, String targetName, String targetAccessorName, Type targetType,
|
||||
MappingMethodReference mappingMethod, TypeConversion conversion) {
|
||||
this.sourceBeanName = sourceBeanName;
|
||||
this.targetBeanName = targetBeanName;
|
||||
public PropertyMapping(String sourceName, String sourceAccessorName, Type sourceType, String targetName,
|
||||
String targetAccessorName, Type targetType, MappingMethodReference mappingMethod,
|
||||
TypeConversion conversion) {
|
||||
|
||||
this.sourceName = sourceName;
|
||||
this.sourceAccessorName = sourceAccessorName;
|
||||
@ -63,14 +58,6 @@ public class PropertyMapping extends AbstractModelElement {
|
||||
this.conversion = conversion;
|
||||
}
|
||||
|
||||
public String getSourceBeanName() {
|
||||
return sourceBeanName;
|
||||
}
|
||||
|
||||
public String getTargetBeanName() {
|
||||
return targetBeanName;
|
||||
}
|
||||
|
||||
public String getSourceName() {
|
||||
return sourceName;
|
||||
}
|
||||
|
@ -18,13 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model.source;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
|
||||
import org.mapstruct.ap.model.Parameter;
|
||||
import org.mapstruct.ap.model.Type;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
|
||||
@ -145,11 +145,6 @@ public class Method {
|
||||
return parameterNames;
|
||||
}
|
||||
|
||||
public String getResultName() {
|
||||
return targetParameter != null ? targetParameter.getName() :
|
||||
Strings.getSaveVariableName( Introspector.decapitalize( getResultType().getName() ), getParameterNames() );
|
||||
}
|
||||
|
||||
public Type getResultType() {
|
||||
return targetParameter != null ? targetParameter.getType() : returnType;
|
||||
}
|
||||
|
@ -48,13 +48,13 @@ import org.mapstruct.ap.model.MapperReference;
|
||||
import org.mapstruct.ap.model.MappingMethod;
|
||||
import org.mapstruct.ap.model.MappingMethodReference;
|
||||
import org.mapstruct.ap.model.Options;
|
||||
import org.mapstruct.ap.model.Parameter;
|
||||
import org.mapstruct.ap.model.PropertyMapping;
|
||||
import org.mapstruct.ap.model.ReportingPolicy;
|
||||
import org.mapstruct.ap.model.Type;
|
||||
import org.mapstruct.ap.model.TypeConversion;
|
||||
import org.mapstruct.ap.model.source.Mapping;
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.model.source.Parameter;
|
||||
import org.mapstruct.ap.util.Executables;
|
||||
import org.mapstruct.ap.util.Filters;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
@ -334,8 +334,6 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
|
||||
PropertyMapping property = new PropertyMapping(
|
||||
method.getSingleSourceParameter().getName(),
|
||||
method.getResultName(),
|
||||
executables.getPropertyName( getterMethod ),
|
||||
getterMethod.getSimpleName().toString(),
|
||||
sourceType,
|
||||
|
@ -35,12 +35,12 @@ import org.mapstruct.ap.MapMappingPrism;
|
||||
import org.mapstruct.ap.MapperPrism;
|
||||
import org.mapstruct.ap.MappingPrism;
|
||||
import org.mapstruct.ap.MappingsPrism;
|
||||
import org.mapstruct.ap.model.Parameter;
|
||||
import org.mapstruct.ap.model.Type;
|
||||
import org.mapstruct.ap.model.source.IterableMapping;
|
||||
import org.mapstruct.ap.model.source.MapMapping;
|
||||
import org.mapstruct.ap.model.source.Mapping;
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.model.source.Parameter;
|
||||
import org.mapstruct.ap.util.Executables;
|
||||
import org.mapstruct.ap.util.TypeUtil;
|
||||
|
||||
|
@ -29,8 +29,8 @@ import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
|
||||
import org.mapstruct.ap.MappingTargetPrism;
|
||||
import org.mapstruct.ap.model.Parameter;
|
||||
import org.mapstruct.ap.model.Type;
|
||||
import org.mapstruct.ap.model.source.Parameter;
|
||||
|
||||
/**
|
||||
* Provides functionality around {@link ExecutableElement}s.
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
-->
|
||||
@Override
|
||||
public ${returnType.name} ${name}(<#list parameters as param>${param.type.name} ${param.name}<#if param_has_next>, </#if></#list>) {
|
||||
public ${returnType.name} ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) {
|
||||
if ( ${singleSourceParameter.name} == null ) {
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
}
|
||||
@ -29,7 +29,7 @@
|
||||
</#if>
|
||||
|
||||
<#list propertyMappings as propertyMapping>
|
||||
<@includeModel object=propertyMapping/>
|
||||
<@includeModel object=propertyMapping sourceBeanName=singleSourceParameter.name targetBeanName=resultName/>
|
||||
</#list>
|
||||
<#if returnType.name != "void">
|
||||
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
-->
|
||||
@Override
|
||||
public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param.type/> ${param.name}<#if param_has_next>, </#if></#list>) {
|
||||
public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) {
|
||||
if ( ${singleSourceParameter.name} == null ) {
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
}
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
-->
|
||||
@Override
|
||||
public <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param.type/> ${param.name}<#if param_has_next>, </#if></#list>) {
|
||||
public <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) {
|
||||
if ( ${singleSourceParameter.name} == null ) {
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
<#--
|
||||
|
||||
Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
and/or other contributors as indicated by the @authors tag. See the
|
||||
copyright.txt file in the distribution for a full listing of all
|
||||
contributors.
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
${type} ${name}
|
@ -20,24 +20,24 @@
|
||||
-->
|
||||
<#-- a) invoke mapping method -->
|
||||
<#if mappingMethod??>
|
||||
${targetBeanName}.${targetAccessorName}( <@includeModel object=mappingMethod input="${sourceBeanName}.${sourceAccessorName}()"/> );
|
||||
${ext.targetBeanName}.${targetAccessorName}( <@includeModel object=mappingMethod input="${ext.sourceBeanName}.${sourceAccessorName}()"/> );
|
||||
<#-- b) simple conversion -->
|
||||
<#elseif conversion??>
|
||||
<#if sourceType.primitive == false>
|
||||
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||
<@applyConversion targetBeanName=targetBeanName targetAccessorName=targetAccessorName conversion=conversion/>
|
||||
if ( ${ext.sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||
<@applyConversion targetBeanName=ext.targetBeanName targetAccessorName=targetAccessorName conversion=conversion/>
|
||||
}
|
||||
<#else>
|
||||
<@applyConversion targetBeanName=targetBeanName targetAccessorName=targetAccessorName conversion=conversion/>
|
||||
<@applyConversion targetBeanName=ext.targetBeanName targetAccessorName=targetAccessorName conversion=conversion/>
|
||||
</#if>
|
||||
<#-- c) simply set -->
|
||||
<#else>
|
||||
<#if targetType.collectionType == true>
|
||||
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||
${targetBeanName}.${targetAccessorName}( new <#if targetType.collectionImplementationType??>${targetType.collectionImplementationType.name}<#else>${targetType.name}</#if><#if targetType.elementType??><${targetType.elementType.name}></#if>( ${sourceBeanName}.${sourceAccessorName}() ) );
|
||||
if ( ${ext.sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||
${ext.targetBeanName}.${targetAccessorName}( new <#if targetType.collectionImplementationType??>${targetType.collectionImplementationType.name}<#else>${targetType.name}</#if><#if targetType.elementType??><${targetType.elementType.name}></#if>( ${ext.sourceBeanName}.${sourceAccessorName}() ) );
|
||||
}
|
||||
<#else>
|
||||
${targetBeanName}.${targetAccessorName}( ${sourceBeanName}.${sourceAccessorName}() );
|
||||
${ext.targetBeanName}.${targetAccessorName}( ${ext.sourceBeanName}.${sourceAccessorName}() );
|
||||
</#if>
|
||||
</#if>
|
||||
<#macro applyConversion targetBeanName targetAccessorName conversion>
|
||||
|
Loading…
x
Reference in New Issue
Block a user