Search in sources :

Example 1 with MappingMethod

use of org.mapstruct.ap.internal.model.MappingMethod in project mapstruct by mapstruct.

the class MapperCreationProcessor method getMapper.

private Mapper getMapper(TypeElement element, MapperConfiguration mapperConfig, List<SourceMethod> methods) {
    List<MapperReference> mapperReferences = mappingContext.getMapperReferences();
    List<MappingMethod> mappingMethods = getMappingMethods(mapperConfig, methods);
    mappingMethods.addAll(mappingContext.getUsedVirtualMappings());
    mappingMethods.addAll(mappingContext.getMappingsToGenerate());
    Mapper mapper = new Mapper.Builder().element(element).mappingMethods(mappingMethods).mapperReferences(mapperReferences).options(options).versionInformation(versionInformation).decorator(getDecorator(element, methods, mapperConfig.implementationName(), mapperConfig.implementationPackage())).typeFactory(typeFactory).elementUtils(elementUtils).extraImports(getExtraImports(element)).implName(mapperConfig.implementationName()).implPackage(mapperConfig.implementationPackage()).build();
    if (!mappingContext.getForgedMethodsUnderCreation().isEmpty()) {
        messager.printMessage(element, Message.GENERAL_NOT_ALL_FORGED_CREATED, mappingContext.getForgedMethodsUnderCreation().keySet());
    }
    return mapper;
}
Also used : Mapper(org.mapstruct.ap.internal.model.Mapper) DefaultMapperReference(org.mapstruct.ap.internal.model.DefaultMapperReference) MapperReference(org.mapstruct.ap.internal.model.MapperReference) ContainerMappingMethodBuilder(org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) EnumMappingMethod(org.mapstruct.ap.internal.model.EnumMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) MappingMethod(org.mapstruct.ap.internal.model.MappingMethod) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) ContainerMappingMethod(org.mapstruct.ap.internal.model.ContainerMappingMethod) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod)

Example 2 with MappingMethod

use of org.mapstruct.ap.internal.model.MappingMethod in project mapstruct by mapstruct.

the class MapperCreationProcessor method getMappingMethods.

private List<MappingMethod> getMappingMethods(MapperConfiguration mapperConfig, List<SourceMethod> methods) {
    List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
    for (SourceMethod method : methods) {
        if (!method.overridesMethod()) {
            continue;
        }
        mergeInheritedOptions(method, mapperConfig, methods, new ArrayList<SourceMethod>());
        MappingOptions mappingOptions = method.getMappingOptions();
        boolean hasFactoryMethod = false;
        if (method.isIterableMapping()) {
            IterableMappingMethod iterableMappingMethod = createWithElementMappingMethod(method, mappingOptions, new IterableMappingMethod.Builder());
            hasFactoryMethod = iterableMappingMethod.getFactoryMethod() != null;
            mappingMethods.add(iterableMappingMethod);
        } else if (method.isMapMapping()) {
            MapMappingMethod.Builder builder = new MapMappingMethod.Builder();
            SelectionParameters keySelectionParameters = null;
            FormattingParameters keyFormattingParameters = null;
            SelectionParameters valueSelectionParameters = null;
            FormattingParameters valueFormattingParameters = null;
            NullValueMappingStrategyPrism nullValueMappingStrategy = null;
            if (mappingOptions.getMapMapping() != null) {
                keySelectionParameters = mappingOptions.getMapMapping().getKeySelectionParameters();
                keyFormattingParameters = mappingOptions.getMapMapping().getKeyFormattingParameters();
                valueSelectionParameters = mappingOptions.getMapMapping().getValueSelectionParameters();
                valueFormattingParameters = mappingOptions.getMapMapping().getValueFormattingParameters();
                nullValueMappingStrategy = mappingOptions.getMapMapping().getNullValueMappingStrategy();
            }
            MapMappingMethod mapMappingMethod = builder.mappingContext(mappingContext).method(method).keyFormattingParameters(keyFormattingParameters).keySelectionParameters(keySelectionParameters).valueFormattingParameters(valueFormattingParameters).valueSelectionParameters(valueSelectionParameters).nullValueMappingStrategy(nullValueMappingStrategy).build();
            hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
            mappingMethods.add(mapMappingMethod);
        } else if (method.isValueMapping()) {
            // prefer value mappings over enum mapping
            ValueMappingMethod valueMappingMethod = new ValueMappingMethod.Builder().mappingContext(mappingContext).method(method).valueMappings(mappingOptions.getValueMappings()).build();
            mappingMethods.add(valueMappingMethod);
        } else if (method.isEnumMapping()) {
            messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_DEPRECATED);
            EnumMappingMethod.Builder builder = new EnumMappingMethod.Builder();
            MappingMethod enumMappingMethod = builder.mappingContext(mappingContext).souceMethod(method).build();
            if (enumMappingMethod != null) {
                mappingMethods.add(enumMappingMethod);
            }
        } else if (method.isStreamMapping()) {
            StreamMappingMethod streamMappingMethod = createWithElementMappingMethod(method, mappingOptions, new StreamMappingMethod.Builder());
            // If we do StreamMapping that means that internally there is a way to generate the result type
            hasFactoryMethod = streamMappingMethod.getFactoryMethod() != null || method.getResultType().isStreamType();
            mappingMethods.add(streamMappingMethod);
        } else {
            NullValueMappingStrategyPrism nullValueMappingStrategy = null;
            SelectionParameters selectionParameters = null;
            if (mappingOptions.getBeanMapping() != null) {
                nullValueMappingStrategy = mappingOptions.getBeanMapping().getNullValueMappingStrategy();
                selectionParameters = mappingOptions.getBeanMapping().getSelectionParameters();
            }
            BeanMappingMethod.Builder builder = new BeanMappingMethod.Builder();
            BeanMappingMethod beanMappingMethod = builder.mappingContext(mappingContext).souceMethod(method).nullValueMappingStrategy(nullValueMappingStrategy).selectionParameters(selectionParameters).build();
            if (beanMappingMethod != null) {
                // We can consider that the bean mapping method can always be constructed. If there is a problem
                // it would have been reported in its build
                hasFactoryMethod = true;
                mappingMethods.add(beanMappingMethod);
            }
        }
        if (!hasFactoryMethod) {
            // A factory method  is allowed to return an interface type and hence, the generated
            // implementation as well. The check below must only be executed if there's no factory
            // method that could be responsible.
            reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(method);
        }
    }
    return mappingMethods;
}
Also used : MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) ContainerMappingMethodBuilder(org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) EnumMappingMethod(org.mapstruct.ap.internal.model.EnumMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) MappingMethod(org.mapstruct.ap.internal.model.MappingMethod) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) ContainerMappingMethod(org.mapstruct.ap.internal.model.ContainerMappingMethod) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) ArrayList(java.util.ArrayList) NullValueMappingStrategyPrism(org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) MappingOptions(org.mapstruct.ap.internal.model.source.MappingOptions) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters) SelectionParameters(org.mapstruct.ap.internal.model.source.SelectionParameters) EnumMappingMethod(org.mapstruct.ap.internal.model.EnumMappingMethod) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 3 with MappingMethod

