mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#212 regression fix
This commit is contained in:
parent
13dbc3cccf
commit
4ff0676c61
@ -51,7 +51,6 @@ import org.mapstruct.ap.internal.option.ReportingPolicy;
|
|||||||
import org.mapstruct.ap.internal.prism.BeanMappingPrism;
|
import org.mapstruct.ap.internal.prism.BeanMappingPrism;
|
||||||
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
|
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
|
||||||
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
|
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
|
||||||
import org.mapstruct.ap.internal.util.Executables;
|
|
||||||
import org.mapstruct.ap.internal.util.MapperConfiguration;
|
import org.mapstruct.ap.internal.util.MapperConfiguration;
|
||||||
import org.mapstruct.ap.internal.util.Message;
|
import org.mapstruct.ap.internal.util.Message;
|
||||||
import org.mapstruct.ap.internal.util.Strings;
|
import org.mapstruct.ap.internal.util.Strings;
|
||||||
@ -372,7 +371,9 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
List<ExecutableElement> candidates = new ArrayList<ExecutableElement>( 2 );
|
List<ExecutableElement> candidates = new ArrayList<ExecutableElement>( 2 );
|
||||||
|
|
||||||
while ( targetPropertiesIterator.hasNext() ) {
|
while ( targetPropertiesIterator.hasNext() ) {
|
||||||
|
|
||||||
Entry<String, ExecutableElement> targetProperty = targetPropertiesIterator.next();
|
Entry<String, ExecutableElement> targetProperty = targetPropertiesIterator.next();
|
||||||
|
String propertyName = targetProperty.getKey();
|
||||||
|
|
||||||
PropertyMapping propertyMapping = null;
|
PropertyMapping propertyMapping = null;
|
||||||
|
|
||||||
@ -380,39 +381,32 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
|
|
||||||
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
||||||
|
|
||||||
if ( sourceParameter.getType().isPrimitive() ) {
|
Type sourceType = sourceParameter.getType();
|
||||||
|
|
||||||
|
if ( sourceType.isPrimitive() ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection<ExecutableElement> sourceReadAccessors =
|
|
||||||
sourceParameter.getType().getPropertyReadAccessors().values();
|
|
||||||
for ( ExecutableElement sourceReadAccessor : sourceReadAccessors ) {
|
|
||||||
String sourcePropertyName = Executables.getPropertyName( sourceReadAccessor );
|
|
||||||
|
|
||||||
if ( sourcePropertyName.equals( targetProperty.getKey() ) ) {
|
|
||||||
candidates.add( sourceReadAccessor );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PropertyMapping newPropertyMapping = null;
|
PropertyMapping newPropertyMapping = null;
|
||||||
ExecutableElement sourceAccessor = getSourceAccessor( targetProperty.getKey(), candidates );
|
ExecutableElement sourceAccessor = sourceType.getPropertyReadAccessors().get( propertyName );
|
||||||
if ( sourceAccessor != null ) {
|
if ( sourceAccessor != null ) {
|
||||||
Mapping mapping = method.getSingleMappingByTargetPropertyName( targetProperty.getKey() );
|
Mapping mapping = method.getSingleMappingByTargetPropertyName( propertyName );
|
||||||
DeclaredType sourceType = (DeclaredType) sourceParameter.getType().getTypeMirror();
|
DeclaredType declaredSourceType = (DeclaredType) sourceParameter.getType().getTypeMirror();
|
||||||
|
|
||||||
SourceReference sourceRef = new SourceReference.BuilderFromProperty()
|
SourceReference sourceRef = new SourceReference.BuilderFromProperty()
|
||||||
.sourceParameter( sourceParameter )
|
.sourceParameter( sourceParameter )
|
||||||
.type( ctx.getTypeFactory().getReturnType( sourceType, sourceAccessor ) )
|
.type( ctx.getTypeFactory().getReturnType( declaredSourceType, sourceAccessor ) )
|
||||||
.accessor( sourceAccessor )
|
.accessor( sourceAccessor )
|
||||||
.name( targetProperty.getKey() )
|
.name( propertyName )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
newPropertyMapping = new PropertyMappingBuilder()
|
newPropertyMapping = new PropertyMappingBuilder()
|
||||||
.mappingContext( ctx )
|
.mappingContext( ctx )
|
||||||
.sourceMethod( method )
|
.sourceMethod( method )
|
||||||
.targetWriteAccessor( targetProperty.getValue() )
|
.targetWriteAccessor( targetProperty.getValue() )
|
||||||
.targetReadAccessor( getTargetPropertyReadAccessor( targetProperty.getKey() ) )
|
.targetReadAccessor( getTargetPropertyReadAccessor( propertyName ) )
|
||||||
.targetPropertyName( targetProperty.getKey() )
|
.targetPropertyName( propertyName )
|
||||||
.sourceReference( sourceRef )
|
.sourceReference( sourceRef )
|
||||||
.formattingParameters( mapping != null ? mapping.getFormattingParameters() : null )
|
.formattingParameters( mapping != null ? mapping.getFormattingParameters() : null )
|
||||||
.selectionParameters( mapping != null ? mapping.getSelectionParameters() : null )
|
.selectionParameters( mapping != null ? mapping.getSelectionParameters() : null )
|
||||||
@ -432,7 +426,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
ctx.getMessager().printMessage(
|
ctx.getMessager().printMessage(
|
||||||
method.getExecutable(),
|
method.getExecutable(),
|
||||||
Message.BEANMAPPING_SEVERAL_POSSIBLE_SOURCES,
|
Message.BEANMAPPING_SEVERAL_POSSIBLE_SOURCES,
|
||||||
targetProperty.getKey()
|
propertyName
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -492,34 +486,6 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExecutableElement getSourceAccessor(String sourcePropertyName, List<ExecutableElement> candidates) {
|
|
||||||
if ( candidates.isEmpty() ) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else if ( candidates.size() == 1 ) {
|
|
||||||
return candidates.get( 0 );
|
|
||||||
}
|
|
||||||
// can only be the case for Booleans: isFoo() and getFoo(); The latter is preferred then
|
|
||||||
else if ( candidates.size() == 2 ) {
|
|
||||||
if ( candidates.get( 0 ).getSimpleName().toString().startsWith( "get" ) ) {
|
|
||||||
return candidates.get( 0 );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return candidates.get( 1 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Should never really happen
|
|
||||||
else {
|
|
||||||
ctx.getMessager().printMessage(
|
|
||||||
method.getExecutable(),
|
|
||||||
Message.BEANMAPPING_SEVERAL_POSSIBLE_TARGET_ACCESSORS,
|
|
||||||
sourcePropertyName
|
|
||||||
);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExecutableElement getTargetPropertyReadAccessor( String propertyName ) {
|
private ExecutableElement getTargetPropertyReadAccessor( String propertyName ) {
|
||||||
return method.getResultType().getPropertyReadAccessors().get( propertyName );
|
return method.getResultType().getPropertyReadAccessors().get( propertyName );
|
||||||
}
|
}
|
||||||
|
@ -361,8 +361,19 @@ public class Type extends ModelElement implements Comparable<Type> {
|
|||||||
if ( getters == null ) {
|
if ( getters == null ) {
|
||||||
List<ExecutableElement> getterList = Filters.getterMethodsIn( getAllExecutables() );
|
List<ExecutableElement> getterList = Filters.getterMethodsIn( getAllExecutables() );
|
||||||
Map<String, ExecutableElement> modifiableGetters = new LinkedHashMap<String, ExecutableElement>();
|
Map<String, ExecutableElement> modifiableGetters = new LinkedHashMap<String, ExecutableElement>();
|
||||||
for (ExecutableElement getter : getterList) {
|
for ( ExecutableElement getter : getterList ) {
|
||||||
modifiableGetters.put( Executables.getPropertyName( getter ), getter );
|
String propertyName = Executables.getPropertyName( getter );
|
||||||
|
if ( modifiableGetters.containsKey( propertyName ) ) {
|
||||||
|
// In the DefaultAccessorNamingStrategy, this can only be the case for Booleans: isFoo() and
|
||||||
|
// getFoo(); The latter is preferred.
|
||||||
|
if ( !getter.getSimpleName().toString().startsWith( "is" ) ) {
|
||||||
|
modifiableGetters.put( Executables.getPropertyName( getter ), getter );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
modifiableGetters.put( Executables.getPropertyName( getter ), getter );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
getters = Collections.unmodifiableMap( modifiableGetters );
|
getters = Collections.unmodifiableMap( modifiableGetters );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user