Search in sources :

Example 31 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class ContainerMappingMethodBuilder method build.

@Override
public final M build() {
    Type sourceParameterType = first(method.getSourceParameters()).getType();
    Type resultType = method.getResultType();
    Type sourceElementType = getElementType(sourceParameterType);
    Type targetElementType = getElementType(resultType);
    String loopVariableName = Strings.getSafeVariableName(sourceElementType.getName(), method.getParameterNames());
    SourceRHS sourceRHS = new SourceRHS(loopVariableName, sourceElementType, new HashSet<>(), errorMessagePart);
    SelectionCriteria criteria = SelectionCriteria.forMappingMethods(selectionParameters, method.getOptions().getIterableMapping().getMappingControl(ctx.getElementUtils()), callingContextTargetPropertyName, false);
    Assignment assignment = ctx.getMappingResolver().getTargetAssignment(method, getDescription(), targetElementType, formattingParameters, criteria, sourceRHS, positionHint, () -> forge(sourceRHS, sourceElementType, targetElementType));
    if (assignment == null) {
        if (method instanceof ForgedMethod) {
            // leave messaging to calling property mapping
            return null;
        } else {
            reportCannotCreateMapping(method, String.format("%s \"%s\"", sourceRHS.getSourceErrorMessagePart(), sourceRHS.getSourceType().describe()), sourceRHS.getSourceType(), targetElementType, "");
        }
    } else {
        ctx.getMessager().note(2, Message.ITERABLEMAPPING_SELECT_ELEMENT_NOTE, assignment);
        if (method instanceof ForgedMethod) {
            ForgedMethod forgedMethod = (ForgedMethod) method;
            forgedMethod.addThrownTypes(assignment.getThrownTypes());
        }
    }
    assignment = getWrapper(assignment, method);
    // mapNullToDefault
    boolean mapNullToDefault = method.getOptions().getIterableMapping().getNullValueMappingStrategy().isReturnDefault();
    MethodReference factoryMethod = null;
    if (!method.isUpdateMethod()) {
        factoryMethod = ObjectFactoryMethodResolver.getFactoryMethod(method, null, ctx);
    }
    Set<String> existingVariables = new HashSet<>(method.getParameterNames());
    existingVariables.add(loopVariableName);
    List<LifecycleCallbackMethodReference> beforeMappingMethods = LifecycleMethodResolver.beforeMappingMethods(method, selectionParameters, ctx, existingVariables);
    List<LifecycleCallbackMethodReference> afterMappingMethods = LifecycleMethodResolver.afterMappingMethods(method, selectionParameters, ctx, existingVariables);
    return instantiateMappingMethod(method, existingVariables, assignment, factoryMethod, mapNullToDefault, loopVariableName, beforeMappingMethods, afterMappingMethods, selectionParameters);
}
Also used : Assignment(org.mapstruct.ap.internal.model.common.Assignment) Type(org.mapstruct.ap.internal.model.common.Type) SourceRHS(org.mapstruct.ap.internal.model.common.SourceRHS) SelectionCriteria(org.mapstruct.ap.internal.model.source.selector.SelectionCriteria) HashSet(java.util.HashSet)

Example 32 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class StreamAdderWrapper method getThrownTypes.

@Override
public List<Type> getThrownTypes() {
    List<Type> parentThrownTypes = super.getThrownTypes();
    List<Type> result = new ArrayList<>(parentThrownTypes);
    for (Type thrownTypeToExclude : thrownTypesToExclude) {
        for (Type parentExceptionType : parentThrownTypes) {
            if (parentExceptionType.isAssignableTo(thrownTypeToExclude)) {
                result.remove(parentExceptionType);
            }
        }
    }
    return result;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) ArrayList(java.util.ArrayList)

Example 33 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class Conversions method register.

private void register(Class<?> sourceClass, Class<?> targetClass, ConversionProvider conversion) {
    Type sourceType = typeFactory.getType(sourceClass);
    Type targetType = typeFactory.getType(targetClass);
    conversions.put(new Key(sourceType, targetType), conversion);
    conversions.put(new Key(targetType, sourceType), inverse(conversion));
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type)

Example 34 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class Conversions method register.

