如何处理java中的“死存储到局部变量”的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23004309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to deal with bug "dead store to local variable" in java?
提问by user3505808
I wrote a simple test code. It is a circle. I suppose most people can image what is a circle class, so I will not paste it.
我写了一个简单的测试代码。它是一个圆圈。我想大多数人都能想象出什么是圈类,所以我就不贴了。
In the test code, I try to test the circle constructor with invalid point, and assume to throw an exception. But a bug occures. I check online, but still do not know how to solve the problem. Is there any one can help me? Thanks
在测试代码中,我尝试使用无效点测试圆构造函数,并假设抛出异常。但是出现了一个错误。网上查了下,还是不知道怎么解决。有没有人可以帮助我?谢谢
code information, bug is in the last sentence of following code
代码信息,bug在下面代码最后一句
/**
* Tests that the Circle constructor throws an exception for center Point.
*/
@Test (expected = IllegalArgumentException.class)
public void testIllegalCenter() {
//Instantiates a circle with an incorrect center point.
@SuppressWarnings("unused")
final Circle testCircle = new Circle(VALID_RADIUS, INVALID_POINT, VALID_COLOR);
}
bug report
错误报告
Bug: Dead store to testCircle in CircleTest.testIllegalCenter()
错误:在 CircleTest.testIllegalCenter() 中死存储到 testCircle
This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used.
该指令为局部变量赋值,但该值不会在任何后续指令中读取或使用。通常,这表示有错误,因为从未使用过计算值。
Note that Sun's javac compiler often generates dead stores for final local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives.
请注意,Sun 的 javac 编译器通常会为最终局部变量生成死存储。由于 FindBugs 是一种基于字节码的工具,因此没有简单的方法可以消除这些误报。
采纳答案by Axel
Just remove the variable and call the constructor like this:
只需删除变量并像这样调用构造函数:
@Test (expected = IllegalArgumentException.class)
public void testIllegalCenter() {
new Circle(VALID_RADIUS, INVALID_POINT, VALID_COLOR);
}