Search in sources :

Example 1 with MappingInheritanceStrategyGem

use of org.mapstruct.ap.internal.gem.MappingInheritanceStrategyGem in project mapstruct by mapstruct.

the class MapperCreationProcessor method mergeInheritedOptions.

private void mergeInheritedOptions(SourceMethod method, MapperOptions mapperConfig, List<SourceMethod> availableMethods, List<SourceMethod> initializingMethods, AnnotationMirror annotationMirror) {
    if (initializingMethods.contains(method)) {
        // cycle detected
        initializingMethods.add(method);
        messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_CYCLE, Strings.join(initializingMethods, " -> "));
        return;
    }
    initializingMethods.add(method);
    MappingMethodOptions mappingOptions = method.getOptions();
    List<SourceMethod> applicableReversePrototypeMethods = method.getApplicableReversePrototypeMethods();
    SourceMethod inverseTemplateMethod = getInverseTemplateMethod(join(availableMethods, applicableReversePrototypeMethods), method, initializingMethods, mapperConfig);
    List<SourceMethod> applicablePrototypeMethods = method.getApplicablePrototypeMethods();
    SourceMethod forwardTemplateMethod = getForwardTemplateMethod(join(availableMethods, applicablePrototypeMethods), method, initializingMethods, mapperConfig);
    // apply defined (@InheritConfiguration, @InheritInverseConfiguration) mappings
    if (forwardTemplateMethod != null) {
        mappingOptions.applyInheritedOptions(method, forwardTemplateMethod, false, annotationMirror);
    }
    if (inverseTemplateMethod != null) {
        mappingOptions.applyInheritedOptions(method, inverseTemplateMethod, true, annotationMirror);
    }
    // apply auto inherited options
    MappingInheritanceStrategyGem inheritanceStrategy = mapperConfig.getMappingInheritanceStrategy();
    if (inheritanceStrategy.isAutoInherit()) {
        // but.. there should not be an @InheritedConfiguration
        if (forwardTemplateMethod == null && inheritanceStrategy.isApplyForward()) {
            if (applicablePrototypeMethods.size() == 1) {
                mappingOptions.applyInheritedOptions(method, first(applicablePrototypeMethods), false, annotationMirror);
            } else if (applicablePrototypeMethods.size() > 1) {
                messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicablePrototypeMethods, ", "));
            }
        }
        // or no @InheritInverseConfiguration
        if (inverseTemplateMethod == null && inheritanceStrategy.isApplyReverse()) {
            if (applicableReversePrototypeMethods.size() == 1) {
                mappingOptions.applyInheritedOptions(method, first(applicableReversePrototypeMethods), true, annotationMirror);
            } else if (applicableReversePrototypeMethods.size() > 1) {
                messager.printMessage(method.getExecutable(), Message.INHERITINVERSECONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicableReversePrototypeMethods, ", "));
            }
        }
    }
    // @BeanMapping( ignoreByDefault = true )
    if (mappingOptions.getBeanMapping() != null && mappingOptions.getBeanMapping().isignoreByDefault()) {
        mappingOptions.applyIgnoreAll(method, typeFactory, mappingContext.getMessager());
    }
    mappingOptions.markAsFullyInitialized();
}
Also used : MappingMethodOptions(org.mapstruct.ap.internal.model.source.MappingMethodOptions) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) MappingInheritanceStrategyGem(org.mapstruct.ap.internal.gem.MappingInheritanceStrategyGem)

Aggregations

MappingInheritanceStrategyGem (org.mapstruct.ap.internal.gem.MappingInheritanceStrategyGem)1 MappingMethodOptions (org.mapstruct.ap.internal.model.source.MappingMethodOptions)1 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)1