Search in sources :

Example 26 with Type

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;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type)

Example 27 with Type

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;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) ParameterBinding(org.mapstruct.ap.internal.model.common.ParameterBinding)

Example 28 with Type

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;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 29 with Type

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;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 30 with Type

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;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) TreeSet(java.util.TreeSet)

Aggregations

Type (org.mapstruct.ap.internal.model.common.Type)36 ArrayList (java.util.ArrayList)13 Parameter (org.mapstruct.ap.internal.model.common.Parameter)11 DeclaredType (javax.lang.model.type.DeclaredType)10 ExecutableType (javax.lang.model.type.ExecutableType)6 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)6 HashSet (java.util.HashSet)5 TypeMirror (javax.lang.model.type.TypeMirror)4 TreeSet (java.util.TreeSet)3 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)3 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)3 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)3 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)3 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)3 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)3 ValueMappingMethod (org.mapstruct.ap.internal.model.ValueMappingMethod)3 Map (java.util.Map)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 ArrayType (javax.lang.model.type.ArrayType)2