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提示: 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 .

日志

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

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

      logger.info("日志內容:{}",變量名稱);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM