1. 程式人生 > >FindBugs常見錯誤描述和解決方法

FindBugs常見錯誤描述和解決方法

SHOULD BE A STATIC INNER CLASS

官方介紹:

This class is an inner class, but does not use its embedded reference to the object which created it.This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.  If possible, the class should be made static

解釋說明:

若成員類中未訪問外圍類的非靜態成員,為避免額外的空間和時間開銷,建議改用靜態成員類。

程式碼示例:

修改方式:

EQUALS METHOD OVERRIDES EQUALS IN SUPERCLASS AND MAY NOT BE SYMMETRIC

官方介紹:

This class defines an equals method that overrides an equals method in a superclass. Both equals methods methods use instanceof in the determination of whether two objects are equal. This is fraught with peril, since it is important that the equals method is symmetrical (in other words, a.equals(b) == b.equals(a)). If B is a subtype of A, and A's equals method checks that the argument is an instanceof A, and B's equals method checks that the argument is an instanceof B, it is quite likely that the equivalence relation defined by these methods is not symmetric。

解釋說明:

子類覆蓋了父類中的equals方法,而兩個類中都是用了instanceof來判斷兩個物件是否相等,這個操作存在風險。equals方法應該具有對稱性(a.equals(b) == b.equals(a)),但是當B是A的子類時,A的equals方法檢查引數是A的例項,B的equals方法檢查引數是B的例項,則這些方法定義的equal關係很可能不是對稱的。

程式碼示例:

修改方式:

REPEATED CONDITIONAL TESTS

官方介紹:

The code contains a conditional test is performed twice, one right after the other (e.g., x == 0 || x == 0). Perhaps the second occurrence is intended to be something else (e.g., x == 0 || y == 0)。

解釋說明:

該程式碼包含對同一個條件試驗了兩次,兩邊完全一樣例如:(如X == 0 | | x == 0)。可能第二次出現是打算判斷別的不同條件(如X == 0 | | y== 0)。

程式碼示例:

修改方式:

RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

官方介紹:

This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value。

解釋說明:

沒有檢查方法的返回值,但返回值可以代表異常。

程式碼示例:

修改方式:

SE_COMPARATOR_SHOULD_BE_SERIALIZABLE

官方說明:

This class implements the Comparator interface. You should consider whether or not it should also implement the Serializable interface. If a comparator is used to construct an ordered collection such as a TreeMap, then the TreeMap will be serializable only if the comparator is also serializable. As most comparators have little or no state, making them serializable is generally easy and good defensive programming。

解釋說明:

Comparator介面沒有實現Serializable介面

修改方式:

實現Serializable介面

DM_NUMBER_CTOR

官方說明:

Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster。

解釋說明:

效能 - 方法呼叫低效的數字構造方法;使用靜態valueOf代替

程式碼示例:

修改示例:

DM_BOXED_PRIMITIVE_FOR_PARSING

官方說明:

A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method。

解釋說明:

裝箱和拆箱的問題,把一個字串裝箱,解析獲取箱子中原始值。更有效率就是使用靜態方法,parseXXX 方法。

程式碼示例:

修改示例:

相關推薦

FindBugs常見錯誤描述解決方法

SHOULD BE A STATIC INNER CLASS 官方介紹: This class is an inner class, but does not use its embedded reference to the object which created i

FindBugs錯誤描述解決方法

(一)[DLS_DEAD_LOCAL_STORE] 描述: Dead store to 未使用的區域性變數 解決方法:區域性變數定義後未使用;例項化物件後又重新對該物件賦值 (二)[ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD]  描述:Write to static fie

Python 爬蟲常見的坑解決方法

