java dead store,如何处理错误“死存储到局部变量”在java中?

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

/**

* 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()

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.

解决方案

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值