Search in sources :

Example 1 with InheritInverseConfigurationPrism

use of org.mapstruct.ap.internal.prism.InheritInverseConfigurationPrism in project mapstruct by mapstruct.

the class MapperCreationProcessor method getInverseMappingOptions.

/**
 * Returns the configuring inverse method's options in case the given method is annotated with
 * {@code @InheritInverseConfiguration} and exactly one such configuring method can unambiguously be selected (as
 * per the source/target type and optionally the name given via {@code @InheritInverseConfiguration}).
 */
private MappingOptions getInverseMappingOptions(List<SourceMethod> rawMethods, SourceMethod method, List<SourceMethod> initializingMethods, MapperConfiguration mapperConfig) {
    SourceMethod resultMethod = null;
    InheritInverseConfigurationPrism reversePrism = InheritInverseConfigurationPrism.getInstanceOn(method.getExecutable());
    if (reversePrism != null) {
        // method is configured as being reverse method, collect candidates
        List<SourceMethod> candidates = new ArrayList<SourceMethod>();
        for (SourceMethod oneMethod : rawMethods) {
            if (method.reverses(oneMethod)) {
                candidates.add(oneMethod);
            }
        }
        String name = reversePrism.name();
        if (candidates.size() == 1) {
            // no ambiguity: if no configuredBy is specified, or configuredBy specified and match
            if (name.isEmpty()) {
                resultMethod = candidates.get(0);
            } else if (candidates.get(0).getName().equals(name)) {
                resultMethod = candidates.get(0);
            } else {
                reportErrorWhenNonMatchingName(candidates.get(0), method, reversePrism);
            }
        } 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 = nameFilteredcandidates.get(0);
            } else if (nameFilteredcandidates.size() > 1) {
                reportErrorWhenSeveralNamesMatch(nameFilteredcandidates, method, reversePrism);
            } else {
                reportErrorWhenAmbigousReverseMapping(candidates, method, reversePrism);
            }
        }
    }
    return extractInitializedOptions(resultMethod, rawMethods, mapperConfig, initializingMethods);
}
Also used : ArrayList(java.util.ArrayList) InheritInverseConfigurationPrism(org.mapstruct.ap.internal.prism.InheritInverseConfigurationPrism) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)1 InheritInverseConfigurationPrism (org.mapstruct.ap.internal.prism.InheritInverseConfigurationPrism)1