Sonar問題及解決方案匯總


Sonar

Avoid long parameter lists.

方法參數過多,最多允許5個

Missing a Javadoc comment.

需要注釋

Expected @param tag for 'area'.

需要注釋

Expected @return tag.

需要注釋

Expected @throws tag for 'Exception'.

需要注釋

Unused @param tag for 'licenseInfoMap'.

修改注釋

Line is longer than 160 characters (found 162).

每一行代碼不得超過160個字符,需要分行

Add a private constructor to hide the implicit public one.

常量類和工具類需要一個私有的構造器,同時將該類定義為final,不可繼承

Variables that are final and static should be all capitals, 'logger' is not all capitals.

final修飾的變量名稱需要全部大寫

Only variables that are final should contain capitals (except for underscores in standard prefix/suffix), '_tar' is not final.

只有final修飾的變量名稱中才可以有下划線,要么去掉下划線,要么加上final修飾符,Map,List和java對象加上final修飾符后,代表這個對象指向的內存地址不可變,但內容和值都是可以改變的

Name '_targetDataSources' must match pattern '(^[a-z][a-z0-9][a-zA-Z0-9]{0,50}$)'.

變量命名需要符合正則表達式

The field name indicates a constant but its modifiers do not

變量沒有final修飾卻使用了final變量的命名規則

Variables should start with a lowercase character, 'SUSPEND_MAP' starts with uppercase character.

普通變量(即沒有final修飾)需要駝峰式命名,首字母為小寫

More than 8 parameters (found 9).

構造方法只能有7個參數

Constructor has 9 parameters, which is greater than 7 authorized.

構造方法只能有7個參數

Define a constant instead of duplicating this literal "muluList" 6 times.

某個字符串出現了4次或以上,需要定義一個常量來代替

A method/constructor shouldnt explicitly throw java.lang.Exception

不能直接拋出Exception,需要精確到具體的異常,且最多拋出4個,超過請try catch

Define and throw a dedicated exception instead of using a generic one.

不能直接拋出Exception,需要精確到具體的異常,且最多拋出4個,超過請try catch

Remove this useless assignment to local variable "orgnIdL".

一個沒有使用的變量,刪掉即可

Avoid unused private fields such as 'fastGroup'.

一個沒有使用的變量,刪掉即可。如果不能刪,則將private改為protected或default即可。

Exceptional return value of java.io.File.mkdirs() ignored in ...

mkdirs方法有返回值,如果是false則代表文件夾創建失敗,需要處理。判斷返回值是否正確,並進行處理,如記日志或拋異常等

Exceptional return value of java.io.File.delete() ignored in ...

delete方法有返回值,如果是false則代表文件刪除失敗,需要處理。判斷返回值是否正確,並進行處理,如記日志或拋異常等

Use "java.nio.Files#delete" here for better messages on error conditions.

使用Files.delete(file.toPath())來代替file.delete()

Suspicious comparison of a Integer reference to constant in ...

Integer需要使用equals來比較,這是由於超出(-128~127)范圍后,==無法比較,但equals性能較低

Refactor the code in order to not assign to this loop counter from within the loop body.

不要在循環體內操作計數器,重構代碼

Rename "jdbcTemplate" which hides the field declared at line 116.

一個類中變量名稱重復,重命名變量名稱即可

Field ssoServer has the same name as a method

一個類中字段名稱和方法名稱重復,重命名即可

Merge this if statement with the enclosing one.

多個if可以合並,合並成一個

Replace the synchronized class "StringBuffer" by an unsynchronized one such as "StringBuilder".

StringBuffer需要換成StringBuilder

A "NullPointerException" could be thrown; "dataResults" is nullable here.

在使用對象之前需要對這個對象進行非空判斷

Possible null pointer dereference of track in ...

在使用對象之前需要對這個對象進行非空判斷

Update this method so that its implementation is not identical to "getDvStatus" on line 149.

兩個方法的內容完全一致,不用寫兩遍,在其中一個方法中調用另一個方法即可。或者刪掉一個,全部使用另一個方法

Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.

空的方法體內部需要寫一個注釋

