dead code的原因是代码无意义。在下面方法中return null;报dead code警告。但是不加这句return null必然是不行的。会报错。因此不是很理解为什么会提示dead code。可有人解惑?
@RequestMapping(value = "/student/get" , method = RequestMethod.GET)
public Student getStudent(@RequestParam(value="name",required=false)String name) throws BizException{
if (name.equals("Chaves")) {
throw new BizException("Chaves is invalid!");
}
if (name != null) {
return studentService.findByName(name);
}
return null;
}
两个if调整下顺序:
if (name == null) {
return null;
}
if(name.equals("Chaves")) {
throw new BizException("Chaves is invalid!");
}
return studentService.findByName(name);
提示dead code,说明无论如何执行不到,,你的那句代码。。。
但是感觉你这个逻辑是能够执行到的,
这样改一下试试:
if (name != null) {
return studentService.findByName(name);
}else{
return null;
}
if("Chaves").equals(name){
return studentService.findByName(name);
}
return null;
很容易理解啊,你的代码逻辑有问题啊,这种return null的情况本来在你这段代码中就是不存在的啊。
最大的问题就是你的if条件的问题,而且重新整理一下,你这段代码的第一个if还可能查询空指针异常的呢。
if (name!=null && name.equals("Chaves")) {
throw new BizException("Chaves is invalid!");
}
//name不是equal的分支
if (name != null) {
return studentService.findByName(name);
}
//name==null
return null;