From c9a313ac151d30dd234dd3d907c213a8e91fac04 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Wed, 1 Feb 2017 21:03:25 +0100 Subject: [PATCH] #1050 Extract a common MappingMethod for the normal (non-enum / non-value) mapping methods that are used within MapStruct --- .../ap/internal/model/BeanMappingMethod.java | 18 +-- .../model/ContainerMappingMethod.java | 60 ++------- .../ap/internal/model/MapMappingMethod.java | 87 +------------ .../model/NormalTypeMappingMethod.java | 114 ++++++++++++++++++ 4 files changed, 132 insertions(+), 147 deletions(-) create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/NormalTypeMappingMethod.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java index d3e6c9473..7797edaeb 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java @@ -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 propertyMappings; private final Map> mappingsByParameter; @@ -667,13 +667,10 @@ public class BeanMappingMethod extends ContainerMappingMethod { List 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 diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java index cc6dfa367..608ed8b17 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java @@ -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 beforeMappingReferences, List 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 thisTypeParameters = getSourceParameters().get( i ).getType().getTypeParameters(); - List 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; } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java index f37729e74..861cd4b1a 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java @@ -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 { @@ -183,13 +180,10 @@ public class MapMappingMethod extends MappingMethod { MethodReference factoryMethod, boolean mapNullToDefault, List beforeMappingReferences, List 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 thisTypeParameters = getSourceParameters().get( i ).getType().getTypeParameters(); - List 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(); - } - } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/NormalTypeMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/NormalTypeMappingMethod.java new file mode 100644 index 000000000..9a5f7863c --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/NormalTypeMappingMethod.java @@ -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 beforeMappingReferences, + List afterMappingReferences) { + super( method, beforeMappingReferences, afterMappingReferences ); + this.factoryMethod = factoryMethod; + this.overridden = method.overridesMethod(); + this.mapNullToDefault = mapNullToDefault; + } + + @Override + public Set getImportTypes() { + Set 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 thisTypeParameters = getSourceParameters().get( i ).getType().getTypeParameters(); + List otherTypeParameters = other.getSourceParameters().get( i ).getType().getTypeParameters(); + + if ( !thisTypeParameters.equals( otherTypeParameters ) ) { + return false; + } + } + + return isMapNullToDefault() == other.isMapNullToDefault(); + } +}