Avoid throwing raw exception types.

自定義一個異常來繼承RuntimeException,然后拋出這個自定義的異常,而不是拋出通用的RuntimeException

Define and throw a dedicated exception instead of using a generic one.

自定義一個異常來繼承RuntimeException,然后拋出這個自定義的異常,而不是拋出通用的RuntimeException

New exception is thrown in catch block, original stack trace may be lost

在catch中拋出異常會使拋出的異常混淆,所以拋出的異常必須帶着原異常信息。例如:throw new BusinessException(errorMsg, e)這里第二個參數是必須的

Avoid empty catch blocks

catch代碼塊不能為空

Either remove or fill this block of code.

刪掉或填充這個代碼塊

Return an empty collection instead of null.

返回空集合Collections.emptyList()而不是null

Put single-quotes around '-' to use the faster "indexOf(char)" method.

將雙引號改為單引號,理由是用char來找字符比用String來找效率更高

Method length is 221 lines (max allowed is 200).

一個方法最多200行,試着壓縮,實在不行只能拆成多個方法

Avoid printStackTrace(); use a logger call instead.

catch中使用logger.error來代替e.printStackTrace()。Logger logger = LoggerFactory.getLogger(this.getClass())。記住,logger不可以在數據庫對象類中使用

Remove this expression which always evaluates to "true"

這個判斷語句總是為true,刪掉即可

... concatenates strings using + in a loop

在循環中String不要用加號來拼接,而是使用StringBuilder

Avoid using implementation types like 'LinkedHashMap'; use the interface instead

在方法的參數中不要使用具體的實現類對象,而是使用它的接口對象。例如:不用LinkedHashMap而用Map

Iterate over the "entrySet" instead of the "keySet".

遍歷Map不使用keySet而使用entrySet

... makes inefficient use of keySet iterator instead of entrySet iterator

遍歷Map不使用keySet而使用entrySet

Move constants to a class or enum.

接口中不要定義常量,移到常量類中

Boxing/unboxing to parse a primitive ...

使用parse方法來代替valueOf方法

Null passed for non-null parameter of ...

將一個可能為空的參數傳到了一個方法中,但在該方法中,該參數不可為空。修改這個方法,在使用變量前進行非空判斷,則這個參數就成為了可為空參數

Remove this "Integer" constructor

不要使用new Integer來初始化變量的值,使用Integer.valueOf

new com.model.CatalogInfo() invokes inefficient new Integer(int) constructor; use Integer.valueOf(int) instead

不要使用new Integer來初始化變量的值,使用Integer.valueOf

HTTP parameter directly written to HTTP header output in ...

resp.setHeader("Content-Disposition", "attachment;filename = " + fileName)改為resp.setHeader("Content-Disposition", "attachment;filename = " + new String(fileName.getBytes("GBK"), "ISO8859-1"))

Relative path traversal in ...

new File(filePath + fileName)改為new File(filePath, fileName)。還有使用new FileInputStream(File srcFile)而不是使用new FileInputStream(filePath + fileName)

Boxing/unboxing to parse a primitive ...

.valueOf改為.parseXXX,這是由於valueOf返回的是包裝類對象,而parseXXX返回的是值

Use try-with-resources or close this "FileInputStream" in a "finally" clause.

使用try(FileInputStream inFile = new FileInputStream(file)){System.out.println("...")}catch{}且不需要finally代碼塊來關閉,這是jdk1.7的新寫法,可以將一些可自動關閉的資源寫在try()中,就可以直接實現自動關閉

Change this "try" to a try-with-resources. (sonar.java.source not set. Assuming 7 or greater.)

使用try(FileInputStream inFile = new FileInputStream(file)){System.out.println("...")}catch{}且不需要finally代碼塊來關閉,這是jdk1.7的新寫法,可以將一些可自動關閉的資源寫在try()中,就可以直接實現自動關閉

Replace this use of System.out or System.err by a logger.

System.out換成logger.info。記住,logger不可以在數據庫對象類中使用。

System.out.println is used

System.out換成logger.info。記住,logger不可以在數據庫對象類中使用。

