Search code examples
javajpajava-streampmd

I am getting a DU anomaly(PMD) in my code. I am not sure how to fix this


I have to iterate over the request and create a new Entity List request which I can use in saveAll() method of JPA repository
I am getting DU anomaly Error I am encountering is from PMD.

  • DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text.
Map<Integer, AlertHeader> alertHeaderMap = findAllAlertRecords(lockAlertRequest);

List<AlertHeader> alertHeaderRequestToUpdate = lockAlertRequest.stream()
        .map(request -> {
            AlertHeader alertHeader = alertHeaderMap.get(request.getAlertId());
            alertValidatorUtil.validateRecordNotFound(alertHeader, request);
            alertValidatorUtil.validateUnauthorizedLock(alertHeader, request);
            alertHeader.setStatus(AlertStatus.valueOf(request.getStatus()));
            alertHeader.setExpediteEmployeeId(request.getExpediteEmployeeId());
            alertHeader.setExpdLockDateTime(parseDateTime(request.getExpediteLockDateTime()));
            return alertHeader;
        })
        .collect(Collectors.toList());

alertHeaderRepository.saveAll(alertHeaderRequestToUpdate);

If am using this code snippet I am not getting any error not able to understand the real cause here.

lockAlertRequest.forEach(request -> { AlertHeader alertHeader = alertHeaderMap.get(request.getAlertId()); alertValidatorUtil.validateRecordNotFound(alertHeader, request); alertValidatorUtil.validateUnauthorizedLock(alertHeader, request); alertHeader.setStatus(AlertStatus.valueOf(request.getStatus())); alertHeader.setExpediteEmployeeId(request.getExpediteEmployeeId()); alertHeader.setExpdLockDateTime(parseDateTime(request.getExpediteLockDateTime())); alertHeaderRequestToUpdate.add(alertHeader); });

Solution

  • The warning is a false positive that can be ignored / suppressed.

    Having said that, you seem to be using a VERY OLD version of PMD. The rule that produces this warning (DataflowAnomalyAnalysis) has been deprecated since PMD 6.27.0 (August 2020), and completely removed in PMD 7.0.0.

    A better (more accurate) rule named UnusedAssignment has been available since PMD 6.26.0 (July 2020).

    My best advice is to update to PMD 7.2.0, or at the very least to 6.55.0 if you can't / don't want to deal with upgrading from PMD 6 to 7.