mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#160 Using update method, basic behavior. Single mapping methods on non collection / array properties.
This commit is contained in:
parent
776bc3d157
commit
c69a56c138
@ -62,4 +62,10 @@ public class Direct extends ModelElement implements Assignment {
|
|||||||
public AssignmentType getType() {
|
public AssignmentType getType() {
|
||||||
return AssignmentType.DIRECT;
|
return AssignmentType.DIRECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUpdateMethod() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,8 @@ public class IterableMappingMethod extends MappingMethod {
|
|||||||
dateFormat,
|
dateFormat,
|
||||||
qualifiers,
|
qualifiers,
|
||||||
qualifyingElementTargetType,
|
qualifyingElementTargetType,
|
||||||
loopVariableName
|
loopVariableName,
|
||||||
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( assignment == null ) {
|
if ( assignment == null ) {
|
||||||
|
@ -125,7 +125,8 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
keyDateFormat,
|
keyDateFormat,
|
||||||
keyQualifiers,
|
keyQualifiers,
|
||||||
keyQualifyingTargetType,
|
keyQualifyingTargetType,
|
||||||
"entry.getKey()"
|
"entry.getKey()",
|
||||||
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( keyAssignment == null ) {
|
if ( keyAssignment == null ) {
|
||||||
@ -145,7 +146,8 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
valueDateFormat,
|
valueDateFormat,
|
||||||
valueQualifiers,
|
valueQualifiers,
|
||||||
valueQualifyingTargetType,
|
valueQualifyingTargetType,
|
||||||
"entry.getValue()"
|
"entry.getValue()",
|
||||||
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( valueAssignment == null ) {
|
if ( valueAssignment == null ) {
|
||||||
|
@ -83,6 +83,7 @@ public class MappingBuilderContext {
|
|||||||
* @param resultType used for further select the appropriate mapping method based on resultType (bean mapping)
|
* @param resultType used for further select the appropriate mapping method based on resultType (bean mapping)
|
||||||
* targetType (Iterable- and MapMapping)
|
* targetType (Iterable- and MapMapping)
|
||||||
* @param sourceReference call to source type as string
|
* @param sourceReference call to source type as string
|
||||||
|
* @param preferUpdateMethods selection should prefer update methods when present.
|
||||||
*
|
*
|
||||||
* @return an assignment to a method parameter, which can either be:
|
* @return an assignment to a method parameter, which can either be:
|
||||||
* <ol>
|
* <ol>
|
||||||
@ -94,7 +95,7 @@ public class MappingBuilderContext {
|
|||||||
*/
|
*/
|
||||||
Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType,
|
Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType,
|
||||||
String targetPropertyName, String dateFormat, List<TypeMirror> qualifiers,
|
String targetPropertyName, String dateFormat, List<TypeMirror> qualifiers,
|
||||||
TypeMirror resultType, String sourceReference);
|
TypeMirror resultType, String sourceReference, boolean preferUpdateMethods);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a no arg factory method
|
* returns a no arg factory method
|
||||||
|
@ -42,6 +42,7 @@ public class MethodReference extends MappingMethod implements Assignment {
|
|||||||
private final MapperReference declaringMapper;
|
private final MapperReference declaringMapper;
|
||||||
private final Set<Type> importTypes;
|
private final Set<Type> importTypes;
|
||||||
private final List<Type> exceptionTypes;
|
private final List<Type> exceptionTypes;
|
||||||
|
private final boolean isUpdateMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In case this reference targets a built-in method, allows to pass specific context information to the invoked
|
* In case this reference targets a built-in method, allows to pass specific context information to the invoked
|
||||||
@ -78,6 +79,7 @@ public class MethodReference extends MappingMethod implements Assignment {
|
|||||||
}
|
}
|
||||||
this.importTypes = Collections.<Type>unmodifiableSet( imported );
|
this.importTypes = Collections.<Type>unmodifiableSet( imported );
|
||||||
this.exceptionTypes = method.getThrownTypes();
|
this.exceptionTypes = method.getThrownTypes();
|
||||||
|
this.isUpdateMethod = method.getMappingTargetParameter() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodReference(BuiltInMethod method, ConversionContext contextParam) {
|
public MethodReference(BuiltInMethod method, ConversionContext contextParam) {
|
||||||
@ -86,6 +88,7 @@ public class MethodReference extends MappingMethod implements Assignment {
|
|||||||
this.contextParam = method.getContextParameter( contextParam );
|
this.contextParam = method.getContextParameter( contextParam );
|
||||||
this.importTypes = Collections.emptySet();
|
this.importTypes = Collections.emptySet();
|
||||||
this.exceptionTypes = Collections.emptyList();
|
this.exceptionTypes = Collections.emptyList();
|
||||||
|
this.isUpdateMethod = method.getMappingTargetParameter() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapperReference getDeclaringMapper() {
|
public MapperReference getDeclaringMapper() {
|
||||||
@ -159,4 +162,9 @@ public class MethodReference extends MappingMethod implements Assignment {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUpdateMethod() {
|
||||||
|
return isUpdateMethod;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ import org.mapstruct.ap.model.assignment.NewCollectionOrMapWrapper;
|
|||||||
import org.mapstruct.ap.model.assignment.NullCheckWrapper;
|
import org.mapstruct.ap.model.assignment.NullCheckWrapper;
|
||||||
import org.mapstruct.ap.model.assignment.SetterWrapper;
|
import org.mapstruct.ap.model.assignment.SetterWrapper;
|
||||||
import org.mapstruct.ap.model.assignment.SetterWrapperForCollectionsAndMaps;
|
import org.mapstruct.ap.model.assignment.SetterWrapperForCollectionsAndMaps;
|
||||||
|
import org.mapstruct.ap.model.assignment.UpdateWrapper;
|
||||||
import org.mapstruct.ap.model.common.ModelElement;
|
import org.mapstruct.ap.model.common.ModelElement;
|
||||||
import org.mapstruct.ap.model.common.Parameter;
|
import org.mapstruct.ap.model.common.Parameter;
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
@ -163,6 +164,16 @@ public class PropertyMapping extends ModelElement {
|
|||||||
sourceRefStr = getSourceRef();
|
sourceRefStr = getSourceRef();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// all the tricky cases will be excluded for the time being.
|
||||||
|
boolean preferUpdateMethods;
|
||||||
|
if ( targetType.isCollectionOrMapType() || targetType.isArrayType() ||
|
||||||
|
targetAccessorType == TargetWriteAccessorType.ADDER ) {
|
||||||
|
preferUpdateMethods = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
preferUpdateMethods = method.getMappingTargetParameter() != null;
|
||||||
|
}
|
||||||
|
|
||||||
Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
|
Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
|
||||||
method,
|
method,
|
||||||
sourceElement,
|
sourceElement,
|
||||||
@ -172,7 +183,8 @@ public class PropertyMapping extends ModelElement {
|
|||||||
dateFormat,
|
dateFormat,
|
||||||
qualifiers,
|
qualifiers,
|
||||||
resultType,
|
resultType,
|
||||||
sourceRefStr
|
sourceRefStr,
|
||||||
|
preferUpdateMethods
|
||||||
);
|
);
|
||||||
|
|
||||||
// No mapping found. Try to forge a mapping
|
// No mapping found. Try to forge a mapping
|
||||||
@ -198,6 +210,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
else {
|
else {
|
||||||
assignment = assignObject( sourceType, targetType, targetAccessorType, assignment );
|
assignment = assignObject( sourceType, targetType, targetAccessorType, assignment );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ctx.getMessager().printMessage(
|
ctx.getMessager().printMessage(
|
||||||
@ -228,8 +241,19 @@ public class PropertyMapping extends ModelElement {
|
|||||||
Assignment result = rhs;
|
Assignment result = rhs;
|
||||||
|
|
||||||
if ( targetAccessorType == TargetWriteAccessorType.SETTER ) {
|
if ( targetAccessorType == TargetWriteAccessorType.SETTER ) {
|
||||||
|
if ( result.isUpdateMethod() ) {
|
||||||
|
if ( targetReadAccessor == null ) {
|
||||||
|
ctx.getMessager().printMessage( method.getExecutable(),
|
||||||
|
Message.PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE,
|
||||||
|
targetPropertyName );
|
||||||
|
}
|
||||||
|
Assignment factoryMethod =
|
||||||
|
ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null );
|
||||||
|
result = new UpdateWrapper( result, method.getThrownTypes(), targetType, factoryMethod );
|
||||||
|
}
|
||||||
|
else {
|
||||||
result = new SetterWrapper( result, method.getThrownTypes() );
|
result = new SetterWrapper( result, method.getThrownTypes() );
|
||||||
|
}
|
||||||
if ( !sourceType.isPrimitive()
|
if ( !sourceType.isPrimitive()
|
||||||
&& !sourceReference.getPropertyEntries().isEmpty() /* parameter null taken care of by beanmapper */
|
&& !sourceReference.getPropertyEntries().isEmpty() /* parameter null taken care of by beanmapper */
|
||||||
&& ( result.getType() == TYPE_CONVERTED
|
&& ( result.getType() == TYPE_CONVERTED
|
||||||
@ -575,17 +599,33 @@ public class PropertyMapping extends ModelElement {
|
|||||||
dateFormat,
|
dateFormat,
|
||||||
qualifiers,
|
qualifiers,
|
||||||
resultType,
|
resultType,
|
||||||
constantExpression
|
constantExpression,
|
||||||
|
method.getMappingTargetParameter() != null
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( assignment != null ) {
|
if ( assignment != null ) {
|
||||||
|
|
||||||
if ( Executables.isSetterMethod( targetWriteAccessor ) ) {
|
if ( Executables.isSetterMethod( targetWriteAccessor ) ) {
|
||||||
|
|
||||||
// target accessor is setter, so decorate assignment as setter
|
// target accessor is setter, so decorate assignment as setter
|
||||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
if ( assignment.isUpdateMethod() ) {
|
||||||
|
if ( targetReadAccessor == null ) {
|
||||||
|
ctx.getMessager().printMessage( method.getExecutable(),
|
||||||
|
Message.CONSTANTMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE,
|
||||||
|
targetPropertyName );
|
||||||
|
}
|
||||||
|
Assignment factoryMethod =
|
||||||
|
ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null );
|
||||||
|
assignment =
|
||||||
|
new UpdateWrapper( assignment, method.getThrownTypes(), targetType, factoryMethod );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// wrap when dealing with getter only on target
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
// target accessor is getter, so getter map/ collection handling
|
||||||
assignment = new GetterWrapperForCollectionsAndMaps(
|
assignment = new GetterWrapperForCollectionsAndMaps(
|
||||||
assignment,
|
assignment,
|
||||||
method.getThrownTypes(),
|
method.getThrownTypes(),
|
||||||
|
@ -103,4 +103,10 @@ public class TypeConversion extends ModelElement implements Assignment {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUpdateMethod() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -83,4 +83,6 @@ public interface Assignment {
|
|||||||
*/
|
*/
|
||||||
AssignmentType getType();
|
AssignmentType getType();
|
||||||
|
|
||||||
|
|
||||||
|
boolean isUpdateMethod();
|
||||||
}
|
}
|
||||||
|
@ -64,4 +64,9 @@ public abstract class AssignmentWrapper extends ModelElement implements Assignme
|
|||||||
public AssignmentType getType() {
|
public AssignmentType getType() {
|
||||||
return decoratedAssignment.getType();
|
return decoratedAssignment.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUpdateMethod() {
|
||||||
|
return decoratedAssignment.isUpdateMethod();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.model.assignment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.mapstruct.ap.model.common.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps the assignment in a target setter.
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class UpdateWrapper extends AssignmentWrapper {
|
||||||
|
|
||||||
|
private final List<Type> exceptionTypesToExclude;
|
||||||
|
private final Type targetType;
|
||||||
|
private final Assignment factoryMethod;
|
||||||
|
|
||||||
|
public UpdateWrapper(Assignment decoratedAssignment, List<Type> exceptionTypesToExclude, Type targetType,
|
||||||
|
Assignment factoryMethod ) {
|
||||||
|
super( decoratedAssignment );
|
||||||
|
this.exceptionTypesToExclude = exceptionTypesToExclude;
|
||||||
|
this.targetType = targetType;
|
||||||
|
this.factoryMethod = factoryMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Type> getExceptionTypes() {
|
||||||
|
List<Type> parentExceptionTypes = super.getExceptionTypes();
|
||||||
|
List<Type> result = new ArrayList<Type>( parentExceptionTypes );
|
||||||
|
for ( Type exceptionTypeToExclude : exceptionTypesToExclude ) {
|
||||||
|
for ( Type parentExceptionType : parentExceptionTypes ) {
|
||||||
|
if ( parentExceptionType.isAssignableTo( exceptionTypeToExclude ) ) {
|
||||||
|
result.remove( parentExceptionType );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Type> getImportTypes() {
|
||||||
|
Set<Type> imported = new HashSet<Type>();
|
||||||
|
imported.addAll( super.getImportTypes() );
|
||||||
|
imported.add( targetType ); /* is a new target type */
|
||||||
|
return imported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Assignment getFactoryMethod() {
|
||||||
|
return factoryMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -23,12 +23,21 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.model.source.Method;
|
import org.mapstruct.ap.model.source.Method;
|
||||||
import org.mapstruct.ap.model.source.MethodMatcher;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects only create method candidates (so methods not containing {@link @MappingTarget} )
|
* Selection based on type of method (create - or update method).
|
||||||
* {@link MethodMatcher}).
|
|
||||||
*
|
*
|
||||||
|
* <p>
|
||||||
|
* Prefers (when present):
|
||||||
|
* <ol>
|
||||||
|
* <li>create method candidates (methods not containing {@link @MappingTarget} ) when mapping method is
|
||||||
|
* a create method</li>
|
||||||
|
* <li>update method candidates (methods containing {@link @MappingTarget} ) when mapping method is
|
||||||
|
* an update method</li>
|
||||||
|
* </ol>
|
||||||
|
* When not present, the remaining (createCandidates when mapping method is update method, updateCandidates when mapping
|
||||||
|
* method is a create method) candidates are selected.
|
||||||
|
* </p>
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
public class CreateOrUpdateSelector implements MethodSelector {
|
public class CreateOrUpdateSelector implements MethodSelector {
|
||||||
@ -38,14 +47,23 @@ public class CreateOrUpdateSelector implements MethodSelector {
|
|||||||
Type sourceType, Type targetType,
|
Type sourceType, Type targetType,
|
||||||
SelectionCriteria criteria) {
|
SelectionCriteria criteria) {
|
||||||
|
|
||||||
|
boolean isCreateMethod = mappingMethod.getMappingTargetParameter() == null;
|
||||||
List<T> createCandidates = new ArrayList<T>();
|
List<T> createCandidates = new ArrayList<T>();
|
||||||
|
List<T> updateCandidates = new ArrayList<T>();
|
||||||
for ( T method : methods ) {
|
for ( T method : methods ) {
|
||||||
boolean isCreateCandidate = method.getMappingTargetParameter() == null;
|
boolean isCreateCandidate = method.getMappingTargetParameter() == null;
|
||||||
if ( isCreateCandidate ) {
|
if ( isCreateCandidate ) {
|
||||||
createCandidates.add( method );
|
createCandidates.add( method );
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
updateCandidates.add( method );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( criteria.isPreferUpdateMapping() ) {
|
||||||
|
if ( !updateCandidates.isEmpty() ) {
|
||||||
|
return updateCandidates;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return createCandidates;
|
return createCandidates;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,14 @@ public class SelectionCriteria {
|
|||||||
private final List<TypeMirror> qualifiers;
|
private final List<TypeMirror> qualifiers;
|
||||||
private final String targetPropertyName;
|
private final String targetPropertyName;
|
||||||
private final TypeMirror qualifyingResultType;
|
private final TypeMirror qualifyingResultType;
|
||||||
|
private boolean preferUpdateMapping;
|
||||||
|
|
||||||
public SelectionCriteria(List<TypeMirror> qualifiers, String targetPropertyName, TypeMirror qualifyingResultType) {
|
public SelectionCriteria(List<TypeMirror> qualifiers, String targetPropertyName, TypeMirror qualifyingResultType,
|
||||||
|
boolean preferUpdateMapping ) {
|
||||||
this.qualifiers = qualifiers;
|
this.qualifiers = qualifiers;
|
||||||
this.targetPropertyName = targetPropertyName;
|
this.targetPropertyName = targetPropertyName;
|
||||||
this.qualifyingResultType = qualifyingResultType;
|
this.qualifyingResultType = qualifyingResultType;
|
||||||
|
this.preferUpdateMapping = preferUpdateMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TypeMirror> getQualifiers() {
|
public List<TypeMirror> getQualifiers() {
|
||||||
@ -49,4 +52,13 @@ public class SelectionCriteria {
|
|||||||
public TypeMirror getQualifyingResultType() {
|
public TypeMirror getQualifyingResultType() {
|
||||||
return qualifyingResultType;
|
return qualifyingResultType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPreferUpdateMapping() {
|
||||||
|
return preferUpdateMapping;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPreferUpdateMapping(boolean preferUpdateMapping) {
|
||||||
|
this.preferUpdateMapping = preferUpdateMapping;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -272,14 +272,6 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidReferencedMethod(List<Parameter> parameters) {
|
private boolean isValidReferencedMethod(List<Parameter> parameters) {
|
||||||
return isValidReferencedOrFactoryMethod( 1, parameters );
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isValidFactoryMethod(List<Parameter> parameters) {
|
|
||||||
return isValidReferencedOrFactoryMethod( 0, parameters );
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isValidReferencedOrFactoryMethod(int sourceParamCount, List<Parameter> parameters) {
|
|
||||||
int validSourceParameters = 0;
|
int validSourceParameters = 0;
|
||||||
int targetParameters = 0;
|
int targetParameters = 0;
|
||||||
int targetTypeParameters = 0;
|
int targetTypeParameters = 0;
|
||||||
@ -297,7 +289,33 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
validSourceParameters++;
|
validSourceParameters++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return validSourceParameters == sourceParamCount && targetParameters == 0 && targetTypeParameters <= 1
|
return validSourceParameters == 1
|
||||||
|
&& targetParameters <= 1
|
||||||
|
&& targetTypeParameters <= 1
|
||||||
|
&& parameters.size() == validSourceParameters + targetParameters + targetTypeParameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidFactoryMethod(List<Parameter> parameters) {
|
||||||
|
int validSourceParameters = 0;
|
||||||
|
int targetParameters = 0;
|
||||||
|
int targetTypeParameters = 0;
|
||||||
|
|
||||||
|
for ( Parameter param : parameters ) {
|
||||||
|
if ( param.isMappingTarget() ) {
|
||||||
|
targetParameters++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( param.isTargetType() ) {
|
||||||
|
targetTypeParameters++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !param.isMappingTarget() && !param.isTargetType() ) {
|
||||||
|
validSourceParameters++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return validSourceParameters == 0
|
||||||
|
&& targetParameters == 0
|
||||||
|
&& targetTypeParameters <= 1
|
||||||
&& parameters.size() == validSourceParameters + targetParameters + targetTypeParameters;
|
&& parameters.size() == validSourceParameters + targetParameters + targetTypeParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,9 +97,10 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
@Override
|
@Override
|
||||||
public Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType,
|
public Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType,
|
||||||
Type targetType, String targetPropertyName, String dateFormat, List<TypeMirror> qualifiers,
|
Type targetType, String targetPropertyName, String dateFormat, List<TypeMirror> qualifiers,
|
||||||
TypeMirror resultType, String sourceReference) {
|
TypeMirror resultType, String sourceReference, boolean preferUpdateMapping) {
|
||||||
|
|
||||||
SelectionCriteria criteria = new SelectionCriteria(qualifiers, targetPropertyName, resultType );
|
SelectionCriteria criteria =
|
||||||
|
new SelectionCriteria(qualifiers, targetPropertyName, resultType, preferUpdateMapping );
|
||||||
|
|
||||||
ResolvingAttempt attempt = new ResolvingAttempt(
|
ResolvingAttempt attempt = new ResolvingAttempt(
|
||||||
sourceModel,
|
sourceModel,
|
||||||
@ -122,7 +123,7 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
public MethodReference getFactoryMethod( Method mappingMethod, Type targetType, List<TypeMirror> qualifiers,
|
public MethodReference getFactoryMethod( Method mappingMethod, Type targetType, List<TypeMirror> qualifiers,
|
||||||
TypeMirror resultType ) {
|
TypeMirror resultType ) {
|
||||||
|
|
||||||
SelectionCriteria criteria = new SelectionCriteria(qualifiers, null, resultType );
|
SelectionCriteria criteria = new SelectionCriteria( qualifiers, null, resultType, false );
|
||||||
|
|
||||||
ResolvingAttempt attempt = new ResolvingAttempt(
|
ResolvingAttempt attempt = new ResolvingAttempt(
|
||||||
sourceModel,
|
sourceModel,
|
||||||
@ -150,6 +151,7 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
private final String dateFormat;
|
private final String dateFormat;
|
||||||
private final SelectionCriteria selectionCriteria;
|
private final SelectionCriteria selectionCriteria;
|
||||||
private final String sourceReference;
|
private final String sourceReference;
|
||||||
|
private final boolean savedPreferUpdateMapping;
|
||||||
|
|
||||||
// resolving via 2 steps creates the possibillity of wrong matches, first builtin method matches,
|
// resolving via 2 steps creates the possibillity of wrong matches, first builtin method matches,
|
||||||
// second doesn't. In that case, the first builtin method should not lead to a virtual method
|
// second doesn't. In that case, the first builtin method should not lead to a virtual method
|
||||||
@ -166,6 +168,7 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
this.sourceReference = sourceReference;
|
this.sourceReference = sourceReference;
|
||||||
this.virtualMethodCandidates = new HashSet<VirtualMappingMethod>();
|
this.virtualMethodCandidates = new HashSet<VirtualMappingMethod>();
|
||||||
this.selectionCriteria = criteria;
|
this.selectionCriteria = criteria;
|
||||||
|
this.savedPreferUpdateMapping = criteria.isPreferUpdateMapping();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Method> List<T> filterPossibleCandidateMethods(List<T> candidateMethods) {
|
private <T extends Method> List<T> filterPossibleCandidateMethods(List<T> candidateMethods) {
|
||||||
@ -223,6 +226,9 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
return referencedMethod;
|
return referencedMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stop here when looking for update methods.
|
||||||
|
selectionCriteria.setPreferUpdateMapping( false );
|
||||||
|
|
||||||
// 2 step method, finally: conversion(method(source))
|
// 2 step method, finally: conversion(method(source))
|
||||||
conversion = resolveViaMethodAndConversion( sourceType, targetType );
|
conversion = resolveViaMethodAndConversion( sourceType, targetType );
|
||||||
if ( conversion != null ) {
|
if ( conversion != null ) {
|
||||||
@ -311,8 +317,10 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
resolveViaMethod( methodYCandidate.getSourceParameters().get( 0 ).getType(), targetType, true );
|
resolveViaMethod( methodYCandidate.getSourceParameters().get( 0 ).getType(), targetType, true );
|
||||||
|
|
||||||
if ( methodRefY != null ) {
|
if ( methodRefY != null ) {
|
||||||
|
selectionCriteria.setPreferUpdateMapping( false );
|
||||||
Assignment methodRefX =
|
Assignment methodRefX =
|
||||||
resolveViaMethod( sourceType, methodYCandidate.getSourceParameters().get( 0 ).getType(), true );
|
resolveViaMethod( sourceType, methodYCandidate.getSourceParameters().get( 0 ).getType(), true );
|
||||||
|
selectionCriteria.setPreferUpdateMapping( savedPreferUpdateMapping );
|
||||||
if ( methodRefX != null ) {
|
if ( methodRefX != null ) {
|
||||||
methodRefY.setAssignment( methodRefX );
|
methodRefY.setAssignment( methodRefX );
|
||||||
methodRefX.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
|
methodRefX.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
|
||||||
|
@ -49,8 +49,10 @@ public enum Message {
|
|||||||
PROPERTYMAPPING_INVALID_PARAMETER_NAME( "Method has no parameter named \"%s\"." ),
|
PROPERTYMAPPING_INVALID_PARAMETER_NAME( "Method has no parameter named \"%s\"." ),
|
||||||
PROPERTYMAPPING_NO_PROPERTY_IN_PARAMETER( "The type of parameter \"%s\" has no property named \"%s\"." ),
|
PROPERTYMAPPING_NO_PROPERTY_IN_PARAMETER( "The type of parameter \"%s\" has no property named \"%s\"." ),
|
||||||
PROPERTYMAPPING_INVALID_PROPERTY_NAME( "No property named \"%s\" exists in source parameter(s)." ),
|
PROPERTYMAPPING_INVALID_PROPERTY_NAME( "No property named \"%s\" exists in source parameter(s)." ),
|
||||||
|
PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE( "No read accessor found for property \"%s\" in target type." ),
|
||||||
|
|
||||||
CONSTANTMAPPING_MAPPING_NOT_FOUND( "Can't map \"%s %s\" to \"%s %s\"." ),
|
CONSTANTMAPPING_MAPPING_NOT_FOUND( "Can't map \"%s %s\" to \"%s %s\"." ),
|
||||||
|
CONSTANTMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE( "No read accessor found for property \"%s\" in target type." ),
|
||||||
|
|
||||||
MAPMAPPING_KEY_MAPPING_NOT_FOUND( "No implementation can be generated for this method. Found no method nor implicit conversion for mapping source key type to target key type." ),
|
MAPMAPPING_KEY_MAPPING_NOT_FOUND( "No implementation can be generated for this method. Found no method nor implicit conversion for mapping source key type to target key type." ),
|
||||||
MAPMAPPING_VALUE_MAPPING_NOT_FOUND( "No implementation can be generated for this method. Found no method nor implicit conversion for mapping source value type to target value type." ),
|
MAPMAPPING_VALUE_MAPPING_NOT_FOUND( "No implementation can be generated for this method. Found no method nor implicit conversion for mapping source value type to target value type." ),
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
<#--
|
||||||
|
|
||||||
|
Copyright 2012-2015 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.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<#if (exceptionTypes?size == 0) >
|
||||||
|
<@_assignment/>;
|
||||||
|
<#else>
|
||||||
|
try {
|
||||||
|
<@_assignment/>;
|
||||||
|
}
|
||||||
|
<#list exceptionTypes as exceptionType>
|
||||||
|
catch ( <@includeModel object=exceptionType/> e ) {
|
||||||
|
throw new RuntimeException( e );
|
||||||
|
}
|
||||||
|
</#list>
|
||||||
|
</#if>
|
||||||
|
<#macro _assignment>
|
||||||
|
if ( ${ext.targetBeanName}.${ext.targetReadAccessorName}() == null ) {
|
||||||
|
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <#if factoryMethod??><@includeModel object=factoryMethod targetType=ext.targetType raw=true/><#else>new <@includeModel object=ext.targetType/>()</#if> );
|
||||||
|
}
|
||||||
|
<@includeModel object=assignment
|
||||||
|
targetBeanName=ext.targetBeanName
|
||||||
|
raw=ext.raw
|
||||||
|
existingInstanceMapping=ext.existingInstanceMapping
|
||||||
|
targetReadAccessorName=ext.targetReadAccessorName
|
||||||
|
targetWriteAccessorName=ext.targetWriteAccessorName
|
||||||
|
targetType=ext.targetType/>
|
||||||
|
</#macro>
|
@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class CompanyDto {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private DepartmentDto department;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DepartmentDto getDepartment() {
|
||||||
|
return department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartment(DepartmentDto department) {
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class CompanyEntity {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private DepartmentEntity department;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DepartmentEntity getDepartment() {
|
||||||
|
return department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartment(DepartmentEntity department) {
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = DepartmentEntityFactory.class )
|
||||||
|
public interface CompanyMapper {
|
||||||
|
|
||||||
|
CompanyMapper INSTANCE = Mappers.getMapper( CompanyMapper.class );
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
DepartmentInBetween toInBetween(DepartmentDto dto);
|
||||||
|
|
||||||
|
void toDepartmentEntity(DepartmentInBetween dto, @MappingTarget DepartmentEntity entity);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class DepartmentDto {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class DepartmentEntity {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public DepartmentEntity(Integer justAnArgToAvoidConstruction) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class DepartmentEntityFactory {
|
||||||
|
|
||||||
|
|
||||||
|
public DepartmentEntity createDepartmentEntity() {
|
||||||
|
return new DepartmentEntity(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class DepartmentInBetween {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = DepartmentEntityFactory.class )
|
||||||
|
public interface ErroneousCompanyMapper1 {
|
||||||
|
|
||||||
|
ErroneousCompanyMapper1 INSTANCE = Mappers.getMapper( ErroneousCompanyMapper1.class );
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
void toInBetween(DepartmentDto dto, @MappingTarget DepartmentInBetween entity);
|
||||||
|
|
||||||
|
void toDepartmentEntity(DepartmentInBetween dto, @MappingTarget DepartmentEntity entity);
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = DepartmentEntityFactory.class )
|
||||||
|
public interface ErroneousOrganizationMapper1 {
|
||||||
|
|
||||||
|
ErroneousOrganizationMapper1 INSTANCE = Mappers.getMapper( ErroneousOrganizationMapper1.class );
|
||||||
|
|
||||||
|
@Mapping(target = "type", constant = "commercial")
|
||||||
|
void toOrganizationEntity(OrganizationDto dto, @MappingTarget OrganizationWithoutCompanyGetterEntity entity);
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
@Mapping(source = "type", target = "type")
|
||||||
|
void toName(String type, @MappingTarget OrganizationTypeEntity entity);
|
||||||
|
|
||||||
|
DepartmentEntity toDepartmentEntity(DepartmentDto dto);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ErroneousOrganizationMapper2 {
|
||||||
|
|
||||||
|
ErroneousOrganizationMapper2 INSTANCE = Mappers.getMapper( ErroneousOrganizationMapper2.class );
|
||||||
|
|
||||||
|
@Mapping(target = "type", constant = "commercial")
|
||||||
|
void toOrganizationEntity(OrganizationDto dto, @MappingTarget OrganizationWithoutTypeGetterEntity entity);
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
@Mapping(source = "type", target = "type")
|
||||||
|
void toName(String type, @MappingTarget OrganizationTypeEntity entity);
|
||||||
|
|
||||||
|
DepartmentEntity toDepartmentEntity(DepartmentDto dto);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class OrganizationDto {
|
||||||
|
|
||||||
|
private CompanyDto company;
|
||||||
|
|
||||||
|
public CompanyDto getCompany() {
|
||||||
|
return company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompany(CompanyDto company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class OrganizationEntity {
|
||||||
|
|
||||||
|
private CompanyEntity company;
|
||||||
|
private OrganizationTypeEntity type;
|
||||||
|
private OrganizationTypeNrEntity typeNr;
|
||||||
|
|
||||||
|
public CompanyEntity getCompany() {
|
||||||
|
return company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompany(CompanyEntity company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrganizationTypeEntity getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(OrganizationTypeEntity type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrganizationTypeNrEntity getTypeNr() {
|
||||||
|
return typeNr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTypeNr(OrganizationTypeNrEntity typeNr) {
|
||||||
|
this.typeNr = typeNr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( uses = DepartmentEntityFactory.class )
|
||||||
|
public interface OrganizationMapper {
|
||||||
|
|
||||||
|
OrganizationMapper INSTANCE = Mappers.getMapper( OrganizationMapper.class );
|
||||||
|
|
||||||
|
@Mappings({
|
||||||
|
@Mapping(target = "type", constant = "commercial"),
|
||||||
|
@Mapping(target = "typeNr", constant = "5")
|
||||||
|
})
|
||||||
|
void toOrganizationEntity(OrganizationDto dto, @MappingTarget OrganizationEntity entity);
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
DepartmentEntity toDepartmentEntity(DepartmentDto dto);
|
||||||
|
|
||||||
|
@Mapping(source = "type", target = "type")
|
||||||
|
void toName(String type, @MappingTarget OrganizationTypeEntity entity);
|
||||||
|
|
||||||
|
@Mapping(source = "number", target = "number")
|
||||||
|
void toNumber(Integer number, @MappingTarget OrganizationTypeNrEntity entity);
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class OrganizationTypeEntity {
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class OrganizationTypeNrEntity {
|
||||||
|
|
||||||
|
private Integer number;
|
||||||
|
|
||||||
|
public Integer getNumber() {
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumber(Integer number) {
|
||||||
|
this.number = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class OrganizationWithoutCompanyGetterEntity {
|
||||||
|
|
||||||
|
private CompanyEntity company;
|
||||||
|
private OrganizationTypeEntity type;
|
||||||
|
|
||||||
|
public void setCompany(CompanyEntity company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrganizationTypeEntity getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(OrganizationTypeEntity type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class OrganizationWithoutTypeGetterEntity {
|
||||||
|
|
||||||
|
private CompanyEntity company;
|
||||||
|
private OrganizationTypeEntity type;
|
||||||
|
|
||||||
|
public CompanyEntity getCompany() {
|
||||||
|
return company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompany(CompanyEntity company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(OrganizationTypeEntity type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,207 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
||||||
|
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
||||||
|
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@IssueKey("160")
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
public class UpdateMethodsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
OrganizationMapper.class,
|
||||||
|
OrganizationDto.class,
|
||||||
|
OrganizationEntity.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentEntity.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
OrganizationTypeEntity.class,
|
||||||
|
OrganizationTypeNrEntity.class
|
||||||
|
} )
|
||||||
|
public void testPreferUpdateMethod() {
|
||||||
|
|
||||||
|
OrganizationEntity organizationEntity = new OrganizationEntity();
|
||||||
|
CompanyEntity companyEntity = new CompanyEntity();
|
||||||
|
organizationEntity.setCompany( companyEntity );
|
||||||
|
companyEntity.setName( "CocaCola" );
|
||||||
|
|
||||||
|
OrganizationDto organizationDto = new OrganizationDto();
|
||||||
|
CompanyDto companyDto = new CompanyDto();
|
||||||
|
organizationDto.setCompany( companyDto );
|
||||||
|
companyDto.setName( "PepsiCo" );
|
||||||
|
DepartmentDto departmentDto = new DepartmentDto();
|
||||||
|
departmentDto.setName( "finance" );
|
||||||
|
companyDto.setDepartment( departmentDto );
|
||||||
|
|
||||||
|
OrganizationMapper.INSTANCE.toOrganizationEntity( organizationDto, organizationEntity );
|
||||||
|
|
||||||
|
assertThat( organizationEntity.getCompany() ).isEqualTo( companyEntity );
|
||||||
|
assertThat( organizationEntity.getCompany().getName() ).isEqualTo( "PepsiCo" );
|
||||||
|
assertThat( organizationEntity.getType().getType() ).isEqualTo( "commercial" );
|
||||||
|
assertThat( organizationEntity.getTypeNr().getNumber()).isEqualTo( 5 );
|
||||||
|
assertThat( organizationEntity.getCompany().getDepartment().getName() ).isEqualTo( "finance" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
OrganizationMapper.class,
|
||||||
|
OrganizationDto.class,
|
||||||
|
OrganizationEntity.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentEntity.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
OrganizationTypeEntity.class,
|
||||||
|
OrganizationTypeNrEntity.class
|
||||||
|
} )
|
||||||
|
public void testPreferUpdateMethodSourceObjectNotDefined() {
|
||||||
|
|
||||||
|
OrganizationEntity organizationEntity = new OrganizationEntity();
|
||||||
|
|
||||||
|
OrganizationDto organizationDto = new OrganizationDto();
|
||||||
|
CompanyDto companyDto = new CompanyDto();
|
||||||
|
organizationDto.setCompany( companyDto );
|
||||||
|
companyDto.setName( "PepsiCo" );
|
||||||
|
DepartmentDto departmentDto = new DepartmentDto();
|
||||||
|
departmentDto.setName( "finance" );
|
||||||
|
companyDto.setDepartment( departmentDto );
|
||||||
|
|
||||||
|
OrganizationMapper.INSTANCE.toOrganizationEntity( organizationDto, organizationEntity );
|
||||||
|
|
||||||
|
assertThat( organizationEntity.getCompany().getName() ).isEqualTo( "PepsiCo" );
|
||||||
|
assertThat( organizationEntity.getType().getType() ).isEqualTo( "commercial" );
|
||||||
|
assertThat( organizationEntity.getTypeNr().getNumber()).isEqualTo( 5 );
|
||||||
|
assertThat( organizationEntity.getCompany().getDepartment().getName() ).isEqualTo( "finance" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
CompanyMapper.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentInBetween.class,
|
||||||
|
DepartmentEntity.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
OrganizationTypeEntity.class,
|
||||||
|
OrganizationTypeNrEntity.class
|
||||||
|
} )
|
||||||
|
public void testPreferUpdateMethodEncapsulatingCreateMethod() {
|
||||||
|
|
||||||
|
CompanyEntity companyEntity = new CompanyEntity();
|
||||||
|
|
||||||
|
CompanyDto companyDto = new CompanyDto();
|
||||||
|
DepartmentDto departmentDto = new DepartmentDto();
|
||||||
|
departmentDto.setName( "finance" );
|
||||||
|
companyDto.setDepartment( departmentDto );
|
||||||
|
|
||||||
|
CompanyMapper.INSTANCE.toCompanyEntity( companyDto, companyEntity );
|
||||||
|
|
||||||
|
assertThat( companyEntity.getDepartment().getName() ).isEqualTo( "finance" );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
ErroneousOrganizationMapper1.class,
|
||||||
|
OrganizationDto.class,
|
||||||
|
OrganizationWithoutCompanyGetterEntity.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentEntity.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
OrganizationTypeEntity.class
|
||||||
|
} )
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousOrganizationMapper1.class,
|
||||||
|
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||||
|
line = 36,
|
||||||
|
messageRegExp = "No read accessor found for property \"company\" in target type.")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void testShouldFailOnPropertyMappingNoPropertyGetter() { }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
ErroneousOrganizationMapper2.class,
|
||||||
|
OrganizationDto.class,
|
||||||
|
OrganizationWithoutTypeGetterEntity.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentEntity.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
OrganizationTypeEntity.class
|
||||||
|
} )
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousOrganizationMapper2.class,
|
||||||
|
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||||
|
line = 36,
|
||||||
|
messageRegExp = "No read accessor found for property \"type\" in target type.")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void testShouldFailOnConstantMappingNoPropertyGetter() { }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( {
|
||||||
|
ErroneousCompanyMapper1.class,
|
||||||
|
OrganizationDto.class,
|
||||||
|
OrganizationWithoutTypeGetterEntity.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentInBetween.class,
|
||||||
|
DepartmentEntity.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
OrganizationTypeEntity.class
|
||||||
|
} )
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousCompanyMapper1.class,
|
||||||
|
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||||
|
line = 34,
|
||||||
|
messageRegExp = "Can't map property \".*DepartmentDto department\" to \".*DepartmentEntity department.")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void testShouldFailOnTwoNestedUpdateMethods() { }
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods.selection;
|
||||||
|
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentDto;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class ExternalHandWrittenMapper {
|
||||||
|
|
||||||
|
|
||||||
|
public void toDepartmentEntity(DepartmentDto dto, @MappingTarget DepartmentEntity entity) {
|
||||||
|
if ( entity != null && dto != null ) {
|
||||||
|
entity.setName( dto.getName() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods.selection;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentDto;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentEntity;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ExternalMapper {
|
||||||
|
|
||||||
|
ExternalMapper INSTANCE = Mappers.getMapper( ExternalMapper.class );
|
||||||
|
|
||||||
|
void toDepartmentEntity(DepartmentDto dto, @MappingTarget DepartmentEntity entity);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods.selection;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.CompanyDto;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.CompanyEntity;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentDto;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentEntity;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentEntityFactory;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@IssueKey("160")
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
public class ExternalSelectionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses({
|
||||||
|
OrganizationMapper1.class,
|
||||||
|
ExternalMapper.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
DepartmentEntity.class
|
||||||
|
})
|
||||||
|
public void shouldSelectGeneratedExternalMapper() {
|
||||||
|
|
||||||
|
CompanyEntity entity = new CompanyEntity();
|
||||||
|
CompanyDto dto = new CompanyDto();
|
||||||
|
OrganizationMapper1.INSTANCE.toCompanyEntity( dto, entity );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses({
|
||||||
|
OrganizationMapper2.class,
|
||||||
|
ExternalHandWrittenMapper.class,
|
||||||
|
CompanyDto.class,
|
||||||
|
CompanyEntity.class,
|
||||||
|
DepartmentDto.class,
|
||||||
|
DepartmentEntityFactory.class,
|
||||||
|
DepartmentEntity.class
|
||||||
|
})
|
||||||
|
public void shouldSelectGeneratedHandWrittenExternalMapper() {
|
||||||
|
|
||||||
|
CompanyEntity entity = new CompanyEntity();
|
||||||
|
CompanyDto dto = new CompanyDto();
|
||||||
|
OrganizationMapper2.INSTANCE.toCompanyEntity( dto, entity );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods.selection;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.CompanyDto;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.CompanyEntity;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentEntityFactory;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper(uses = { ExternalMapper.class, DepartmentEntityFactory.class } )
|
||||||
|
public interface OrganizationMapper1 {
|
||||||
|
|
||||||
|
OrganizationMapper1 INSTANCE = Mappers.getMapper( OrganizationMapper1.class );
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 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.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.updatemethods.selection;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.CompanyDto;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.CompanyEntity;
|
||||||
|
import org.mapstruct.ap.test.updatemethods.DepartmentEntityFactory;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper(uses = { ExternalHandWrittenMapper.class, DepartmentEntityFactory.class } )
|
||||||
|
public interface OrganizationMapper2 {
|
||||||
|
|
||||||
|
OrganizationMapper2 INSTANCE = Mappers.getMapper( OrganizationMapper2.class );
|
||||||
|
|
||||||
|
void toCompanyEntity(CompanyDto dto, @MappingTarget CompanyEntity entity);
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user