Variable 'auditTypeName' must be private and have accessor methods.

只有靜態變量才可以用public修飾,所以public改為private或protected或default

The return value of "valueOf" must be used.

這個語句的返回值必須要被使用,刪掉或者使用它

Return value of String.valueOf(Object) ignored in ...

這個語句的返回值必須要被使用,刪掉或者使用它

Synchronize this method to match the synchronization on "setSuspendMap".

一組get和set方法,如果其中一個方法被synchronized修飾,則另一個也需要使用synchronized 修飾

An empty statement (semicolon) not part of a loop

雙分號導致sonar認為兩個分號之間有一個空語句,刪掉一個分號即可

Avoid empty if statements

if代碼塊不能為空,刪掉或填充代碼

A class which only has private constructors should be final

有private構造函數的類需定義為final類

Class Convert2Track should be declared as final.

該類需定義為final類

Verify this is the key that was intended; it was already set before.

該key之前已被塞入過一次,一個key塞兩次沒有意義,去掉一次

Extract this nested try block into a separate method.

不要在try代碼塊中再次使用try,去掉內部的,或者把內部的try代碼塊拉到外面去

Absolute path traversal in ...

直接將文件的絕對路徑傳到后台,然后生成File對象即會報錯。如D:\1.jpg,不可直接傳絕對路徑,如果知道這個文件會在d盤,可定義常量D:\,然后傳值為1.jpg,后台進行拼接后生成File對象即可

Only one statement per line allowed.

每一行只允許一個語句,可能是多寫了分號,去掉即可

Make this anonymous inner class a lambda (sonar.java.source not set. Assuming 8 or greater.)

jdk1.8之后推薦使用蘭布達表達式。請參考:https://blog.csdn.net/qq_33865313/article/details/81203412

Non-virtual method call in ... passes null for non-null parameter of ...

將一個可能為空的參數傳到了一個方法中,但在該方法中,該參數不可為空。修改這個方法,在使用變量前進行非空判斷,則這個參數就成為了可為空參數

A "NullPointerException" could be thrown; "printStream" is nullable here.

可能會拋出空指針異常,先進行非空判斷

... could be null and is guaranteed to be dereferenced in ...

可能會拋出空指針異常,先進行非空判斷

Unused import - ...

引入了一個類,但是沒有使用,刪掉即可

2' is a magic number.

這是一個魔術數字,需要定義一個常量,然后引用常量,而不是直接使用這個數字

+' is preceded with whitespace.

代碼之前不能有空格,刪掉空格即可

;' is not followed by whitespace.

代碼之后沒有空格,需要添加一個空格

?' is not preceded with whitespace.

代碼之前沒有空格,需要添加一個空格

Redundant 'private' modifier.

這個修飾符是多余的,刪掉該修飾符即可

Incorrect lazy initialization of static field

對靜態變量的延遲初始化有問題。一般可以試着把static修飾符去掉,或者在靜態代碼塊中進行static變量的初始化。

... Service not allowed Autowired field ...

Service中不允許自動注解Manager,一般Service中都是注解其他Service,刪掉這個Manager,換成相同功能的Service

Unread field: ... should this field be static?

final修飾的字段需要加上static修飾符

member def modifier' has incorrect indentation level 8, expected level should be 4.

縮進不對,可以刪掉前面的縮進,用Tab鍵縮進,或者MyEclipse用Ctrl+Shift+F格式化代碼

Change this code to use a stronger protocol.

修改編碼以使用更強壯的協議,照着給的編碼改就行

for child at indentation level 21 not at correct indentation, 20

首行縮進不對,使用Tab鍵進行縮進

0 is a valid index, but is ignored by this check.

0是一個有效的值,但在這里不能用。一般都是要用特殊的值,比如indexOf應該使用>=0或>-1

Use already-defined constant '...' instead of duplicating its value here.

使用已經定義的常量,而不是在這里復制它的值

Using the '.*' form of import should be avoided - ...

不要使用.*的方式將所有類導入進來,而應該詳細的導入每一個用到的類


免責聲明!

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



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