mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1050 Extract a common MappingMethod for the normal (non-enum / non-value) mapping methods that are used within MapStruct
This commit is contained in:
parent
79acfff9c3
commit
c9a313ac15
@ -69,7 +69,7 @@ import org.mapstruct.ap.internal.util.accessor.ExecutableElementAccessor;
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class BeanMappingMethod extends ContainerMappingMethod {
|
||||
public class BeanMappingMethod extends NormalTypeMappingMethod {
|
||||
|
||||
private final List<PropertyMapping> propertyMappings;
|
||||
private final Map<String, List<PropertyMapping>> mappingsByParameter;
|
||||
@ -667,13 +667,10 @@ public class BeanMappingMethod extends ContainerMappingMethod {
|
||||
List<LifecycleCallbackMethodReference> afterMappingReferences) {
|
||||
super(
|
||||
method,
|
||||
null,
|
||||
factoryMethod,
|
||||
mapNullToDefault,
|
||||
null,
|
||||
beforeMappingReferences,
|
||||
afterMappingReferences,
|
||||
null
|
||||
afterMappingReferences
|
||||
);
|
||||
|
||||
this.propertyMappings = propertyMappings;
|
||||
@ -749,17 +746,10 @@ public class BeanMappingMethod extends ContainerMappingMethod {
|
||||
return sourceParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getResultElementType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( getResultType() == null ) ? 0 : getResultType().hashCode() );
|
||||
return result;
|
||||
//Needed for Checkstyle, otherwise it fails due to EqualsHashCode rule
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,11 +36,8 @@ import org.mapstruct.ap.internal.util.Strings;
|
||||
*
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
public abstract class ContainerMappingMethod extends NormalTypeMappingMethod {
|
||||
private final Assignment elementAssignment;
|
||||
private final MethodReference factoryMethod;
|
||||
private final boolean overridden;
|
||||
private final boolean mapNullToDefault;
|
||||
private final String loopVariableName;
|
||||
private final SelectionParameters selectionParameters;
|
||||
|
||||
@ -49,11 +46,8 @@ public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
List<LifecycleCallbackMethodReference> beforeMappingReferences,
|
||||
List<LifecycleCallbackMethodReference> afterMappingReferences,
|
||||
SelectionParameters selectionParameters) {
|
||||
super( method, beforeMappingReferences, afterMappingReferences );
|
||||
super( method, factoryMethod, mapNullToDefault, beforeMappingReferences, afterMappingReferences );
|
||||
this.elementAssignment = parameterAssignment;
|
||||
this.factoryMethod = factoryMethod;
|
||||
this.overridden = method.overridesMethod();
|
||||
this.mapNullToDefault = mapNullToDefault;
|
||||
this.loopVariableName = loopVariableName;
|
||||
this.selectionParameters = selectionParameters;
|
||||
}
|
||||
@ -78,22 +72,9 @@ public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
if ( elementAssignment != null ) {
|
||||
types.addAll( elementAssignment.getImportTypes() );
|
||||
}
|
||||
if ( ( factoryMethod == null ) && ( !isExistingInstanceMapping() ) ) {
|
||||
if ( getReturnType().getImplementationType() != null ) {
|
||||
types.addAll( getReturnType().getImplementationType().getImportTypes() );
|
||||
}
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
public boolean isMapNullToDefault() {
|
||||
return mapNullToDefault;
|
||||
}
|
||||
|
||||
public boolean isOverridden() {
|
||||
return overridden;
|
||||
}
|
||||
|
||||
public String getLoopVariableName() {
|
||||
return loopVariableName;
|
||||
}
|
||||
@ -119,10 +100,6 @@ public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
}
|
||||
}
|
||||
|
||||
public MethodReference getFactoryMethod() {
|
||||
return this.factoryMethod;
|
||||
}
|
||||
|
||||
public abstract Type getResultElementType();
|
||||
|
||||
public String getIndex1Name() {
|
||||
@ -135,10 +112,8 @@ public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( getResultType() == null ) ? 0 : getResultType().hashCode() );
|
||||
return result;
|
||||
//Needed for Checkstyle, otherwise it fails due to EqualsHashCode rule
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -152,28 +127,13 @@ public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
if ( getClass() != obj.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !super.equals( obj ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ContainerMappingMethod other = (ContainerMappingMethod) obj;
|
||||
|
||||
if ( !getResultType().equals( other.getResultType() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( getSourceParameters().size() != other.getSourceParameters().size() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ( int i = 0; i < getSourceParameters().size(); i++ ) {
|
||||
if ( !getSourceParameters().get( i ).getType().equals( other.getSourceParameters().get( i ).getType() ) ) {
|
||||
return false;
|
||||
}
|
||||
List<Type> thisTypeParameters = getSourceParameters().get( i ).getType().getTypeParameters();
|
||||
List<Type> otherTypeParameters = other.getSourceParameters().get( i ).getType().getTypeParameters();
|
||||
|
||||
if ( !thisTypeParameters.equals( otherTypeParameters ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( this.selectionParameters != null ) {
|
||||
if ( !this.selectionParameters.equals( other.selectionParameters ) ) {
|
||||
return false;
|
||||
@ -183,7 +143,7 @@ public abstract class ContainerMappingMethod extends MappingMethod {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isMapNullToDefault() == other.isMapNullToDefault();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,8 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model;
|
||||
|
||||
import static org.mapstruct.ap.internal.util.Collections.first;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -36,19 +34,18 @@ import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
|
||||
import org.mapstruct.ap.internal.util.Strings;
|
||||
|
||||
import static org.mapstruct.ap.internal.util.Collections.first;
|
||||
|
||||
/**
|
||||
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one {@code Map} type to another. Keys and
|
||||
* values are mapped either by a {@link TypeConversion} or another mapping method if required.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class MapMappingMethod extends MappingMethod {
|
||||
public class MapMappingMethod extends NormalTypeMappingMethod {
|
||||
|
||||
private final Assignment keyAssignment;
|
||||
private final Assignment valueAssignment;
|
||||
private final MethodReference factoryMethod;
|
||||
private final boolean overridden;
|
||||
private final boolean mapNullToDefault;
|
||||
|
||||
public static class Builder extends AbstractMappingMethodBuilder<Builder, MapMappingMethod> {
|
||||
|
||||
@ -183,13 +180,10 @@ public class MapMappingMethod extends MappingMethod {
|
||||
MethodReference factoryMethod, boolean mapNullToDefault,
|
||||
List<LifecycleCallbackMethodReference> beforeMappingReferences,
|
||||
List<LifecycleCallbackMethodReference> afterMappingReferences) {
|
||||
super( method, beforeMappingReferences, afterMappingReferences );
|
||||
super( method, factoryMethod, mapNullToDefault, beforeMappingReferences, afterMappingReferences );
|
||||
|
||||
this.keyAssignment = keyAssignment;
|
||||
this.valueAssignment = valueAssignment;
|
||||
this.factoryMethod = factoryMethod;
|
||||
this.overridden = method.overridesMethod();
|
||||
this.mapNullToDefault = mapNullToDefault;
|
||||
}
|
||||
|
||||
public Parameter getSourceParameter() {
|
||||
@ -229,12 +223,6 @@ public class MapMappingMethod extends MappingMethod {
|
||||
if ( valueAssignment != null ) {
|
||||
types.addAll( valueAssignment.getImportTypes() );
|
||||
}
|
||||
if ( ( factoryMethod == null ) && ( !isExistingInstanceMapping() ) ) {
|
||||
types.addAll( getReturnType().getImportTypes() );
|
||||
if ( getReturnType().getImplementationType() != null ) {
|
||||
types.addAll( getReturnType().getImplementationType().getImportTypes() );
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
@ -259,71 +247,4 @@ public class MapMappingMethod extends MappingMethod {
|
||||
getParameterNames()
|
||||
);
|
||||
}
|
||||
|
||||
public MethodReference getFactoryMethod() {
|
||||
return this.factoryMethod;
|
||||
}
|
||||
|
||||
public boolean isMapNullToDefault() {
|
||||
return mapNullToDefault;
|
||||
}
|
||||
|
||||
public boolean isOverridden() {
|
||||
return overridden;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( getResultType() == null ) ? 0 : getResultType().hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if ( obj == null ) {
|
||||
return false;
|
||||
}
|
||||
if ( getClass() != obj.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
MapMappingMethod other = (MapMappingMethod) obj;
|
||||
|
||||
if ( !getResultType().equals( other.getResultType() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( getSourceParameters().size() != other.getSourceParameters().size() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ( int i = 0; i < getSourceParameters().size(); i++ ) {
|
||||
if ( !getSourceParameters().get( i ).getType().equals( other.getSourceParameters().get( i ).getType() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Type> thisTypeParameters = getSourceParameters().get( i ).getType().getTypeParameters();
|
||||
List<Type> otherTypeParameters = other.getSourceParameters().get( i ).getType().getTypeParameters();
|
||||
|
||||
if ( !thisTypeParameters.equals( otherTypeParameters ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( this.factoryMethod != null ) {
|
||||
if ( !this.factoryMethod.equals( other.factoryMethod ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ( other.factoryMethod != null ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isMapNullToDefault() == other.isMapNullToDefault();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Copyright 2012-2017 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.internal.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
|
||||
/**
|
||||
* A {@link MappingMethod} that is used by the main mapping methods ({@link BeanMappingMethod},
|
||||
* {@link MapMappingMethod}, {@link IterableMappingMethod} and {@link StreamMappingMethod} (non-enum / non-value
|
||||
* mapping)
|
||||
*
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public abstract class NormalTypeMappingMethod extends MappingMethod {
|
||||
private final MethodReference factoryMethod;
|
||||
private final boolean overridden;
|
||||
private final boolean mapNullToDefault;
|
||||
|
||||
NormalTypeMappingMethod(Method method, MethodReference factoryMethod, boolean mapNullToDefault,
|
||||
List<LifecycleCallbackMethodReference> beforeMappingReferences,
|
||||
List<LifecycleCallbackMethodReference> afterMappingReferences) {
|
||||
super( method, beforeMappingReferences, afterMappingReferences );
|
||||
this.factoryMethod = factoryMethod;
|
||||
this.overridden = method.overridesMethod();
|
||||
this.mapNullToDefault = mapNullToDefault;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> types = super.getImportTypes();
|
||||
if ( ( factoryMethod == null ) && ( !isExistingInstanceMapping() ) ) {
|
||||
if ( getReturnType().getImplementationType() != null ) {
|
||||
types.addAll( getReturnType().getImplementationType().getImportTypes() );
|
||||
}
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
public boolean isMapNullToDefault() {
|
||||
return mapNullToDefault;
|
||||
}
|
||||
|
||||
public boolean isOverridden() {
|
||||
return overridden;
|
||||
}
|
||||
|
||||
public MethodReference getFactoryMethod() {
|
||||
return this.factoryMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ( ( getResultType() == null ) ? 0 : getResultType().hashCode() );
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if ( obj == null ) {
|
||||
return false;
|
||||
}
|
||||
if ( getClass() != obj.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
NormalTypeMappingMethod other = (NormalTypeMappingMethod) obj;
|
||||
|
||||
if ( !getResultType().equals( other.getResultType() ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( getSourceParameters().size() != other.getSourceParameters().size() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ( int i = 0; i < getSourceParameters().size(); i++ ) {
|
||||
if ( !getSourceParameters().get( i ).getType().equals( other.getSourceParameters().get( i ).getType() ) ) {
|
||||
return false;
|
||||
}
|
||||
List<Type> thisTypeParameters = getSourceParameters().get( i ).getType().getTypeParameters();
|
||||
List<Type> otherTypeParameters = other.getSourceParameters().get( i ).getType().getTypeParameters();
|
||||
|
||||
if ( !thisTypeParameters.equals( otherTypeParameters ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return isMapNullToDefault() == other.isMapNullToDefault();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user