mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#120 Formatting and commenting
This commit is contained in:
parent
47ecc23fa0
commit
32f0bc0e22
@ -18,12 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.source.BuiltInMethod;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.mapstruct.ap.model.common.ConversionContext;
|
|
||||||
|
|
||||||
|
import org.mapstruct.ap.model.common.ConversionContext;
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
|
import org.mapstruct.ap.model.source.BuiltInMethod;
|
||||||
import org.mapstruct.ap.model.source.SourceMethod;
|
import org.mapstruct.ap.model.source.SourceMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,19 +34,24 @@ import org.mapstruct.ap.model.source.SourceMethod;
|
|||||||
public class MethodReference extends MappingMethod {
|
public class MethodReference extends MappingMethod {
|
||||||
|
|
||||||
private final MapperReference declaringMapper;
|
private final MapperReference declaringMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In case this reference targets a built-in method, allows to pass specific context information to the invoked
|
||||||
|
* method. Currently this is only used to pass in the configured date format string when invoking a built-in method
|
||||||
|
* which requires that.
|
||||||
|
*/
|
||||||
private final String contextParam;
|
private final String contextParam;
|
||||||
|
|
||||||
|
public MethodReference(SourceMethod method, MapperReference declaringMapper) {
|
||||||
public MethodReference(SourceMethod method, MapperReference declaringMapper ) {
|
|
||||||
super( method );
|
super( method );
|
||||||
this.declaringMapper = declaringMapper;
|
this.declaringMapper = declaringMapper;
|
||||||
this.contextParam = null;
|
this.contextParam = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodReference(BuiltInMethod method, ConversionContext contextParam ) {
|
public MethodReference(BuiltInMethod method, ConversionContext contextParam) {
|
||||||
super( method );
|
super( method );
|
||||||
this.declaringMapper = null;
|
this.declaringMapper = null;
|
||||||
this.contextParam = method.getContextParameter( contextParam );
|
this.contextParam = method.getContextParameter( contextParam );
|
||||||
}
|
}
|
||||||
|
|
||||||
public MapperReference getDeclaringMapper() {
|
public MapperReference getDeclaringMapper() {
|
||||||
@ -64,11 +69,4 @@ public class MethodReference extends MappingMethod {
|
|||||||
public String getContextParam() {
|
public String getContextParam() {
|
||||||
return contextParam;
|
return contextParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String quoteParamWhenNotNull(String param) {
|
|
||||||
if (param != null ) {
|
|
||||||
return param != null ? "\"" + param + "\"" : "null";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
package org.mapstruct.ap.model.common;
|
package org.mapstruct.ap.model.common;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConversionContext object passed to conversion providers.
|
* Context object passed to conversion providers and built-in methods.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
@ -33,11 +33,11 @@ public interface ConversionContext {
|
|||||||
Type getTargetType();
|
Type getTargetType();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the date format if this conversion is from String to
|
* Returns the date format if this conversion or built-in method is from String to a date type (e.g. {@link Date})
|
||||||
* {@link Date} or vice versa. Returns {@code null} for other types or
|
* or vice versa.
|
||||||
* if not given.
|
|
||||||
*
|
*
|
||||||
* @return The date format if this conversion.
|
* @return The date format if this conversion or built-in method is from String to a date type. {@code null} is
|
||||||
|
* returned for other types or if not given.
|
||||||
*/
|
*/
|
||||||
String getDateFormat();
|
String getDateFormat();
|
||||||
|
|
||||||
|
@ -70,9 +70,9 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
|
|||||||
* excluding generic type variables. When the implementor sees a need for this, this method can be overridden.
|
* excluding generic type variables. When the implementor sees a need for this, this method can be overridden.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean matches( Type sourceType, Type targetType ) {
|
public boolean matches(Type sourceType, Type targetType) {
|
||||||
if ( targetType.erasure().isAssignableTo( getReturnType().erasure() )
|
if ( targetType.erasure().isAssignableTo( getReturnType().erasure() )
|
||||||
&& sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) {
|
&& sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) {
|
||||||
return doTypeVarsMatch( sourceType, targetType );
|
return doTypeVarsMatch( sourceType, targetType );
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -93,6 +93,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
|
|||||||
*
|
*
|
||||||
* declaring mapper is always null, being the MapperImpl itself. This method should not be overridden by
|
* declaring mapper is always null, being the MapperImpl itself. This method should not be overridden by
|
||||||
* implementors
|
* implementors
|
||||||
|
*
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -110,6 +111,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* target parameter mechanism not supported for build-in-method
|
* target parameter mechanism not supported for build-in-method
|
||||||
|
*
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -122,6 +124,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
|
|||||||
* with context specific information such as a date format.
|
* with context specific information such as a date format.
|
||||||
*
|
*
|
||||||
* @param conversionContext
|
* @param conversionContext
|
||||||
|
*
|
||||||
* @return null if no context parameter should be included
|
* @return null if no context parameter should be included
|
||||||
* "null" if there should be an explicit null call
|
* "null" if there should be an explicit null call
|
||||||
* "'dateFormat'" for instance, to indicate how the build-in method should format the date
|
* "'dateFormat'" for instance, to indicate how the build-in method should format the date
|
||||||
@ -144,10 +147,11 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
|
|||||||
* equals based on class
|
* equals based on class
|
||||||
*
|
*
|
||||||
* @param obj other class
|
* @param obj other class
|
||||||
|
*
|
||||||
* @return true when classes are the same
|
* @return true when classes are the same
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean equals( Object obj ) {
|
public boolean equals(Object obj) {
|
||||||
if ( obj == null ) {
|
if ( obj == null ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -160,9 +164,10 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
|
|||||||
*
|
*
|
||||||
* @param parameter source
|
* @param parameter source
|
||||||
* @param returnType target
|
* @param returnType target
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean doTypeVarsMatch( Type parameter, Type returnType ) {
|
public boolean doTypeVarsMatch(Type parameter, Type returnType) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -578,18 +578,20 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
targetType = typeFactory.getReturnType( targetAcessor );
|
targetType = typeFactory.getReturnType( targetAcessor );
|
||||||
}
|
}
|
||||||
|
|
||||||
// first try the SourceMethods
|
// first try the source methods
|
||||||
String mappedElement = "property '" + Executables.getPropertyName( sourceAccessor ) + "'";
|
String mappedElement = "property '" + Executables.getPropertyName( sourceAccessor ) + "'";
|
||||||
MethodReference propertyMappingMethod = getMappingMethodReference(
|
MethodReference propertyMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, mappedElement, methods, sourceType, targetType ),
|
getBestMatch( method, mappedElement, methods, sourceType, targetType ),
|
||||||
mapperReferences );
|
mapperReferences
|
||||||
|
);
|
||||||
|
|
||||||
// then try BuiltInMethods
|
// then try built-in methods
|
||||||
if ( propertyMappingMethod == null ) {
|
if ( propertyMappingMethod == null ) {
|
||||||
propertyMappingMethod = getMappingMethodReference(
|
propertyMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, mappedElement, builtInMethods.getBuiltInMethods(), sourceType, targetType ),
|
getBestMatch( method, mappedElement, builtInMethods.getBuiltInMethods(), sourceType, targetType ),
|
||||||
targetType,
|
targetType,
|
||||||
dateFormat );
|
dateFormat
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeConversion conversion = getConversion(
|
TypeConversion conversion = getConversion(
|
||||||
@ -636,18 +638,22 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// first try the SourceMethods
|
// first try the source methods
|
||||||
MethodReference elementMappingMethod = getMappingMethodReference(
|
MethodReference elementMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, "collection element", methods, sourceElementType, targetElementType ),
|
getBestMatch( method, "collection element", methods, sourceElementType, targetElementType ),
|
||||||
mapperReferences );
|
mapperReferences
|
||||||
|
);
|
||||||
|
|
||||||
// then try BuiltInMethods
|
// then try built-in methods
|
||||||
if ( elementMappingMethod == null ) {
|
if ( elementMappingMethod == null ) {
|
||||||
elementMappingMethod = getMappingMethodReference(
|
elementMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, "collection element", builtInMethods.getBuiltInMethods(), sourceElementType,
|
getBestMatch(
|
||||||
targetElementType ),
|
method, "collection element", builtInMethods.getBuiltInMethods(), sourceElementType,
|
||||||
targetElementType,
|
targetElementType
|
||||||
dateFormat );
|
),
|
||||||
|
targetElementType,
|
||||||
|
dateFormat
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !sourceElementType.isAssignableTo( targetElementType ) && conversion == null &&
|
if ( !sourceElementType.isAssignableTo( targetElementType ) && conversion == null &&
|
||||||
@ -693,29 +699,39 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
"entry.getValue()"
|
"entry.getValue()"
|
||||||
);
|
);
|
||||||
|
|
||||||
// first try the SourceMethods
|
// first try the source methods
|
||||||
MethodReference keyMappingMethod = getMappingMethodReference(
|
MethodReference keyMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, "map key", methods, sourceKeyType, targetKeyType ), mapperReferences );
|
getBestMatch( method, "map key", methods, sourceKeyType, targetKeyType ), mapperReferences
|
||||||
|
);
|
||||||
|
|
||||||
// then try BuiltInMethods
|
// then try built-in methods
|
||||||
if ( keyMappingMethod == null ) {
|
if ( keyMappingMethod == null ) {
|
||||||
keyMappingMethod = getMappingMethodReference(
|
keyMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, "map key", builtInMethods.getBuiltInMethods(), sourceKeyType, targetKeyType ),
|
getBestMatch( method, "map key", builtInMethods.getBuiltInMethods(), sourceKeyType, targetKeyType ),
|
||||||
targetKeyType,
|
targetKeyType,
|
||||||
keyDateFormat );
|
keyDateFormat
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// first try the SourceMethods
|
// first try the source methods
|
||||||
MethodReference valueMappingMethod = getMappingMethodReference(
|
MethodReference valueMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, "map value", methods, sourceValueType, targetValueType ),
|
getBestMatch( method, "map value", methods, sourceValueType, targetValueType ),
|
||||||
mapperReferences );
|
mapperReferences
|
||||||
|
);
|
||||||
|
|
||||||
// then try BuiltInMethods
|
// then try built-in methods
|
||||||
if ( valueMappingMethod == null ) {
|
if ( valueMappingMethod == null ) {
|
||||||
valueMappingMethod = getMappingMethodReference(
|
valueMappingMethod = getMappingMethodReference(
|
||||||
getBestMatch( method, "map value", builtInMethods.getBuiltInMethods(), sourceValueType, targetValueType ),
|
getBestMatch(
|
||||||
targetValueType,
|
method,
|
||||||
valueDateFormat );
|
"map value",
|
||||||
|
builtInMethods.getBuiltInMethods(),
|
||||||
|
sourceValueType,
|
||||||
|
targetValueType
|
||||||
|
),
|
||||||
|
targetValueType,
|
||||||
|
valueDateFormat
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !sourceKeyType.isAssignableTo( targetKeyType ) && keyConversion == null && keyMappingMethod == null ) {
|
if ( !sourceKeyType.isAssignableTo( targetKeyType ) && keyConversion == null && keyMappingMethod == null ) {
|
||||||
@ -764,10 +780,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private <T extends Method> T getBestMatch(SourceMethod mappingMethod, String mappedElement,
|
private <T extends Method> T getBestMatch(SourceMethod mappingMethod, String mappedElement,
|
||||||
Iterable<T> methods, Type parameterType,
|
Iterable<T> methods, Type parameterType,
|
||||||
Type returnType) {
|
Type returnType) {
|
||||||
List<T> candidatesWithMathingTargetType = new ArrayList<T>();
|
List<T> candidatesWithMathingTargetType = new ArrayList<T>();
|
||||||
|
|
||||||
for ( T method : methods ) {
|
for ( T method : methods ) {
|
||||||
@ -820,7 +835,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
|
|
||||||
|
|
||||||
private <T extends Method> int addToCandidateListIfMinimal(List<T> candidatesWithBestMathingType,
|
private <T extends Method> int addToCandidateListIfMinimal(List<T> candidatesWithBestMathingType,
|
||||||
int bestMatchingTypeDistance, T method, int currentTypeDistance) {
|
int bestMatchingTypeDistance, T method,
|
||||||
|
int currentTypeDistance) {
|
||||||
if ( currentTypeDistance == bestMatchingTypeDistance ) {
|
if ( currentTypeDistance == bestMatchingTypeDistance ) {
|
||||||
candidatesWithBestMathingType.add( method );
|
candidatesWithBestMathingType.add( method );
|
||||||
}
|
}
|
||||||
@ -833,7 +849,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
return bestMatchingTypeDistance;
|
return bestMatchingTypeDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MethodReference getMappingMethodReference( SourceMethod method, List<MapperReference> mapperReferences ) {
|
private MethodReference getMappingMethodReference(SourceMethod method, List<MapperReference> mapperReferences) {
|
||||||
if ( method != null ) {
|
if ( method != null ) {
|
||||||
MapperReference mapperReference = null;
|
MapperReference mapperReference = null;
|
||||||
for ( MapperReference ref : mapperReferences ) {
|
for ( MapperReference ref : mapperReferences ) {
|
||||||
@ -847,7 +863,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MethodReference getMappingMethodReference( BuiltInMethod method, Type returnType, String dateFormat ) {
|
private MethodReference getMappingMethodReference(BuiltInMethod method, Type returnType, String dateFormat) {
|
||||||
if ( method != null ) {
|
if ( method != null ) {
|
||||||
virtualMethods.add( new VirtualMappingMethod( method ) );
|
virtualMethods.add( new VirtualMappingMethod( method ) );
|
||||||
ConversionContext ctx = new DefaultConversionContext( typeFactory, returnType, dateFormat );
|
ConversionContext ctx = new DefaultConversionContext( typeFactory, returnType, dateFormat );
|
||||||
@ -980,7 +996,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A getter could be an alternative getReturnType-accessor if a setter is not available, and the
|
* A getter could be an alternative getReturnType-accessor if a setter is not available, and the
|
||||||
getReturnType is a collection.
|
* getReturnType is a collection.
|
||||||
*
|
*
|
||||||
* Provided such a getter is initialized lazy by the getReturnType class, e.g. in generated JAXB beans.
|
* Provided such a getter is initialized lazy by the getReturnType class, e.g. in generated JAXB beans.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user