Support field access if visibility rules permit #557

Closed
gunnarmorling opened this Issue May 22, 2015 · 11 comments

Projects

None yet

5 participants

@gunnarmorling
Member

If e.g. the target object is a plain "struct" with public fields, it should supported to write to these using direct field access. This should only be supported if the visibility rules permit that access, i.e. no reflection-based work-around otherwise.

@GilraenBurland

+1 for this.

@cardosso

Is this being considered?

For me this is the current deal breaker with MapStruct and the only reason I'm still using Orika. I don't want to fill my Hibernate domain objects with unnecessary getters and setters just to be able to use a mapping framework. It's a pitty this feature is missing because otherwise this is a great library.

This should only be done on public accessible fields and could even be made configurable (ie disabled by default).

@gunnarmorling
Member

Hey @cardosso, yes, it is considered for sure. We just did not yet find the time to implement it. If you are interested in helping out, let me know and I can help you to get started with hacking on MapStruct.

I will schedule this for the 1.1 release.

@gunnarmorling gunnarmorling added this to the 1.1-next milestone Nov 16, 2015
@cardosso

That's great news thanks.
I never looked into the MapStruct source code, I guess I could check it out and see if I could be of some help implementing it if you could give me some quick guidelines. Not sure if I will have much free time during the next days though.

@gunnarmorling
Member

Hi, any help would be more than welcome. If you are interested, you may check out the resources given here for getting started. The change would have to include adaptions the logic for identifying mappable properties as well as the templates for generating code working with properties.

@xiemeilong

+1

@filiphr
Member
filiphr commented Oct 8, 2016

@gunnarmorling I had a look at this issue. And I think I can start working on it. I had a deeper look at the code and the inner workings. I think that in order to do this, we would need to add our own Accessor class which would wrap the ExecutableElemement(s) and the VariableElements. In our own Accessor we would add the methods which we need and get them from the underlying Element.
The Type methods getPropertyPresenceCheckers, getPropertyReadAccessors and getPropertyWriteAccessors will need to be adjusted to return our wrapper.

This is only the first step, I will have to investigate a bit more, about how to proceed once we have the fields for mapping.

What do you think? Am I on the right path?

@filiphr
Member
filiphr commented Oct 8, 2016

Another thing, we are using the AccessorNamingStrategy with the ExecutableElement(s). I guess this is public and should not be modified?

@gunnarmorling
Member
gunnarmorling commented Oct 9, 2016 edited

@filiphr Thanks for picking up this one!

What do you think? Am I on the right path?

Yes, I think building such common abstraction would the right way to go.

we are using the AccessorNamingStrategy with the ExecutableElement(s)

Good question, I reckon that it doesn't need to be changed. A field named "foo" really can only represent a property named "foo" and it cannot be a "presence checker" or any of the other method types.

@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Oct 10, 2016
@filiphr filiphr Fixes #557; Provide a way to use field mappng within mapstruct d19441b
@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Oct 10, 2016
@filiphr filiphr Fixes #557; Provide a way to use field mappng within mapstruct cee8f95
@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Nov 29, 2016
@filiphr filiphr #557; Provide a way to use field mappng within mapstruct 6f226a1
@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Nov 30, 2016
@filiphr filiphr #557; Provide a way to use field mappng within mapstruct b95e715
@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Dec 2, 2016
@filiphr filiphr #557; Provide a way to use field mappng within mapstruct f2d4c8d
@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Dec 3, 2016
@filiphr filiphr #557 Add documentation about using fields as read/write accessors 511a47a
@filiphr
Member
filiphr commented Dec 3, 2016

Official support for direct field mappings in MapStruct is possible since #928. You can try it out with the latest SNAPSHOT version. Once the documentation PR #982 is merged the ticket will be closed.

@filiphr filiphr modified the milestone: 1.2.x, Future planning Dec 3, 2016
@filiphr
Member
filiphr commented Dec 4, 2016

With the merged documentation from #982 this new feature is completely implemented

@filiphr filiphr closed this Dec 4, 2016
@filiphr filiphr added a commit to filiphr/mapstruct that referenced this issue Dec 17, 2016
@filiphr filiphr #557 add Javadoc for the Accessor a4e9c21
@filiphr filiphr added a commit that referenced this issue Dec 19, 2016
@filiphr filiphr #557 add Javadoc for the Accessor 228c1f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment