Search in sources :

Example 1 with BuilderGem

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

the class BuilderFinisherMethodResolver method getBuilderFinisherMethod.

public static MethodReference getBuilderFinisherMethod(Method method, BuilderType builderType, MappingBuilderContext ctx) {
    Collection<ExecutableElement> buildMethods = builderType.getBuildMethods();
    if (buildMethods.isEmpty()) {
        // If we reach this method this should never happen
        return null;
    }
    BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
    if (builder == null && buildMethods.size() == 1) {
        return MethodReference.forMethodCall(first(buildMethods).getSimpleName().toString());
    } else {
        String buildMethodPattern = DEFAULT_BUILD_METHOD_NAME;
        if (builder != null) {
            buildMethodPattern = builder.buildMethod().get();
        }
        for (ExecutableElement buildMethod : buildMethods) {
            String methodName = buildMethod.getSimpleName().toString();
            if (methodName.matches(buildMethodPattern)) {
                return MethodReference.forMethodCall(methodName);
            }
        }
        if (builder == null) {
            ctx.getMessager().printMessage(method.getExecutable(), Message.BUILDER_NO_BUILD_METHOD_FOUND_DEFAULT, buildMethodPattern, builderType.getBuilder(), builderType.getBuildingType(), Strings.join(buildMethods, ", ", EXECUTABLE_ELEMENT_NAME_EXTRACTOR));
        } else {
            ctx.getMessager().printMessage(method.getExecutable(), builder.mirror(), Message.BUILDER_NO_BUILD_METHOD_FOUND, buildMethodPattern, builderType.getBuilder(), builderType.getBuildingType(), Strings.join(buildMethods, ", ", EXECUTABLE_ELEMENT_NAME_EXTRACTOR));
        }
    }
    return null;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) BuilderGem(org.mapstruct.ap.internal.gem.BuilderGem)

Example 2 with BuilderGem

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

the class AbstractMappingMethodBuilder method forgeMapping.

private Assignment forgeMapping(SourceRHS sourceRHS, Type sourceType, Type targetType, ForgeMethodCreator forgeMethodCreator) {
    if (!canGenerateAutoSubMappingBetween(sourceType, targetType)) {
        return null;
    }
    String name = getName(sourceType, targetType);
    name = Strings.getSafeVariableName(name, ctx.getReservedNames());
    ForgedMethodHistory history = null;
    if (method instanceof ForgedMethod) {
        history = ((ForgedMethod) method).getHistory();
    }
    description = new ForgedMethodHistory(history, Strings.stubPropertyName(sourceRHS.getSourceType().getName()), Strings.stubPropertyName(targetType.getName()), sourceRHS.getSourceType(), targetType, shouldUsePropertyNamesInHistory(), sourceRHS.getSourceErrorMessagePart());
    ForgedMethod forgedMethod = forgeMethodCreator.createMethod(name, sourceType, targetType, method, description, true);
    BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
    return createForgedAssignment(sourceRHS, ctx.getTypeFactory().builderTypeFor(targetType, builder), forgedMethod);
}
Also used : BuilderGem(org.mapstruct.ap.internal.gem.BuilderGem)

Example 3 with BuilderGem

use of org.mapstruct.ap.internal.gem.BuilderGem 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

BuilderGem (org.mapstruct.ap.internal.gem.BuilderGem)3 ArrayList (java.util.ArrayList)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 NullValueMappingStrategyGem (org.mapstruct.ap.internal.gem.NullValueMappingStrategyGem)1 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)1 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)1 ContainerMappingMethodBuilder (org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder)1 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)1 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)1 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)1 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)1 ValueMappingMethod (org.mapstruct.ap.internal.model.ValueMappingMethod)1 FormattingParameters (org.mapstruct.ap.internal.model.common.FormattingParameters)1 Type (org.mapstruct.ap.internal.model.common.Type)1 MappingMethodOptions (org.mapstruct.ap.internal.model.source.MappingMethodOptions)1 SelectionParameters (org.mapstruct.ap.internal.model.source.SelectionParameters)1 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)1