jborgers / PMD-jPinpoint-rules

PMD rule set for responsible Java and Kotlin coding: performance, sustainability, multi-threading, data mixup and more.
Apache License 2.0
41 stars 10 forks source link

Fix Request: MDCPutWithoutRemove false positive #285

Closed jborgers closed 7 months ago

jborgers commented 7 months ago

Following example is a false positive:

public class TracingInterceptor extends HandlerInterceptorAdapter {

[...]
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        MDC.put("sessionId", request.getSession().getId());
        Optional.ofNullable(request.getHeader(Y_REQUEST_ID)).ifPresent(id -> MDC.put(REQUEST_ID, id));
        Optional.ofNullable(request.getHeader(REQUESTOR_ID)).ifPresent(id -> MDC.put(MDC_REQUESTOR_ID, id));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           @Nullable ModelAndView modelAndView) {
        MDC.remove("sessionId");
        Optional.ofNullable(MDC.get(REQUEST_ID)).ifPresent(id -> MDC.remove(REQUEST_ID));
        Optional.ofNullable(MDC.get(MDC_REQUESTOR_ID)).ifPresent(id -> MDC.remove(MDC_REQUESTOR_ID));
    }
}
jborgers commented 7 months ago

Also, the lambda put is not considered. Add checking of these.