mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#220 Allow getter as target accessor for Map types
This commit is contained in:
parent
ff6f8ab3ca
commit
b5423cc8f8
@ -22,6 +22,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.lang.model.element.AnnotationMirror;
|
import javax.lang.model.element.AnnotationMirror;
|
||||||
@ -284,11 +285,9 @@ public class Type extends ModelElement implements Comparable<Type> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alternative accessors could be a getter for a collection. By means of the
|
* Alternative accessors could be a getter for a collection / map. By means of the
|
||||||
* {@link java.util.Collection#addAll(java.util.Collection) } this getter can still
|
* {@link Collection#addAll(Collection) } or {@link Map#putAll(Map)} this getter can still be used as
|
||||||
* be used as targetAccessor. JAXB XJC tool generates such constructs.
|
* targetAccessor. JAXB XJC tool generates such constructs. This method can be extended when new cases come along.
|
||||||
*
|
|
||||||
* This method can be extended when new cases come along.
|
|
||||||
*
|
*
|
||||||
* @return an unmodifiable list of alternative target accessors.
|
* @return an unmodifiable list of alternative target accessors.
|
||||||
*/
|
*/
|
||||||
@ -299,34 +298,47 @@ public class Type extends ModelElement implements Comparable<Type> {
|
|||||||
List<ExecutableElement> setterMethods = getSetters();
|
List<ExecutableElement> setterMethods = getSetters();
|
||||||
List<ExecutableElement> getterMethods = getGetters();
|
List<ExecutableElement> getterMethods = getGetters();
|
||||||
|
|
||||||
if ( getterMethods.size() > setterMethods.size() ) {
|
// there could be a getter method for a list/map that is not present as setter.
|
||||||
// there could be a getter method for a list that is not present as setter.
|
// a getter could substitute the setter in that case and act as setter.
|
||||||
// a getter could substitute the setter in that case and act as setter.
|
// (assuming it is initialized)
|
||||||
// (assuming it is initialized)
|
for ( ExecutableElement getterMethod : getterMethods ) {
|
||||||
for ( ExecutableElement getterMethod : getterMethods ) {
|
if ( hasNoSetterMethod( getterMethod, setterMethods ) && isCollectionOrMap( getterMethod ) ) {
|
||||||
boolean matchFound = false;
|
result.add( getterMethod );
|
||||||
String getterPropertyName = Executables.getPropertyName( getterMethod );
|
|
||||||
for ( ExecutableElement setterMethod : setterMethods ) {
|
|
||||||
String setterPropertyName = Executables.getPropertyName( setterMethod );
|
|
||||||
if ( getterPropertyName.equals( setterPropertyName ) ) {
|
|
||||||
matchFound = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( !matchFound && isCollection( getterMethod.getReturnType() ) ) {
|
|
||||||
result.add( getterMethod );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
alternativeTargetAccessors = Collections.unmodifiableList( result );
|
alternativeTargetAccessors = Collections.unmodifiableList( result );
|
||||||
}
|
}
|
||||||
return alternativeTargetAccessors;
|
return alternativeTargetAccessors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasNoSetterMethod(ExecutableElement getterMethod, List<ExecutableElement> setterMethods) {
|
||||||
|
String getterPropertyName = Executables.getPropertyName( getterMethod );
|
||||||
|
for ( ExecutableElement setterMethod : setterMethods ) {
|
||||||
|
String setterPropertyName = Executables.getPropertyName( setterMethod );
|
||||||
|
if ( getterPropertyName.equals( setterPropertyName ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCollectionOrMap(ExecutableElement getterMethod) {
|
||||||
|
return isCollection( getterMethod.getReturnType() ) || isMap( getterMethod.getReturnType() );
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isCollection(TypeMirror candidate) {
|
private boolean isCollection(TypeMirror candidate) {
|
||||||
String collectionName = Collection.class.getCanonicalName();
|
return isSubType( candidate, Collection.class );
|
||||||
TypeMirror collectionType = typeUtils.erasure( elementUtils.getTypeElement( collectionName ).asType() );
|
}
|
||||||
return TypeUtilsJDK6Fix.isSubType( typeUtils, candidate, collectionType );
|
|
||||||
|
private boolean isMap(TypeMirror candidate) {
|
||||||
|
return isSubType( candidate, Map.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSubType(TypeMirror candidate, Class<?> clazz) {
|
||||||
|
String className = clazz.getCanonicalName();
|
||||||
|
TypeMirror classType = typeUtils.erasure( elementUtils.getTypeElement( className ).asType() );
|
||||||
|
return TypeUtilsJDK6Fix.isSubType( typeUtils, candidate, classType );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user