Search in sources :

Example 1 with MappingInheritanceStrategyPrism

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

the class MapperCreationProcessor method mergeInheritedOptions.

private void mergeInheritedOptions(SourceMethod method, MapperConfiguration mapperConfig, List<SourceMethod> availableMethods, List<SourceMethod> initializingMethods) {
    if (initializingMethods.contains(method)) {
        // cycle detected
        initializingMethods.add(method);
        messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_CYCLE, Strings.join(initializingMethods, " -> "));
        return;
    }
    initializingMethods.add(method);
    MappingOptions mappingOptions = method.getMappingOptions();
    List<SourceMethod> applicableReversePrototypeMethods = method.getApplicableReversePrototypeMethods();
    MappingOptions inverseMappingOptions = getInverseMappingOptions(join(availableMethods, applicableReversePrototypeMethods), method, initializingMethods, mapperConfig);
    List<SourceMethod> applicablePrototypeMethods = method.getApplicablePrototypeMethods();
    MappingOptions forwardMappingOptions = getTemplateMappingOptions(join(availableMethods, applicablePrototypeMethods), method, initializingMethods, mapperConfig);
    // apply defined (@InheritConfiguration, @InheritInverseConfiguration) mappings
    if (forwardMappingOptions != null) {
        mappingOptions.applyInheritedOptions(forwardMappingOptions, false, method, messager, typeFactory);
    }
    if (inverseMappingOptions != null) {
        mappingOptions.applyInheritedOptions(inverseMappingOptions, true, method, messager, typeFactory);
    }
    // apply auto inherited options
    MappingInheritanceStrategyPrism inheritanceStrategy = mapperConfig.getMappingInheritanceStrategy();
    if (inheritanceStrategy.isAutoInherit()) {
        // but.. there should not be an @InheritedConfiguration
        if (forwardMappingOptions == null && inheritanceStrategy.isApplyForward()) {
            if (applicablePrototypeMethods.size() == 1) {
                mappingOptions.applyInheritedOptions(first(applicablePrototypeMethods).getMappingOptions(), false, method, messager, typeFactory);
            } else if (applicablePrototypeMethods.size() > 1) {
                messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicablePrototypeMethods, ", "));
            }
        }
        // or no @InheritInverseConfiguration
        if (inverseMappingOptions == null && inheritanceStrategy.isApplyReverse()) {
            if (applicableReversePrototypeMethods.size() == 1) {
                mappingOptions.applyInheritedOptions(first(applicableReversePrototypeMethods).getMappingOptions(), true, method, messager, typeFactory);
            } else if (applicableReversePrototypeMethods.size() > 1) {
                messager.printMessage(method.getExecutable(), Message.INHERITINVERSECONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicableReversePrototypeMethods, ", "));
            }
        }
    }
    mappingOptions.markAsFullyInitialized();
}
Also used : MappingOptions(org.mapstruct.ap.internal.model.source.MappingOptions) MappingInheritanceStrategyPrism(org.mapstruct.ap.internal.prism.MappingInheritanceStrategyPrism) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

MappingOptions (org.mapstruct.ap.internal.model.source.MappingOptions)1 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)1 MappingInheritanceStrategyPrism (org.mapstruct.ap.internal.prism.MappingInheritanceStrategyPrism)1