Skip to content

How to map objects when source object has custom method for Getter? #2753

@wonsuc

Description

@wonsuc

I'm using a specific framework(it's called TeamCenter), I want to use this library to map from that framework's object to my custom DTO.

However, this framework's model class is ModelObject and to get values from the object, I have to call method like this =>
modelObject.getPropertyDisplayableValue("propertyName");

How can I achieve this with MapStruct?

I created a Mapper class like below.

@Mapper
public interface ProjectMapper {
    ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class);

    @Mapping(target = "id", source = "prg0PlanId")
    ProjectDto toDto(ModelObject projectModel) throws NotLoadedException;
}

The only working way is to use expression.

@Mapper
public interface ProjectMapper {
    ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class);

    @Mapping(target = "id", expression = "java(projectModel.getPropertyDisplayableValue(\"prg0PlanId\")")
    ProjectDto toDto(ModelObject projectModel) throws NotLoadedException;
}

But, I think this is not the advantage to use MapStruct.

I want to use something like this.

@Mapper
public interface ProjectMapper {
    ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class);

    @Mapping(target = "id", source = "prg0PlanId", qualifiedByName = "getPropertyDisplayableValue")
    ProjectDto toDto(ModelObject projectModel) throws NotLoadedException;

    @Named("getPropertyDisplayableValue")
    static String getPropertyDisplayableValue(ModelObject modelObject, String source) throws NotLoadedException {
        return modelObject.getPropertyDisplayableValue(source);
    }
}

But, I noticed there are no workarounds something like this.

I also don't want to manually implement mapping method something like this.

What I need is just specifying custom method to get values from source object.

Am I missing something or this is not available with MapStruct?

Thanks for helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions