Search in sources :

Example 11 with Parameter

use of org.mapstruct.ap.internal.model.common.Parameter 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<ParameterBinding>(sourceTypes.size() + 2);
    addMappingTargetAndTargetTypeBindings(availableParams, targetType);
    for (Type sourceType : sourceTypes) {
        availableParams.add(ParameterBinding.forSourceTypeBinding(sourceType));
    }
    for (Parameter param : mappingMethod.getParameters()) {
        if (param.isMappingContext()) {
            availableParams.add(ParameterBinding.fromParameter(param));
        }
    }
    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 12 with Parameter

use of org.mapstruct.ap.internal.model.common.Parameter 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 targetType, SelectionCriteria criteria) {
    if (sourceTypes.size() != 1) {
        return methods;
    }
    Type singleSourceType = first(sourceTypes);
    List<SelectedMethod<T>> candidatesWithBestMatchingSourceType = new ArrayList<SelectedMethod<T>>();
    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 13 with Parameter

use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method isValidReferencedOrFactoryMethod.

private boolean isValidReferencedOrFactoryMethod(int sourceParamCount, int targetParamCount, List<Parameter> parameters) {
    int validSourceParameters = 0;
    int targetParameters = 0;
    int targetTypeParameters = 0;
    for (Parameter param : parameters) {
        if (param.isMappingTarget()) {
            targetParameters++;
        } else if (param.isTargetType()) {
            targetTypeParameters++;
        } else if (!param.isMappingContext()) {
            validSourceParameters++;
        }
    }
    return validSourceParameters == sourceParamCount && targetParameters <= targetParamCount && targetTypeParameters <= 1;
}
Also used : Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 14 with Parameter

use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method retrievePrototypeMethods.

private List<SourceMethod> retrievePrototypeMethods(TypeElement mapperTypeElement, MapperConfiguration mapperConfig) {
    if (mapperConfig.config() == null) {
        return Collections.emptyList();
    }
    TypeElement typeElement = asTypeElement(mapperConfig.config());
    List<SourceMethod> methods = new ArrayList<SourceMethod>();
    for (ExecutableElement executable : getAllEnclosedExecutableElements(elementUtils, typeElement)) {
        ExecutableType methodType = typeFactory.getMethodType(mapperConfig.config(), executable);
        List<Parameter> parameters = typeFactory.getParameters(methodType, executable);
        boolean containsTargetTypeParameter = SourceMethod.containsTargetTypeParameter(parameters);
        // prototype methods don't have prototypes themselves
        List<SourceMethod> prototypeMethods = Collections.emptyList();
        SourceMethod method = getMethodRequiringImplementation(methodType, executable, parameters, containsTargetTypeParameter, mapperConfig, prototypeMethods, mapperTypeElement);
        if (method != null) {
            methods.add(method);
        }
    }
    return methods;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 15 with Parameter

use of org.mapstruct.ap.internal.model.common.Parameter in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method getMethod.

private SourceMethod getMethod(TypeElement usedMapper, ExecutableElement method, TypeElement mapperToImplement, MapperConfiguration mapperConfig, List<SourceMethod> prototypeMethods) {
    ExecutableType methodType = typeFactory.getMethodType((DeclaredType) usedMapper.asType(), method);
    List<Parameter> parameters = typeFactory.getParameters(methodType, method);
    Type returnType = typeFactory.getReturnType(methodType);
    boolean methodRequiresImplementation = method.getModifiers().contains(Modifier.ABSTRACT);
    boolean containsTargetTypeParameter = SourceMethod.containsTargetTypeParameter(parameters);
    // add method with property mappings if an implementation needs to be generated
    if ((usedMapper.equals(mapperToImplement)) && methodRequiresImplementation) {
        return getMethodRequiringImplementation(methodType, method, parameters, containsTargetTypeParameter, mapperConfig, prototypeMethods, mapperToImplement);
    } else // otherwise add reference to existing mapper method
    if (isValidReferencedMethod(parameters) || isValidFactoryMethod(method, parameters, returnType) || isValidLifecycleCallbackMethod(method, returnType)) {
        return getReferencedMethod(usedMapper, methodType, method, mapperToImplement, parameters);
    } else {
        return null;
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Aggregations

Parameter (org.mapstruct.ap.internal.model.common.Parameter)16 ArrayList (java.util.ArrayList)8 Type (org.mapstruct.ap.internal.model.common.Type)8 DeclaredType (javax.lang.model.type.DeclaredType)4 ExecutableType (javax.lang.model.type.ExecutableType)4 HashSet (java.util.HashSet)3 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)3 ParameterBinding (org.mapstruct.ap.internal.model.common.ParameterBinding)2 ParameterProvidedMethods (org.mapstruct.ap.internal.model.source.ParameterProvidedMethods)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 ArrayType (javax.lang.model.type.ArrayType)1 PrimitiveType (javax.lang.model.type.PrimitiveType)1 TypeMirror (javax.lang.model.type.TypeMirror)1 TypeVariable (javax.lang.model.type.TypeVariable)1 WildcardType (javax.lang.model.type.WildcardType)1 PropertyEntry.forSourceReference (org.mapstruct.ap.internal.model.source.PropertyEntry.forSourceReference)1