private void register(String sourceTypeName, Class<?> targetClass, ConversionProvider conversion) {
    Type sourceType = typeFactory.getType(sourceTypeName);
    Type targetType = typeFactory.getType(targetClass);
    conversions.put(new Key(sourceType, targetType), conversion);
    conversions.put(new Key(targetType, sourceType), inverse(conversion));
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type)

Example 35 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class MapperCreationProcessor method getMappingMethods.

private List<MappingMethod> getMappingMethods(MapperOptions mapperAnnotation, List<SourceMethod> methods) {
    List<MappingMethod> mappingMethods = new ArrayList<>();
    for (SourceMethod method : methods) {
        if (!method.overridesMethod()) {
            continue;
        }
        mergeInheritedOptions(method, mapperAnnotation, methods, new ArrayList<>(), null);
        MappingMethodOptions mappingOptions = method.getOptions();
        boolean hasFactoryMethod = false;
        if (method.isIterableMapping()) {
            this.messager.note(1, Message.ITERABLEMAPPING_CREATE_NOTE, method);
            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;
            NullValueMappingStrategyGem 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();
            }
            this.messager.note(1, Message.MAPMAPPING_CREATE_NOTE, method);
            MapMappingMethod mapMappingMethod = builder.mappingContext(mappingContext).method(method).keyFormattingParameters(keyFormattingParameters).keySelectionParameters(keySelectionParameters).valueFormattingParameters(valueFormattingParameters).valueSelectionParameters(valueSelectionParameters).build();
            hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
            mappingMethods.add(mapMappingMethod);
        } else if (method.isValueMapping()) {
            // prefer value mappings over enum mapping
            this.messager.note(1, Message.VALUEMAPPING_CREATE_NOTE, method);
            ValueMappingMethod valueMappingMethod = new ValueMappingMethod.Builder().mappingContext(mappingContext).method(method).valueMappings(mappingOptions.getValueMappings()).enumMapping(mappingOptions.getEnumMappingOptions()).build();
            if (valueMappingMethod != null) {
                mappingMethods.add(valueMappingMethod);
            }
        } else if (method.isRemovedEnumMapping()) {
            messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_REMOVED);
        } else if (method.isStreamMapping()) {
            this.messager.note(1, Message.STREAMMAPPING_CREATE_NOTE, method);
            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 {
            this.messager.note(1, Message.BEANMAPPING_CREATE_NOTE, method);
            BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
            Type userDefinedReturnType = getUserDesiredReturnType(method);
            Type builderBaseType = userDefinedReturnType != null ? userDefinedReturnType : method.getReturnType();
            BeanMappingMethod.Builder beanMappingBuilder = new BeanMappingMethod.Builder();
            BeanMappingMethod beanMappingMethod = beanMappingBuilder.mappingContext(mappingContext).sourceMethod(method).userDefinedReturnType(userDefinedReturnType).returnTypeBuilder(typeFactory.builderTypeFor(builderBaseType, builder)).build();
            // 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;
            if (beanMappingMethod != null) {
                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 : MappingMethodOptions(org.mapstruct.ap.internal.model.source.MappingMethodOptions) 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) 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) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) BuilderGem(org.mapstruct.ap.internal.gem.BuilderGem) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) Type(org.mapstruct.ap.internal.model.common.Type) FormattingParameters(org.mapstruct.ap.internal.model.common.FormattingParameters) NullValueMappingStrategyGem(org.mapstruct.ap.internal.gem.NullValueMappingStrategyGem) SelectionParameters(org.mapstruct.ap.internal.model.source.SelectionParameters) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

Type (org.mapstruct.ap.internal.model.common.Type)36 ArrayList (java.util.ArrayList)13 Parameter (org.mapstruct.ap.internal.model.common.Parameter)11 DeclaredType (javax.lang.model.type.DeclaredType)10 ExecutableType (javax.lang.model.type.ExecutableType)6 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)6 HashSet (java.util.HashSet)5 TypeMirror (javax.lang.model.type.TypeMirror)4 TreeSet (java.util.TreeSet)3 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)3 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)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 Map (java.util.Map)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 ArrayType (javax.lang.model.type.ArrayType)2