From 371e0884a4babe543dca34721aa012d83789a537 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Thu, 15 Sep 2016 22:07:14 +0200 Subject: [PATCH] #891 Cleanup and renaming of Direct and AssignmentFactory --- .../conversion/DateToStringConversion.java | 8 ++- .../internal/conversion/SimpleConversion.java | 7 +-- .../ap/internal/model/AssignmentFactory.java | 54 ------------------- .../internal/model/IterableMappingMethod.java | 2 +- .../ap/internal/model/MapMappingMethod.java | 4 +- .../internal/model/MappingBuilderContext.java | 6 +-- .../ap/internal/model/PropertyMapping.java | 36 ++++++------- .../model/{Direct.java => SourceRHS.java} | 6 +-- .../ap/internal/model/TypeConversion.java | 2 +- .../creation/MappingResolverImpl.java | 34 ++++++------ .../model/{Direct.ftl => SourceRHS.ftl} | 0 11 files changed, 49 insertions(+), 110 deletions(-) delete mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/AssignmentFactory.java rename processor/src/main/java/org/mapstruct/ap/internal/model/{Direct.java => SourceRHS.java} (91%) rename processor/src/main/resources/org/mapstruct/ap/internal/model/{Direct.ftl => SourceRHS.ftl} (100%) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToStringConversion.java index 11f1ea193..e43f9be4b 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/DateToStringConversion.java @@ -27,10 +27,10 @@ import java.util.Collections; import java.util.Date; import java.util.List; -import org.mapstruct.ap.internal.model.AssignmentFactory; import org.mapstruct.ap.internal.model.assignment.Assignment; import org.mapstruct.ap.internal.model.common.ConversionContext; import org.mapstruct.ap.internal.model.HelperMethod; +import org.mapstruct.ap.internal.model.TypeConversion; import org.mapstruct.ap.internal.model.common.Type; /** @@ -42,8 +42,7 @@ public class DateToStringConversion implements ConversionProvider { @Override public Assignment to(ConversionContext conversionContext) { - return AssignmentFactory.createTypeConversion( - asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), + return new TypeConversion( asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), Collections.emptyList(), getConversionExpression( conversionContext, "format" ) ); @@ -51,8 +50,7 @@ public class DateToStringConversion implements ConversionProvider { @Override public Assignment from(ConversionContext conversionContext) { - return AssignmentFactory.createTypeConversion( - asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), + return new TypeConversion( asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), asList( conversionContext.getTypeFactory().getType( ParseException.class ) ), getConversionExpression( conversionContext, "parse" ) ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/SimpleConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/SimpleConversion.java index 8539b6d68..61e11c523 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/conversion/SimpleConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/SimpleConversion.java @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; -import org.mapstruct.ap.internal.model.AssignmentFactory; import org.mapstruct.ap.internal.model.TypeConversion; import org.mapstruct.ap.internal.model.assignment.Assignment; import org.mapstruct.ap.internal.model.common.ConversionContext; @@ -39,8 +38,7 @@ public abstract class SimpleConversion implements ConversionProvider { @Override public Assignment to(ConversionContext conversionContext) { String toExpression = getToExpression( conversionContext ); - return AssignmentFactory.createTypeConversion( - getToConversionImportTypes( conversionContext ), + return new TypeConversion( getToConversionImportTypes( conversionContext ), getToConversionExceptionTypes( conversionContext ), toExpression ); @@ -49,8 +47,7 @@ public abstract class SimpleConversion implements ConversionProvider { @Override public Assignment from(ConversionContext conversionContext) { String fromExpression = getFromExpression( conversionContext ); - return AssignmentFactory.createTypeConversion( - getFromConversionImportTypes( conversionContext ), + return new TypeConversion( getFromConversionImportTypes( conversionContext ), getFromConversionExceptionTypes( conversionContext ), fromExpression ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/AssignmentFactory.java b/processor/src/main/java/org/mapstruct/ap/internal/model/AssignmentFactory.java deleted file mode 100644 index 864ebda4c..000000000 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/AssignmentFactory.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2012-2016 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.assignment.Assignment; -import org.mapstruct.ap.internal.model.common.ConversionContext; -import org.mapstruct.ap.internal.model.common.Type; -import org.mapstruct.ap.internal.model.source.Method; -import org.mapstruct.ap.internal.model.source.builtin.BuiltInMethod; - -/** - * Factory class for creating all types of assignments - * - * @author Sjaak Derksen - */ -public class AssignmentFactory { - - private AssignmentFactory() { - } - - public static Assignment createTypeConversion(Set importTypes, List exceptionTypes, String expression) { - return new TypeConversion( importTypes, exceptionTypes, expression ); - } - - public static Assignment createMethodReference(Method method, MapperReference declaringMapper, - Type targetType) { - return new MethodReference( method, declaringMapper, targetType ); - } - - public static Assignment createMethodReference(BuiltInMethod method, ConversionContext contextParam) { - return new MethodReference( method, contextParam ); - } - -} - diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/IterableMappingMethod.java index 32a902167..234a4df86 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/IterableMappingMethod.java @@ -108,7 +108,7 @@ public class IterableMappingMethod extends MappingMethod { null, // there is no targetPropertyName formattingParameters, selectionParameters, - new Direct( loopVariableName, sourceElementType ), + new SourceRHS( loopVariableName, sourceElementType ), false ); 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 ac110a5d7..9df422ca6 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 @@ -111,7 +111,7 @@ public class MapMappingMethod extends MappingMethod { null, // there is no targetPropertyName keyFormattingParameters, keySelectionParameters, - new Direct( "entry.getKey()", keySourceType ), + new SourceRHS( "entry.getKey()", keySourceType ), false ); @@ -138,7 +138,7 @@ public class MapMappingMethod extends MappingMethod { null, // there is no targetPropertyName valueFormattingParameters, valueSelectionParameters, - new Direct( "entry.getValue()", valueSourceType ), + new SourceRHS( "entry.getValue()", valueSourceType ), false ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java b/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java index 26ada12e9..f340681b3 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java @@ -81,21 +81,21 @@ public class MappingBuilderContext { * @param targetPropertyName name of the target property * @param formattingParameters used for formatting dates and numbers * @param selectionParameters parameters used in the selection process - * @param sourceReference source information + * @param sourceRHS source information * @param preferUpdateMethods selection should prefer update methods when present. * * @return an assignment to a method parameter, which can either be: *
    *
  1. MethodReference
  2. *
  3. TypeConversion
  4. - *
  5. Direct Assignment (empty TargetAssignment)
  6. + *
  7. SourceRHS Assignment (empty TargetAssignment)
  8. *
  9. null, no assignment found
  10. *
