Search in sources :

Example 6 with SourceMethod

use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.

the class MapperCreationProcessor method getTemplateMappingOptions.

/**
 * Returns the configuring forward method's options in case the given method is annotated with
 * {@code @InheritConfiguration} and exactly one such configuring method can unambiguously be selected (as per the
 * source/target type and optionally the name given via {@code @InheritConfiguration}). The method cannot be marked
 * forward mapping itself (hence 'other'). And neither can it contain an {@code @InheritReverseConfiguration}
 */
private MappingOptions getTemplateMappingOptions(List<SourceMethod> rawMethods, SourceMethod method, List<SourceMethod> initializingMethods, MapperConfiguration mapperConfig) {
    SourceMethod resultMethod = null;
    InheritConfigurationPrism forwardPrism = InheritConfigurationPrism.getInstanceOn(method.getExecutable());
    if (forwardPrism != null) {
        List<SourceMethod> candidates = new ArrayList<SourceMethod>();
        for (SourceMethod oneMethod : rawMethods) {
            // method must be similar but not equal
            if (method.canInheritFrom(oneMethod) && !(oneMethod.equals(method))) {
                candidates.add(oneMethod);
            }
        }
        String name = forwardPrism.name();
        if (candidates.size() == 1) {
            // no ambiguity: if no configuredBy is specified, or configuredBy specified and match
            SourceMethod sourceMethod = first(candidates);
            if (name.isEmpty()) {
                resultMethod = sourceMethod;
            } else if (sourceMethod.getName().equals(name)) {
                resultMethod = sourceMethod;
            } else {
                reportErrorWhenNonMatchingName(sourceMethod, method, forwardPrism);
            }
        } else if (candidates.size() > 1) {
            // ambiguity: find a matching method that matches configuredBy
            List<SourceMethod> nameFilteredcandidates = new ArrayList<SourceMethod>();
            for (SourceMethod candidate : candidates) {
                if (candidate.getName().equals(name)) {
                    nameFilteredcandidates.add(candidate);
                }
            }
            if (nameFilteredcandidates.size() == 1) {
                resultMethod = first(nameFilteredcandidates);
            } else if (nameFilteredcandidates.size() > 1) {
                reportErrorWhenSeveralNamesMatch(nameFilteredcandidates, method, forwardPrism);
            } else {
                reportErrorWhenAmbigousMapping(candidates, method, forwardPrism);
            }
        }
    }
    return extractInitializedOptions(resultMethod, rawMethods, mapperConfig, initializingMethods);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) InheritConfigurationPrism(org.mapstruct.ap.internal.prism.InheritConfigurationPrism)

Example 7 with SourceMethod

use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method retrieveLifecycleMethodsFromContext.

private ParameterProvidedMethods retrieveLifecycleMethodsFromContext(List<Parameter> contextParameters, TypeElement mapperToImplement, MapperConfiguration mapperConfig) {
    ParameterProvidedMethods.Builder builder = ParameterProvidedMethods.builder();
    for (Parameter contextParam : contextParameters) {
        if (contextParam.getType().isPrimitive()) {
            continue;
        }
        List<SourceMethod> contextParamMethods = retrieveMethods(contextParam.getType().getTypeElement(), mapperToImplement, mapperConfig, Collections.<SourceMethod>emptyList());
        List<SourceMethod> lifecycleMethods = new ArrayList<SourceMethod>(contextParamMethods.size());
        for (SourceMethod sourceMethod : contextParamMethods) {
            if (sourceMethod.isLifecycleCallbackMethod()) {
                lifecycleMethods.add(sourceMethod);
            }
        }
        builder.addMethodsForParameter(contextParam, lifecycleMethods);
    }
    return builder.build();
}
Also used : ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 8 with SourceMethod

use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method getReferencedMethod.