use of org.mapstruct.ap.internal.model.MappingMethod in project mapstruct by mapstruct.

the class MapperCreationProcessor method getDecorator.

private Decorator getDecorator(TypeElement element, List<SourceMethod> methods, String implName, String implPackage) {
    DecoratedWithPrism decoratorPrism = DecoratedWithPrism.getInstanceOn(element);
    if (decoratorPrism == null) {
        return null;
    }
    TypeElement decoratorElement = (TypeElement) typeUtils.asElement(decoratorPrism.value());
    if (!typeUtils.isAssignable(decoratorElement.asType(), element.asType())) {
        messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_NO_SUBTYPE);
    }
    List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>(methods.size());
    for (SourceMethod mappingMethod : methods) {
        boolean implementationRequired = true;
        for (ExecutableElement method : ElementFilter.methodsIn(decoratorElement.getEnclosedElements())) {
            if (elementUtils.overrides(method, mappingMethod.getExecutable(), decoratorElement)) {
                implementationRequired = false;
                break;
            }
        }
        Type declaringMapper = mappingMethod.getDeclaringMapper();
        if (implementationRequired && !(mappingMethod.isDefault() || mappingMethod.isStatic())) {
            if ((declaringMapper == null) || declaringMapper.equals(typeFactory.getType(element))) {
                mappingMethods.add(new DelegatingMethod(mappingMethod));
            }
        }
    }
    boolean hasDelegateConstructor = false;
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor : ElementFilter.constructorsIn(decoratorElement.getEnclosedElements())) {
        if (constructor.getParameters().isEmpty()) {
            hasDefaultConstructor = true;
        } else if (constructor.getParameters().size() == 1) {
            if (typeUtils.isAssignable(element.asType(), first(constructor.getParameters()).asType())) {
                hasDelegateConstructor = true;
            }
        }
    }
    if (!hasDelegateConstructor && !hasDefaultConstructor) {
        messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR);
    }
    Decorator decorator = new Decorator.Builder().elementUtils(elementUtils).typeFactory(typeFactory).mapperElement(element).decoratorPrism(decoratorPrism).methods(mappingMethods).hasDelegateConstructor(hasDelegateConstructor).options(options).versionInformation(versionInformation).implName(implName).implPackage(implPackage).extraImports(getExtraImports(element)).build();
    return decorator;
}
Also used : Decorator(org.mapstruct.ap.internal.model.Decorator) Type(org.mapstruct.ap.internal.model.common.Type) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ContainerMappingMethodBuilder(org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) EnumMappingMethod(org.mapstruct.ap.internal.model.EnumMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) MappingMethod(org.mapstruct.ap.internal.model.MappingMethod) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) ContainerMappingMethod(org.mapstruct.ap.internal.model.ContainerMappingMethod) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) ArrayList(java.util.ArrayList) DelegatingMethod(org.mapstruct.ap.internal.model.DelegatingMethod) DecoratedWithPrism(org.mapstruct.ap.internal.prism.DecoratedWithPrism) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)3 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)3 ContainerMappingMethodBuilder (org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder)3 EnumMappingMethod (org.mapstruct.ap.internal.model.EnumMappingMethod)3 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)3 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)3 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)3 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)3 ValueMappingMethod (org.mapstruct.ap.internal.model.ValueMappingMethod)3 ArrayList (java.util.ArrayList)2 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)2 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 Decorator (org.mapstruct.ap.internal.model.Decorator)1 DefaultMapperReference (org.mapstruct.ap.internal.model.DefaultMapperReference)1 DelegatingMethod (org.mapstruct.ap.internal.model.DelegatingMethod)1 Mapper (org.mapstruct.ap.internal.model.Mapper)1 MapperReference (org.mapstruct.ap.internal.model.MapperReference)1 FormattingParameters (org.mapstruct.ap.internal.model.common.FormattingParameters)1 Type (org.mapstruct.ap.internal.model.common.Type)1