How to solve Sonarqube’s criticism: Read of unwritten field Connector

Asked

Viewed 39 times

0

In my class in the method findRecursosMensagem() Sonarqube makes a criticism, but I did not understand what should be corrected. Could help me understand what must be done?

public class LoginController extends AbstractController  implements AuthenticationProvider, Serializable {
    private static final long serialVersionUID = 1L;
    
    private Connector connector;    
    @Resource(name = "messageSource")
    protected MessageSource messageSource;
    
    @Inject
    private IntegracaoService integracaoService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index() {
        return new ModelAndView("login");
    }

    @ResponseBody
    @RequestMapping(value = "/logar", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public Autenticacao logar(@RequestBody AutenticacaoDMN autenticacao, HttpServletRequest request){
        request.getSession().invalidate();
        autenticacao.setSistema(SISTEMA);
        Autenticacao autenticacaoRetorno = integracaoService.login(autenticacao);
        if (autenticacaoRetorno.isAutenticado()) {
            autenticacao.setIdUsuario(autenticacaoRetorno.getIdUsuario());
            integracaoService.setUserAutenticate(autenticacao, request);
        }
        
        return autenticacaoRetorno;
    }   

    private List<Map<String, Object>>  findRecursosMensagem(){
        Map<String, Object> args = new HashMap<>();
        args.put("chave", "MensagemCovid");
        List<Map<String, Object>> mensagem = null;
        mensagem = connector.findServicoReturnList(args, "lms.radar.usuarioFacade.findRecursosMensagem");
        return mensagem;        
    }
}

mensagem/crítica do SONARqube

  • Sonar always has an explanation. Click on See Rule, that he explains to you

2 answers

0

What the tool is saying is that the object connector has not been initialized, and you are reading/accessing/calling a method from it.

This can be both a false-positive (sometimes - especially in older versions of sonar, the tool does not detect initializations made "indirectly" - as those made by Spring, when you use a @autowired, for example) how much a serious problem can cause a Runtime error (a Nullpointerexception, for example) if it passes through the line of code.

If, in your case, it’s a false positive, you need to adjust Sonar’s rules so that it doesn’t get caught as a problem.

0


Has a code smell in:

mensagem = connector.findServicoReturnList(args, "lms.radar.usuarioFacade.findRecursosMensagem");
return mensagem;  

Only return direct without assigning to the message variable. It would look like this:

return connector.findServicoReturnList(args, "lms.radar.usuarioFacade.findRecursosMensagem");

About the critical sonar error, in the variable connector, How is she being instantiated? Because there is no constructor to inject, there is no @Autowired. In fact, Sonar is signaling that it may be null at the time of execution of the code below:

connector.findServicoReturnList(args, "lms.radar.usuarioFacade.findRecursosMensagem");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.