private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType, ExecutableElement method, TypeElement mapperToImplement, List<Parameter> parameters) {
    Type returnType = typeFactory.getReturnType(methodType);
    List<Type> exceptionTypes = typeFactory.getThrownTypes(methodType);
    Type usedMapperAsType = typeFactory.getType(usedMapper);
    Type mapperToImplementAsType = typeFactory.getType(mapperToImplement);
    if (!mapperToImplementAsType.canAccess(usedMapperAsType, method)) {
        return null;
    }
    Type definingType = typeFactory.getType(method.getEnclosingElement().asType());
    return new SourceMethod.Builder().setDeclaringMapper(usedMapper.equals(mapperToImplement) ? null : usedMapperAsType).setDefininingType(definingType).setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setTypeUtils(typeUtils).setTypeFactory(typeFactory).build();
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 9 with SourceMethod

use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.

the class XmlElementDeclSelector method getMatchingMethods.

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod, List<SelectedMethod<T>> methods, List<Type> sourceTypes, Type targetType, SelectionCriteria criteria) {
    List<SelectedMethod<T>> nameMatches = new ArrayList<SelectedMethod<T>>();
    List<SelectedMethod<T>> scopeMatches = new ArrayList<SelectedMethod<T>>();
    List<SelectedMethod<T>> nameAndScopeMatches = new ArrayList<SelectedMethod<T>>();
    XmlElementRefInfo xmlElementRefInfo = findXmlElementRef(mappingMethod.getResultType(), criteria.getTargetPropertyName());
    for (SelectedMethod<T> candidate : methods) {
        if (!(candidate.getMethod() instanceof SourceMethod)) {
            continue;
        }
        SourceMethod candidateMethod = (SourceMethod) candidate.getMethod();
        XmlElementDeclPrism xmlElememtDecl = XmlElementDeclPrism.getInstanceOn(candidateMethod.getExecutable());
        if (xmlElememtDecl == null) {
            continue;
        }
        String name = xmlElememtDecl.name();
        TypeMirror scope = xmlElememtDecl.scope();
        boolean nameIsSetAndMatches = name != null && name.equals(xmlElementRefInfo.nameValue());
        boolean scopeIsSetAndMatches = scope != null && typeUtils.isSameType(scope, xmlElementRefInfo.sourceType());
        if (nameIsSetAndMatches) {
            if (scopeIsSetAndMatches) {
                nameAndScopeMatches.add(candidate);
            } else {
                nameMatches.add(candidate);
            }
        } else if (scopeIsSetAndMatches) {
            scopeMatches.add(candidate);
        }
    }
    if (nameAndScopeMatches.size() > 0) {
        return nameAndScopeMatches;
    } else if (scopeMatches.size() > 0) {
        return scopeMatches;
    } else if (nameMatches.size() > 0) {
        return nameMatches;
    } else {
        return methods;
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) XmlElementDeclPrism(org.mapstruct.ap.internal.prism.XmlElementDeclPrism)

Example 10 with SourceMethod

use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.

the class LifecycleCallbackFactory method toLifecycleCallbackMethodRefs.

private static List<LifecycleCallbackMethodReference> toLifecycleCallbackMethodRefs(Method method, List<SelectedMethod<SourceMethod>> candidates, MappingBuilderContext ctx, Set<String> existingVariableNames) {
    List<LifecycleCallbackMethodReference> result = new ArrayList<LifecycleCallbackMethodReference>();
    for (SelectedMethod<SourceMethod> candidate : candidates) {
        Parameter providingParameter = method.getContextProvidedMethods().getParameterForProvidedMethod(candidate.getMethod());
        if (providingParameter != null) {
            result.add(LifecycleCallbackMethodReference.forParameterProvidedMethod(candidate, providingParameter, method, existingVariableNames));
        } else {
            MapperReference mapperReference = findMapperReference(ctx.getMapperReferences(), candidate.getMethod());
            result.add(LifecycleCallbackMethodReference.forMethodReference(candidate, mapperReference, method, existingVariableNames));
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)16 ArrayList (java.util.ArrayList)12 DeclaredType (javax.lang.model.type.DeclaredType)3 Parameter (org.mapstruct.ap.internal.model.common.Parameter)3 Type (org.mapstruct.ap.internal.model.common.Type)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 ExecutableType (javax.lang.model.type.ExecutableType)2 TypeMirror (javax.lang.model.type.TypeMirror)2 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)2 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)2 ContainerMappingMethodBuilder (org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder)2 EnumMappingMethod (org.mapstruct.ap.internal.model.EnumMappingMethod)2 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)2 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)2 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)2 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)2