mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#891 Cleanup and renaming of Direct and AssignmentFactory
This commit is contained in:
parent
750af10900
commit
371e0884a4
@ -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.<Type>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" )
|
||||
);
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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<Type> importTypes, List<Type> 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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
|
||||
|
@ -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
|
||||
);
|
||||
|
||||
|
@ -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:
|
||||
* <ol>
|
||||
* <li>MethodReference</li>
|
||||
* <li>TypeConversion</li>
|
||||
* <li>Direct Assignment (empty TargetAssignment)</li>
|
||||
* <li>SourceRHS Assignment (empty TargetAssignment)</li>
|
||||
* <li>null, no assignment found</li>
|
||||
* </ol>
|
||||
*/
|
||||
@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);
|
||||
|
||||
/**
|
||||
|
@ -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<PropertyEntry> 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
|
||||
|
@ -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;
|
||||
}
|
@ -47,7 +47,7 @@ public class TypeConversion extends ModelElement implements Assignment {
|
||||
*/
|
||||
private Assignment assignment;
|
||||
|
||||
TypeConversion( Set<Type> importTypes,
|
||||
public TypeConversion( Set<Type> importTypes,
|
||||
List<Type> exceptionTypes,
|
||||
String expression ) {
|
||||
this.importTypes = new HashSet<Type>( importTypes );
|
||||
|
@ -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<VirtualMappingMethod> virtualMethodCandidates;
|
||||
|
||||
private ResolvingAttempt(List<SourceMethod> 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<VirtualMappingMethod>();
|
||||
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
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user