#120 Formatting and commenting

This commit is contained in:
Gunnar Morling 2014-02-22 19:50:54 +01:00
parent 47ecc23fa0
commit 32f0bc0e22
4 changed files with 77 additions and 58 deletions

View File

@ -18,12 +18,12 @@
*/
package org.mapstruct.ap.model;
import org.mapstruct.ap.model.source.BuiltInMethod;
import java.util.HashSet;
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.source.BuiltInMethod;
import org.mapstruct.ap.model.source.SourceMethod;
/**
@ -34,19 +34,24 @@ import org.mapstruct.ap.model.source.SourceMethod;
public class MethodReference extends MappingMethod {
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;
public MethodReference(SourceMethod method, MapperReference declaringMapper ) {
public MethodReference(SourceMethod method, MapperReference declaringMapper) {
super( method );
this.declaringMapper = declaringMapper;
this.contextParam = null;
this.contextParam = null;
}
public MethodReference(BuiltInMethod method, ConversionContext contextParam ) {
public MethodReference(BuiltInMethod method, ConversionContext contextParam) {
super( method );
this.declaringMapper = null;
this.contextParam = method.getContextParameter( contextParam );
this.contextParam = method.getContextParameter( contextParam );
}
public MapperReference getDeclaringMapper() {
@ -64,11 +69,4 @@ public class MethodReference extends MappingMethod {
public String getContextParam() {
return contextParam;
}
private String quoteParamWhenNotNull(String param) {
if (param != null ) {
return param != null ? "\"" + param + "\"" : "null";
}
return null;
}
}

View File

@ -19,7 +19,7 @@
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
*/
@ -33,11 +33,11 @@ public interface ConversionContext {
Type getTargetType();
/**
* Returns the date format if this conversion is from String to
* {@link Date} or vice versa. Returns {@code null} for other types or
* if not given.
* Returns the date format if this conversion or built-in method is from String to a date type (e.g. {@link Date})
* or vice versa.
*
* @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();

View File

@ -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.
*/
@Override
public boolean matches( Type sourceType, Type targetType ) {
public boolean matches(Type sourceType, Type targetType) {
if ( targetType.erasure().isAssignableTo( getReturnType().erasure() )
&& sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) {
&& sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) {
return doTypeVarsMatch( sourceType, targetType );
}
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
* implementors
*
* @return null
*/
@Override
@ -110,6 +111,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
/**
* target parameter mechanism not supported for build-in-method
*
* @return null
*/
@Override
@ -122,6 +124,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
* with context specific information such as a date format.
*
* @param conversionContext
*
* @return null if no context parameter should be included
* "null" if there should be an explicit null call
* "'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
*
* @param obj other class
*
* @return true when classes are the same
*/
@Override
public boolean equals( Object obj ) {
public boolean equals(Object obj) {
if ( obj == null ) {
return false;
}
@ -160,9 +164,10 @@ public abstract class BuiltInMethod extends ModelElement implements Method {
*
* @param parameter source
* @param returnType target
*
* @return
*/
public boolean doTypeVarsMatch( Type parameter, Type returnType ) {
public boolean doTypeVarsMatch(Type parameter, Type returnType) {
return true;
}

View File

@ -578,18 +578,20 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
targetType = typeFactory.getReturnType( targetAcessor );
}
// first try the SourceMethods
// first try the source methods
String mappedElement = "property '" + Executables.getPropertyName( sourceAccessor ) + "'";
MethodReference propertyMappingMethod = getMappingMethodReference(
getBestMatch( method, mappedElement, methods, sourceType, targetType ),
mapperReferences );
getBestMatch( method, mappedElement, methods, sourceType, targetType ),
mapperReferences
);
// then try BuiltInMethods
// then try built-in methods
if ( propertyMappingMethod == null ) {
propertyMappingMethod = getMappingMethodReference(
getBestMatch( method, mappedElement, builtInMethods.getBuiltInMethods(), sourceType, targetType ),
targetType,
dateFormat );
getBestMatch( method, mappedElement, builtInMethods.getBuiltInMethods(), sourceType, targetType ),
targetType,
dateFormat
);
}
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(
getBestMatch( method, "collection element", methods, sourceElementType, targetElementType ),
mapperReferences );
getBestMatch( method, "collection element", methods, sourceElementType, targetElementType ),
mapperReferences
);
// then try BuiltInMethods
// then try built-in methods
if ( elementMappingMethod == null ) {
elementMappingMethod = getMappingMethodReference(
getBestMatch( method, "collection element", builtInMethods.getBuiltInMethods(), sourceElementType,
targetElementType ),
targetElementType,
dateFormat );
getBestMatch(
method, "collection element", builtInMethods.getBuiltInMethods(), sourceElementType,
targetElementType
),
targetElementType,
dateFormat
);
}
if ( !sourceElementType.isAssignableTo( targetElementType ) && conversion == null &&
@ -693,29 +699,39 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
"entry.getValue()"
);
// first try the SourceMethods
// first try the source methods
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 ) {
keyMappingMethod = getMappingMethodReference(
getBestMatch( method, "map key", builtInMethods.getBuiltInMethods(), sourceKeyType, targetKeyType ),
targetKeyType,
keyDateFormat );
getBestMatch( method, "map key", builtInMethods.getBuiltInMethods(), sourceKeyType, targetKeyType ),
targetKeyType,
keyDateFormat
);
}
// first try the SourceMethods
// first try the source methods
MethodReference valueMappingMethod = getMappingMethodReference(
getBestMatch( method, "map value", methods, sourceValueType, targetValueType ),
mapperReferences );
getBestMatch( method, "map value", methods, sourceValueType, targetValueType ),
mapperReferences
);
// then try BuiltInMethods
// then try built-in methods
if ( valueMappingMethod == null ) {
valueMappingMethod = getMappingMethodReference(
getBestMatch( method, "map value", builtInMethods.getBuiltInMethods(), sourceValueType, targetValueType ),
targetValueType,
valueDateFormat );
getBestMatch(
method,
"map value",
builtInMethods.getBuiltInMethods(),
sourceValueType,
targetValueType
),
targetValueType,
valueDateFormat
);
}
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,
Iterable<T> methods, Type parameterType,
Type returnType) {
Iterable<T> methods, Type parameterType,
Type returnType) {
List<T> candidatesWithMathingTargetType = new ArrayList<T>();
for ( T method : methods ) {
@ -820,7 +835,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
private <T extends Method> int addToCandidateListIfMinimal(List<T> candidatesWithBestMathingType,
int bestMatchingTypeDistance, T method, int currentTypeDistance) {
int bestMatchingTypeDistance, T method,
int currentTypeDistance) {
if ( currentTypeDistance == bestMatchingTypeDistance ) {
candidatesWithBestMathingType.add( method );
}
@ -833,7 +849,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
return bestMatchingTypeDistance;
}
private MethodReference getMappingMethodReference( SourceMethod method, List<MapperReference> mapperReferences ) {
private MethodReference getMappingMethodReference(SourceMethod method, List<MapperReference> mapperReferences) {
if ( method != null ) {
MapperReference mapperReference = null;
for ( MapperReference ref : mapperReferences ) {
@ -847,7 +863,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
return null;
}
private MethodReference getMappingMethodReference( BuiltInMethod method, Type returnType, String dateFormat ) {
private MethodReference getMappingMethodReference(BuiltInMethod method, Type returnType, String dateFormat) {
if ( method != null ) {
virtualMethods.add( new VirtualMappingMethod( method ) );
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
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.
*