use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.
the class MappingMethodUtils method isEnumMapping.
/**
* Checks if the provided {@code method} is for enum mapping. A Method is an Enum Mapping method when the
* <ol>
* <li>source parameter type and result type are enum types</li>
* <li>source parameter type is a String and result type is an enum type</li>
* <li>source parameter type is a enum type and result type is a String</li>
* </ol>
*
* @param method to check
*
* @return {@code true} if the method is for enum mapping, {@code false} otherwise
*/
public static boolean isEnumMapping(Method method) {
if (method.getSourceParameters().size() != 1) {
return false;
}
Type source = first(method.getSourceParameters()).getType();
Type result = method.getResultType();
if (source.isEnumType() && result.isEnumType()) {
return true;
}
if (source.isString() && result.isEnumType()) {
return true;
}
if (source.isEnumType() && result.isString()) {
return true;
}
return false;
}
use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.
the class TypeSelector method getAvailableParameterBindingsFromSourceTypes.
private List<ParameterBinding> getAvailableParameterBindingsFromSourceTypes(List<Type> sourceTypes, Type targetType, Method mappingMethod) {
List<ParameterBinding> availableParams = new ArrayList<>(sourceTypes.size() + 2);
for (Type sourceType : sourceTypes) {
availableParams.add(ParameterBinding.forSourceTypeBinding(sourceType));
}
for (Parameter param : mappingMethod.getParameters()) {
if (param.isMappingContext()) {
availableParams.add(ParameterBinding.fromParameter(param));
}
}
addMappingTargetAndTargetTypeBindings(availableParams, targetType);
return availableParams;
}
use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.
the class InheritanceSelector method getMatchingMethods.
@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod, List<SelectedMethod<T>> methods, List<Type> sourceTypes, Type mappingTargetType, Type returnType, SelectionCriteria criteria) {
if (sourceTypes.size() != 1) {
return methods;
}
Type singleSourceType = first(sourceTypes);
List<SelectedMethod<T>> candidatesWithBestMatchingSourceType = new ArrayList<>();
int bestMatchingSourceTypeDistance = Integer.MAX_VALUE;
// find the methods with the minimum distance regarding getParameter getParameter type
for (SelectedMethod<T> method : methods) {
Parameter singleSourceParam = first(method.getMethod().getSourceParameters());
int sourceTypeDistance = singleSourceType.distanceTo(singleSourceParam.getType());
bestMatchingSourceTypeDistance = addToCandidateListIfMinimal(candidatesWithBestMatchingSourceType, bestMatchingSourceTypeDistance, method, sourceTypeDistance);
}
return candidatesWithBestMatchingSourceType;
}
use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.
the class ForgedMethod method matches.
@Override
public boolean matches(List<Type> sourceTypes, Type targetType) {
if (!targetType.equals(returnType)) {
return false;
}
if (parameters.size() != sourceTypes.size()) {
return false;
}
Iterator<Type> srcTypeIt = sourceTypes.iterator();
Iterator<Parameter> paramIt = parameters.iterator();
while (srcTypeIt.hasNext() && paramIt.hasNext()) {
Type sourceType = srcTypeIt.next();
Parameter param = paramIt.next();
if (!sourceType.equals(param.getType())) {
return false;
}
}
return true;
}
use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.
the class GeneratedType method getImportTypes.
@Override
public SortedSet<Type> getImportTypes() {
SortedSet<Type> importedTypes = new TreeSet<>();
addIfImportRequired(importedTypes, generatedType);
addIfImportRequired(importedTypes, mapperDefinitionType);
for (MappingMethod mappingMethod : methods) {
for (Type type : mappingMethod.getImportTypes()) {
addIfImportRequired(importedTypes, type);
}
}
for (Field field : fields) {
if (field.isTypeRequiresImport()) {
for (Type type : field.getImportTypes()) {
addIfImportRequired(importedTypes, type);
}
}
}
for (Annotation annotation : annotations) {
addIfImportRequired(importedTypes, annotation.getType());
}
for (Type extraImport : extraImportedTypes) {
addIfImportRequired(importedTypes, extraImport);
}
if (constructor != null) {
for (Type type : constructor.getImportTypes()) {
addIfImportRequired(importedTypes, type);
}
}
return importedTypes;
}
Aggregations