Sonar抱怨无用的局部变量赋值

Awa*_*Awa 1 java spring controller maven sonarqube

我在我的程序中有以下代码片段,并且在与Maven集成之后,我正在运行SonarQube 5以进行代码质量检查.

但是,Sonar要求将此无用的赋值移除到局部变量"session".

@RequestMapping(value = "/logoff", method = RequestMethod.GET)
public String getLogoffPage(HttpServletRequest request, HttpServletResponse response) {
    logger.info(" Before Log Offf........ " + request.getSession().getId() );
    HttpSession session =request.getSession(true);
    request.getSession().invalidate();
    myApplication.logout();
    SecurityContextHolder.clearContext();
    session=null;                   
    return "login";
}
Run Code Online (Sandbox Code Playgroud)

G. *_*eam 11

假设问题是"为什么":

你到底session什么?没有.

HttpSession session =request.getSession(true);  // session assigned
request.getSession().invalidate();         // session NOT used
myApplication.logout();
SecurityContextHolder.clearContext();
session=null;                            // session re-assigned
Run Code Online (Sandbox Code Playgroud)

也许你的意思是这个?

HttpSession session =request.getSession(true);
session.invalidate();
myApplication.logout();
SecurityContextHolder.clearContext();
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我已经放弃了,session = null因为Java没有理由(C会是另一回事).

当然,代码可能更清晰:

request.getSession().invalidate();
myApplication.logout();
SecurityContextHolder.clearContext();
Run Code Online (Sandbox Code Playgroud)