gpo 爬蟲 nic 詳細 true wow user html encoding 1.請求時出現HTTP Error 403: Forbidden headers = {‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; WOW64;

關於Maven的錯誤集合解決方法

關於maven的錯誤集合 1.Java compiler level does not match the version of the installed Java project facet 當我把jdk版本換成1.8的時候,會報錯如下:

常見錯誤分析及解決方法

Such as :compile block ----Con_appError:make[1]: *** No rule to make target `Hai_Extern.obj', needed by `build/GXQ26_07B/gprs/MT6226o/lib/conn_app.lib'.  S

安卓開發常見錯誤原因及解決方法

做安卓開發一年半了,遇到過不少的問題,其中有些花了不少時間才得到解決,有些至今不知為何,也請高人指點迷津,為我撥雲見日~~有錯誤也多多包涵,幫我糾正~~ 1.更換安卓專案的庫檔案(即使庫檔案程

NFS服務常見故障排查解決方法

NFS,全名叫Network File System,中文叫網路檔案系統,是Linux、UNIX系統的分散式檔案系統的一個組成部分,可實現在不同網路上共享遠端檔案系統。NFS由Sun公司開發,目前已經成為檔案服務的一種標準之一(RFC1904,RFC1813)。其最大的功

Mysql常見錯誤提示及解決方法

130 :檔案格式不正確。(還不是很清楚錯誤的狀況) 145 :檔案無法開啟。1005:建立表失敗。1006:建立資料庫失敗。 1007:資料庫已存在,建立資料庫失敗。 1008:資料庫不存在,刪除資料庫失敗。 1009:不能刪除資料庫檔案導致刪除資料

ActiveMQ常見的問題解決方法

1.先講嚴重的:服務掛掉。 這得從ActiveMQ的儲存機制說起。在通常的情況下,非持久化訊息是儲存在記憶體中的,持久化訊息是儲存在檔案中的,它們的最大限制在配置檔案的節點中配置。但是,在非持久化訊息堆積到一定程度,記憶體告急的時候,ActiveMQ會

ABAP--關於Unicode的常見錯誤解決方法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

傳送郵件常見錯誤解決方法

傳送郵件是生活工作中最常見不過的事情了,但是在這個過程中,很多人都碰到過傳送錯誤,本文給列舉了這些常見的錯誤和解決方法,希望對大家有所幫助。 錯誤1 550 Mail content denied 這種是內容的問題,使用者加伺服器IP白名單就可以了。 錯誤2

Ftp上傳常見錯誤解決方法

Ftp 是管理維護網站資料 的重要手段,經常有一些客戶提出Ftp上傳的疑問.現在總結常見錯誤和解決方案如下(以FlashFxp為例) 1. Ftp伺服器連線失敗,分為以下四種情況。 a).連線被拒, 錯誤資訊如下: [右] 正在連線到 www.yourdomain.com -> DNS=www.you

android學習——android 常見錯誤 解決方法

1. Application does not specify an API level requirement! 解決方法:AndroidManifest.xml中 加入:<uses-sdk android:minSdkVersion="3"></use

Ftp上傳常見錯誤解決方法

Ftp連線常見問題Ftp 是管理維護網站資料 的重要手段,經常有一些客戶提出Ftp上傳的疑問.現在總結常見錯誤和解決方案如下(以FlashFxp為例)1. Ftp伺服器連線失敗,分為以下四種情況。a).

stsuts常見錯誤解決方法

   剛剛接觸struts,感覺好多的東西都不太明白,感覺最重要的就是看不懂錯誤以及解決方法。這兩天在網上找了些資料,copy了過來,為以後方便以後的繼續學習打個基礎。    PS:以下所說的struts-config.

mysql load data遇到的兩個錯誤解決方法

ora sele rwx oba color term 兩個 讀寫 nal 1.load數據遇到報錯1 mysql> load data infile "/root/mysql_test/mysql_t1data" into table t1; ERROR 1290

分享一些python中的錯誤解決方法

 1. SyntaxError: 'return' outside function 解決: 將return 放在方法體中,return不能在方法以外使用 2.name='小王'    age=16    print('我的名字是'+

一個JAVA單例模式的典型錯誤應用的分析解決方法

                問題來自論壇,其程式碼如下:[java] view plain copy print?import java.sql.Connection;  import java.sql.PreparedStatement;  import java.sql.ResultSet;  imp

nginx 403 Forbidden錯誤的原因解決方法

問題總是等著自己去解決,剛弄了下nginx開機自啟動和開機禁用防火牆,可是在瀏覽器訪問卻報了403 Forbidden錯誤。 檢視錯誤日誌,發現是許可權不足導致的。 [[email protected] logs]# cat error.log

常見HBase故障分析解決方法

1. java.net.SocketException: Too many open files 問題原因: 問題現象:所有節點region server程序掛掉,hbase不可訪問,檢視日誌有如下資訊 tail hbase-hbase-regionserver-ip-10