*/ @SuppressWarnings("checkstyle:parameternumber") Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType, String targetPropertyName, FormattingParameters formattingParameters, - SelectionParameters selectionParameters, Direct sourceReference, + SelectionParameters selectionParameters, SourceRHS sourceRHS, boolean preferUpdateMethods); /** diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index 8e1d0885a..9deb776c0 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -216,15 +216,15 @@ public class PropertyMapping extends ModelElement { // handle source String sourceElement = getSourceElement(); Type sourceType = getSourceType(); - Direct source; + SourceRHS sourceRHS; if ( targetWriteAccessorType == TargetWriteAccessorType.ADDER && sourceType.isCollectionType() ) { // handle adder, if source is collection then use iterator element type as source type. // sourceRef becomes a local variable in the itereation. sourceType = sourceType.getTypeParameters().get( 0 ); - source = new Direct( Executables.getElementNameForAdder( targetWriteAccessor ), sourceType ); + sourceRHS = new SourceRHS( Executables.getElementNameForAdder( targetWriteAccessor ), sourceType ); } else { - source = getSource(); + sourceRHS = getSourceRHS(); } // all the tricky cases will be excluded for the time being. @@ -244,17 +244,17 @@ public class PropertyMapping extends ModelElement { targetPropertyName, formattingParameters, selectionParameters, - source, + sourceRHS, preferUpdateMethods ); // No mapping found. Try to forge a mapping if ( assignment == null ) { if ( (sourceType.isCollectionType() || sourceType.isArrayType()) && targetType.isIterableType() ) { - assignment = forgeIterableMapping( sourceType, targetType, source, method.getExecutable() ); + assignment = forgeIterableMapping( sourceType, targetType, sourceRHS, method.getExecutable() ); } else if ( sourceType.isMapType() && targetType.isMapType() ) { - assignment = forgeMapMapping( sourceType, targetType, source, method.getExecutable() ); + assignment = forgeMapMapping( sourceType, targetType, sourceRHS, method.getExecutable() ); } } @@ -402,7 +402,7 @@ public class PropertyMapping extends ModelElement { result = new AdderWrapper( result, method.getThrownTypes(), - getSource().getSourceReference(), + getSourceRHS().getSourceReference(), sourceType ); result = new NullCheckWrapper( result, getSourcePresenceCheckerRef() ); @@ -527,18 +527,18 @@ public class PropertyMapping extends ModelElement { } } - private Direct getSource() { + private SourceRHS getSourceRHS() { Parameter sourceParam = sourceReference.getParameter(); List propertyEntries = sourceReference.getPropertyEntries(); // parameter reference if ( propertyEntries.isEmpty() ) { - return new Direct( sourceParam.getName(), getSourceType() ); + return new SourceRHS( sourceParam.getName(), getSourceType() ); } // simple property else if ( propertyEntries.size() == 1 ) { PropertyEntry propertyEntry = propertyEntries.get( 0 ); - return new Direct( sourceParam.getName() + return new SourceRHS( sourceParam.getName() + "." + propertyEntry.getReadAccessor().getSimpleName() + "()", propertyEntry.getType() ); } // nested property given as dot path @@ -571,7 +571,7 @@ public class PropertyMapping extends ModelElement { else { forgedName = ctx.getExistingMappingMethod( nestedPropertyMapping ).getName(); } - return new Direct( forgedName + "( " + sourceParam.getName() + " )", getSourceType() ); + return new SourceRHS( forgedName + "( " + sourceParam.getName() + " )", getSourceType() ); } } @@ -610,7 +610,7 @@ public class PropertyMapping extends ModelElement { return sourcePresenceChecker; } - private Assignment forgeIterableMapping(Type sourceType, Type targetType, Direct source, + private Assignment forgeIterableMapping(Type sourceType, Type targetType, SourceRHS source, ExecutableElement element) { Assignment assignment = null; @@ -638,14 +638,14 @@ public class PropertyMapping extends ModelElement { methodRef = new ForgedMethod( existingName, methodRef ); } - assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType ); + assignment = new MethodReference( methodRef, null, targetType ); assignment.setAssignment( source ); } return assignment; } - private Assignment forgeMapMapping(Type sourceType, Type targetType, Direct source, + private Assignment forgeMapMapping(Type sourceType, Type targetType, SourceRHS source, ExecutableElement element) { Assignment assignment = null; @@ -671,7 +671,7 @@ public class PropertyMapping extends ModelElement { String existingName = ctx.getExistingMappingMethod( mapMappingMethod ).getName(); methodRef = new ForgedMethod( existingName, methodRef ); } - assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType ); + assignment = new MethodReference( methodRef, null, targetType ); assignment.setAssignment( source ); } @@ -730,7 +730,7 @@ public class PropertyMapping extends ModelElement { targetPropertyName, formattingParameters, selectionParameters, - new Direct( constantExpression, sourceType ), + new SourceRHS( constantExpression, sourceType ), method.getMappingTargetParameter() != null ); } @@ -797,7 +797,7 @@ public class PropertyMapping extends ModelElement { // String String quotation marks. String enumExpression = constantExpression.substring( 1, constantExpression.length() - 1 ); if ( targetType.getEnumConstants().contains( enumExpression ) ) { - assignment = new Direct( enumExpression, targetType ); + assignment = new SourceRHS( enumExpression, targetType ); assignment = new EnumConstantWrapper( assignment, targetType ); } else { @@ -823,7 +823,7 @@ public class PropertyMapping extends ModelElement { } public PropertyMapping build() { - Assignment assignment = new Direct( javaExpression, null ); + Assignment assignment = new SourceRHS( javaExpression, null ); if ( Executables.isSetterMethod( targetWriteAccessor ) ) { // setter, so wrap in setter diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/Direct.java b/processor/src/main/java/org/mapstruct/ap/internal/model/SourceRHS.java similarity index 91% rename from processor/src/main/java/org/mapstruct/ap/internal/model/Direct.java rename to processor/src/main/java/org/mapstruct/ap/internal/model/SourceRHS.java index 5010cf4d0..c88b6c182 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/Direct.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/SourceRHS.java @@ -27,17 +27,17 @@ import org.mapstruct.ap.internal.model.common.ModelElement; import org.mapstruct.ap.internal.model.common.Type; /** - * Direct Assignment. Just a source reference + * SourceRHS Assignment. Right Hand Side (RHS), source part of the assignment. * * @author Sjaak Derksen */ -public class Direct extends ModelElement implements Assignment { +public class SourceRHS extends ModelElement implements Assignment { private final String sourceReference; private final Type sourceType; private String sourceLocalVarName; - public Direct(String sourceReference, Type sourceType ) { + public SourceRHS(String sourceReference, Type sourceType ) { this.sourceReference = sourceReference; this.sourceType = sourceType; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/TypeConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/model/TypeConversion.java index a1172f3fc..fbb0053b8 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/TypeConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/TypeConversion.java @@ -47,7 +47,7 @@ public class TypeConversion extends ModelElement implements Assignment { */ private Assignment assignment; - TypeConversion( Set importTypes, + public TypeConversion( Set importTypes, List exceptionTypes, String expression ) { this.importTypes = new HashSet( importTypes ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java index 8a3a56e6c..029448707 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java @@ -35,8 +35,7 @@ import javax.lang.model.util.Types; import org.mapstruct.ap.internal.conversion.ConversionProvider; import org.mapstruct.ap.internal.conversion.Conversions; -import org.mapstruct.ap.internal.model.AssignmentFactory; -import org.mapstruct.ap.internal.model.Direct; +import org.mapstruct.ap.internal.model.SourceRHS; import org.mapstruct.ap.internal.model.MapperReference; import org.mapstruct.ap.internal.model.MappingBuilderContext.MappingResolver; import org.mapstruct.ap.internal.model.MethodReference; @@ -109,7 +108,7 @@ public class MappingResolverImpl implements MappingResolver { @SuppressWarnings("checkstyle:parameternumber") public Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType, String targetPropertyName, FormattingParameters formattingParameters, - SelectionParameters selectionParameters, Direct sourceReference, boolean preferUpdateMapping) { + SelectionParameters selectionParameters, SourceRHS sourceRHS, boolean preferUpdateMapping) { SelectionCriteria criteria = new SelectionCriteria( selectionParameters, targetPropertyName, preferUpdateMapping ); @@ -127,7 +126,7 @@ public class MappingResolverImpl implements MappingResolver { mappedElement, dateFormat, numberFormat, - sourceReference, + sourceRHS, criteria ); @@ -172,7 +171,7 @@ public class MappingResolverImpl implements MappingResolver { private final String dateFormat; private final String numberFormat; private final SelectionCriteria selectionCriteria; - private final Direct sourceReference; + private final SourceRHS sourceRHS; private final boolean savedPreferUpdateMapping; // resolving via 2 steps creates the possibillity of wrong matches, first builtin method matches, @@ -181,14 +180,14 @@ public class MappingResolverImpl implements MappingResolver { private final Set virtualMethodCandidates; private ResolvingAttempt(List sourceModel, Method mappingMethod, String mappedElement, - String dateFormat, String numberFormat, Direct sourceReference, SelectionCriteria criteria) { + String dateFormat, String numberFormat, SourceRHS sourceRHS, SelectionCriteria criteria) { this.mappingMethod = mappingMethod; this.mappedElement = mappedElement; this.methods = filterPossibleCandidateMethods( sourceModel ); this.dateFormat = dateFormat; this.numberFormat = numberFormat; - this.sourceReference = sourceReference; + this.sourceRHS = sourceRHS; this.virtualMethodCandidates = new HashSet(); this.selectionCriteria = criteria; this.savedPreferUpdateMapping = criteria.isPreferUpdateMapping(); @@ -210,28 +209,28 @@ public class MappingResolverImpl implements MappingResolver { // first simple mapping method Assignment referencedMethod = resolveViaMethod( sourceType, targetType, false ); if ( referencedMethod != null ) { - referencedMethod.setAssignment( sourceReference ); + referencedMethod.setAssignment( sourceRHS ); return referencedMethod; } // then direct assignable if ( sourceType.isAssignableTo( targetType ) || isAssignableThroughCollectionCopyConstructor( sourceType, targetType ) ) { - Assignment simpleAssignment = sourceReference; + Assignment simpleAssignment = sourceRHS; return simpleAssignment; } // then type conversion Assignment conversion = resolveViaConversion( sourceType, targetType ); if ( conversion != null ) { - conversion.setAssignment( sourceReference ); + conversion.setAssignment( sourceRHS ); return conversion; } // check for a built-in method Assignment builtInMethod = resolveViaBuiltInMethod( sourceType, targetType ); if ( builtInMethod != null ) { - builtInMethod.setAssignment( sourceReference ); + builtInMethod.setAssignment( sourceRHS ); usedVirtualMappings.addAll( virtualMethodCandidates ); return builtInMethod; } @@ -309,8 +308,8 @@ public class MappingResolverImpl implements MappingResolver { ConversionContext ctx = new DefaultConversionContext( typeFactory, messager, sourceType, targetType, dateFormat, numberFormat); - Assignment methodReference = AssignmentFactory.createMethodReference( matchingBuiltInMethod, ctx ); - methodReference.setAssignment( sourceReference ); + Assignment methodReference = new MethodReference( matchingBuiltInMethod, ctx ); + methodReference.setAssignment( sourceRHS ); return methodReference; } @@ -351,7 +350,7 @@ public class MappingResolverImpl implements MappingResolver { selectionCriteria.setPreferUpdateMapping( savedPreferUpdateMapping ); if ( methodRefX != null ) { methodRefY.setAssignment( methodRefX ); - methodRefX.setAssignment( sourceReference ); + methodRefX.setAssignment( sourceRHS ); break; } else { @@ -388,7 +387,7 @@ public class MappingResolverImpl implements MappingResolver { resolveViaConversion( sourceType, methodYCandidate.getSourceParameters().get( 0 ).getType() ); if ( conversionXRef != null ) { methodRefY.setAssignment( conversionXRef ); - conversionXRef.setAssignment( sourceReference ); + conversionXRef.setAssignment( sourceRHS ); break; } else { @@ -431,7 +430,7 @@ public class MappingResolverImpl implements MappingResolver { conversionYRef = resolveViaConversion( methodXCandidate.getReturnType(), targetType ); if ( conversionYRef != null ) { conversionYRef.setAssignment( methodRefX ); - methodRefX.setAssignment( sourceReference ); + methodRefX.setAssignment( sourceRHS ); break; } else { @@ -505,8 +504,7 @@ public class MappingResolverImpl implements MappingResolver { Type targetType) { MapperReference mapperReference = findMapperReference( method ); - return AssignmentFactory.createMethodReference( - method, + return new MethodReference( method, mapperReference, SourceMethod.containsTargetTypeParameter( method.getParameters() ) ? targetType : null ); diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/Direct.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/SourceRHS.ftl similarity index 100% rename from processor/src/main/resources/org/mapstruct/ap/internal/model/Direct.ftl rename to processor/src/main/resources/org/mapstruct/ap/internal/model/SourceRHS.ftl