javaspringintellij-ideaspring-retryunused-functions

Intellij - Spring Retry @Recover method marked as unused


When using Spring Retry @Recover method (which works ok) IntelliJ always marking method as unused and suggest to safe delete

@Recover
public void recover(RetryableException e, String param1) {
    //recover
}
@Retryable(include = RetryableException.class, maxAttempts = 2)
public void retryable(String param1) {
    //execute
    throw new RetryableException();
}

Method 'recover(com.gth.common.exceptions.RetryableException, java.lang.String)' is never used

How can it be avoided? how can IntelliJ be aware of the recover method usage?


Solution

  • Method declarations looks ok but as you haven't shared any further details, it is mostly because you are not throwing RetryableException from your retryable method.

    To invoke recovery after retries, your retry must throw type of exception which you have defined as recover method's argument. Please check about that , if that is not case, please share some more details.

    Edit:

    Spring's recovery method gets called internally after retry & hence after scanning code, intellij didn't find any reference where your recover is getting called.

    This is just warning from intellij & don't pose any issue. You can disable this behaviour from Preferences > Editor > Inspections > Unused Declaration >Java > Unused declaration .

    Other option is to use @SuppressWarnings("unused") above your recover.