mapstruct / mapstruct/mapstruct
WriteAccessors prefers 'later' candidates to earlier ones?
- Dominant language
- Java
- Stars
- 7.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
On this line, why does it prefer to overwrite the existing candidate if it has found one? It seems somewhat arbitrary.
https://github.com/mapstruct/mapstruct/blob/1ab5db6a2774a58a6c41b5e4c2bfb23b2fa1bab7/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java#L806
I guess the particular reason I'm asking is that when it is mapping to Immutable.Builders, it finds both the 'normal' setter and the one that accepts an `Optional`, and it prefers the `Optional` because it comes later in the candidate list, even though the 'normal' setter would be marginally more efficient - and wouldn't require me to define loads of
```
Optional toOptional(MyClass x)
```
methods. The Builders have no readAccessors unfortunately, so it can only really match on the propertyName and is unable to determine the 'preferredType'. This is when a custom SPI is in use btw (the protobuf one), so it isn't using `ImmutablesAccessorNamingStrategy` though from a brief look that wouldn't make a difference anyway.
Contributor guide
Research direction
Start at processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java line 806 and trace how WriteAccessors candidates are replaced. Reproduce the Immutable.Builder mapping with the protobuf custom SPI, comparing the normal and Optional setters. Done should make the candidate choice intentional rather than depending on list order, with coverage for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100