Skip to content

#3385 Warn when mapper-declared lifecycle methods are never used - #4114

Open
bkks1004 wants to merge 1 commit into
mapstruct:mainfrom
bkks1004:issue-3385
Open

#3385 Warn when mapper-declared lifecycle methods are never used#4114
bkks1004 wants to merge 1 commit into
mapstruct:mainfrom
bkks1004:issue-3385

Conversation

@bkks1004

Copy link
Copy Markdown

Unused @BeforeMapping / @AfterMapping methods currently produce no diagnostic.
If a developer adds an extra parameter by mistake, the method is silently ignored.

For the mapper from the issue:

@Mapper
public interface X {

    B map(A a);

    @AfterMapping
    default void good(A a) {
    }

    @AfterMapping
    default void nowarning(String break_it, A a) {
    }
}

good is invoked, but nowarning is not, and MapStruct does not warn.

Change

Warn only for lifecycle methods that are declared directly on the processed mapper and never type-match any generated mapping method.

Out of scope on purpose, so existing overload / inheritance patterns stay quiet:

  • methods inherited from a super type (BaseMapper)
  • methods coming from Mapper#uses
  • overloads that match by type but are dropped in favor of a more specific one

CarMapper.neverMatched(...) is the remaining true unused case and now expects the warning.

Verification

Run from the repository root with JDK 21.

  • Issue3385Test covers unused @AfterMapping and @BeforeMapping
  • CallbackMethodTest, CallbackMethodTypeMatchingTest, Issue3849Test stay green
  • ./mvnw -pl processor test -Dtest=Issue3385Test,CallbackMethodTest,CallbackMethodTypeMatchingTest,Issue3849Test

Fixes #3385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unused @AfterMapping does not trigger warning

1 participant