如何处理Bug和“死存储到局部变量"在java中? [英] how to deal with bug "dead store to local variable" in java?

查看:62
本文介绍了如何处理Bug和“死存储到局部变量"在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.

在测试代码中,我尝试使用无效的点来测试circle构造函数,并假设抛出异常.但是会出现错误.我在线检查,但仍然不知道如何解决该问题.有没有人可以帮助我?谢谢

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

错误报告

错误:CircleTest.testIllegalCenter()中的testCircle死存储

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.

请注意,Sun的javac编译器通常会为最终的局部变量生成无效存储.因为FindBugs是基于字节码的工具,所以没有消除这些误报的简便方法.

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

这篇关于如何处理Bug和“死存储到局部变量"在java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