SonarBug修复

数据类型

  • Sonar提示: Use “BigDecimal.valueOf” instead.

解决方法:使用BigDecimal.valueOf()代替。因为这个方法内部会将参数转换为String,保证精度不丢失。

    public static BigDecimal valueOf(double val) {
        return new BigDecimal(Double.toString(val));
    }
  • Sonar提示: Equality tests should not be made with floating point values.

解决方法: 浮点数不应该用==去比较,可能会精度丢失导致不准确。

使用BigDecimal.valueOf().compareTo()比较。如果为零可以直接使用BigDecimal.ZERO。

        if (BigDecimal.valueOf(value1).compareTo(BigDecimal.valueOf(value2)) == 0) {
            //....
        }

如果是double类型,也可以直接用Double.doubleToLongBits()的结果值用==,>,<进行比较,如下:

      if(Double.doubleToLongBits(d1) == Double.doubleToLongBits(d2))){
            //      
      }
  • Sonar提示: Cast one of the operands of this subtraction operation to a “double”.

解决方法:使用(double)类型转换,再进行运算。

double d = (double)a + (double)b;
  • Sonar提示: Cast one of the operands of this multiplication operation to a “long”.

解决方法:使用(long)类型转换,或者在数字后面加上L转换类型。

60*1000*60L;

修饰符

  • Sonar提示: Make this "public static " field final .

解决方法:添加final,或者改成 protected。

改成protected时,一直要注意在其他类中有没有用到,不然会报错。

如果使用了"public static " 最好加上final,避免多个对象都修改了变量造成错误。

Sonar解释如下:There is no good reason to declare a field “public” and “static” without also declaring it “final”. Most of the time this is a kludge to share a state among several objects. But with this approach, any object can do whatever it wants with the shared state, such as setting it to null。

  • Sonar提示: Cannot assign a value to final variable “”

解决方法:final修饰的变量不可赋值,去掉final修饰符或赋值语句。

  • Sonar提示: SonarLint: Format specifiers should be used instead of string concatenation.

解决方法: 使用格式说明符。

String.format("字符串其他内容 %s 字符串其他内容", data)

方法

  • Sonar提示: Return an empty collection instead of null.

解决方法: 返回空集合而不是返回null,空集合不会导致空指针异常。

return Collections.emptyList();
  • Sonar提示: Merge this if statement with the enclosing one.

解决方法: 将靠在一起的多个if合并成一个。

实体类(model)

  • Sonar提示: This class overrides “equals()” and should therefore also override “hashCode()”.

解决方法: 重写equals()必须重写hashCode()。IDEA可以通过Alt+Insert自动生成。

  • Sonar提示: 硬编码http地址不应该为sit地址。

Sonar提示: Make sure using this hardcoded IP address is safe here.

解决方法: 把http地址、IP地址写到property之类的配置文件中。

异常

  • Sonar提示: Use a logger to log this exception.

解决方法:logger.error(“错误提示字符串:”,e);

  • Sonar提示: Define and throw a dedicated exception instead of using a generic one.

解决方法:不要直接抛Error,RuntimeException/Throwable/Exception这样的通用的异常,使用更具体的异常代替

比较常用的 IllegalArgumentException(不合法参数的异常) 。

其他的还有 IOException等

示例如下:

      throw new IllegalArgumentException("invalid argument, please check argument.");
  • Sonar提示: Either log or rethrow this exception.

解决方法: logger.error(“错误提示字符串:”,e);

  • Sonar提示: Either re-interrupt this method or rethrow the “InterruptedException” that can be caught here

解决方法: catch InterruptedException 异常后,需要中断该线程。

        try {
            //...

        } catch (InterruptedException e) {
            log.error("doSomething InterruptedException error.",  e);
            Thread.currentThread().interrupt();
        } catch (Exception e) {
            log.error("doSomething error.",  e);
        }

集合

  • Sonar提示: Iterate over the “entrySet” instead of the “keySet”.

解决方法: 使用entrySet,然后再通过entrySet获取key。

  for (Map.Entry<String, String> entry : map.entrySet()) {
      String key=entry.getKey();
      String value=entry.getValue();
  }
  • Sonar提示: SonarLint: Provide the parametrized type for this generic.

Sonar提示: Raw types should not be used.

解决方法: 使用泛型,比如 List后面指定泛型为 String.

List<String> goodsInfoList = new ArrayList<>();

日志

  • Sonar提示: “Preconditions” and logging arguments should not require evaluation

解决方法: 直接使用{}格式符,如下所示:

      logger.info("日志内容:{}",变量名称);
  • 23
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设个人所得税的计算公式如下: 1. 对于月薪不超过5000元的人员,个人所得税为月薪的5%; 2. 对于月薪在5000元到10000元之间的人员,个人所得税为月薪的10%; 3. 对于月薪在10000元到20000元之间的人员,个人所得税为月薪的15%; 4. 对于月薪在20000元到40000元之间的人员,个人所得税为月薪的20%; 5. 对于月薪超过40000元的人员,个人所得税为月薪的25%。 根据边界值分析法和等价类划分方法,我们可以得到以下测试用例: |月薪|期望个人所得税| |---|---| |-1|无效输入| |0|0| |1|0.05| |4999|249.95| |5000|500| |5001|500.1| |9999|999.9| |10000|1000| |10001|1500.15| |19999|2999.85| |20000|4000| |20001|4400.2| |39999|7999.8| |40000|8000| |40001|10000.25| |50000|12500| 根据上述测试用例,我们可以编写如下的测试代码: ```java import static org.junit.Assert.*; import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class TestTax { private int salary; private double expectedTax; public TestTax(int salary, double expectedTax) { this.salary = salary; this.expectedTax = expectedTax; } @Parameters public static Iterable<Object[]> data() { return Arrays.asList(new Object[][] { { -1, -1 }, { 0, 0 }, { 1, 0.05 }, { 4999, 249.95 }, { 5000, 500 }, { 5001, 500.1 }, { 9999, 999.9 }, { 10000, 1000 }, { 10001, 1500.15 }, { 19999, 2999.85 }, { 20000, 4000 }, { 20001, 4400.2 }, { 39999, 7999.8 }, { 40000, 8000 }, { 40001, 10000.25 }, { 50000, 12500 } }); } @Test public void testTax() { if (salary < 0) { assertEquals(-1, expectedTax, 0.001); } else { double actualTax = calculateTax(salary); assertEquals(expectedTax, actualTax, 0.001); } } private double calculateTax(int salary) { double tax = 0; if (salary > 0 && salary <= 5000) { tax = salary * 0.05; } else if (salary > 5000 && salary <= 10000) { tax = salary * 0.1; } else if (salary > 10000 && salary <= 20000) { tax = salary * 0.15; } else if (salary > 20000 && salary <= 40000) { tax = salary * 0.2; } else if (salary > 40000) { tax = salary * 0.25; } else { // salary <= 0 return -1; } return tax; } } ``` 以上代码中,我们使用了JUnit的Parametrized运行器来运行多组测试数据。通过`@Parameters`注解,我们可以指定测试数据集合,它是一个二维数组,每一行表示一组测试数据。在测试方法中,我们首先判断月薪是否小于0,如果是则期望返回-1,表示无效输入;否则计算个人所得税,然后使用`assertEquals()`方法来断言计算得到的税额与期望值是否相等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值