0

I have installed an extension called sonarlint, I am trying to refactor my code and reduce code smells, the plugin is suggesting I do further modifications like

Raw types should not be used Code smell Generic types shouldn’t be used raw (without type parameters) in variable declarations or return values. Doing so bypasses generic type checking, and defers the catch of unsafe code to runtime.

This is one example where it complains

public ResponseWrapper update(TerritoryDto territoryDto) throws IOException {
    // Validate the payload
    var validationResponse = validateUpdatePayload(territoryDto);
    if (validationResponse.getCode() != HttpStatus.OK.value()) return validationResponse;
    TerritoryEntity territory = (TerritoryEntity) validationResponse.getData();

    territory.setPolygon(territoryDto.getPolygon());
    var updated = territoryRepository.save(territory);
    var erpResponse = territoryErpClient.updateTerritory(
        new UpdateTerritoryBoundary(territory.getPolygon().toString()),
        territory.getName()
    );
    if (erpResponse == null) {
        // todo - Consider sending notifications to slack about ERPNext not being reachable.
    }
    return ResponseWrapper
        .builder()
        .code(HttpStatus.OK.value())
        .message("Territory updated successfully")
        .data(TerritoryConverterInterface.TerritoryEntityToDtoMapper.INSTANCE.converter(updated))
        .build();
}

I tried to make modifications according to the sonarLint alert but it is not working. Please help me with this problem. Thank you all.

justrying
  • 69
  • 7
  • Which types are generic? We need to see the definitions of your types. (But I assume it is `ResponseWrapper` which is used as a raw type in the return type. Should probably be `ResponseWrapper` or something similar. But I'm pretty sure that SonarLint has highlighted this exact location already and complained that `ResponseWrapper` is a raw type – knittl Sep 28 '22 at 18:25
  • Hi @knittl and if I use `` Instead of ``, what is the difference? – justrying Sep 28 '22 at 18:54
  • Either callers of your method will not know which type is wrapped in the response or the code will simply fail to compile. Try it out. – knittl Sep 28 '22 at 23:06

0 Answers0