check style 分析

checkStyle結果分析

參考:http://commons.apache.org/jelly/libs/http/checkstyle-report.html

1.Missing a Javadoc comment:缺少JavaDoc註釋
2.First sentence should end with a period:你的註釋的第一行文字結束應該加上一個"."
3.Expected @throws tag for 'Exception':在註釋中希望有@throws的說明,在方法前得註釋中添加這樣一行:* @throws Exception if has error(異常說明)
4.Parameter docType should be final:參數docType應該爲final類型 解決方法:在參數docType前面加個final
5.Variable “ABC” must match pattern “^[a-z][a-zA-Z0-9]*$”變量“ABC”不符合命名規則“^[a-z][a-zA-Z0-9]*$”解決方法:把這個命名改成符合規則的命名 “aBC”
6.Utility classes should not have a public or default constructor. 接口中的內部類中不應該有公共的或者默認的構造方法
解決方法:在內部類中,定義一個私有的構造方法,然後內部類聲明爲final類型。如果前面有static,那麼final還必須放在static之後
7.'{' is not preceded with whitespace.大括號後面必須空一格

8.'public' modifier out of order with the JLS suggestions. public順序錯誤

9.Method 'deleteChild' is not designed for extension - needs to be abstract, final or empty. 不是拓展或繼承的方法,必須指定abstract,final或空

1Type is missing a javadoc commentClass 缺少類型說明

2“{” should be on the previous line“{” 應該位於前一行。解決方法:把“{”放到上一行去

3Methos is missing a javadoc comment 方法前面缺少javadoc註釋。解決方法:添加javadoc註釋 類似這樣:

/**

* set default mock parameter.(方法說明)

* @param additionalParameters parameter additional(參數名稱)

* @return data manager(返回值說明)

* @throws Exception if has error(異常說明)

*/

4 Expected @throws tag for “Exception”在註釋中希望有@throws的說明

解決方法:在方法前得註釋中添加這樣一行:* @throws Exception if has error(異常說明)

5“.” Is preceeded with whitespace “.” 前面不能有空格。解決方法:把“(”前面的空格去掉

6“.” Is followed by whitespace“.” 後面不能有空格。解決方法:把“)”後面的空格去掉

7“=” is not preceeded with whitespace“=” 前面缺少空格。解決方法:在“=”前面加個空格

8“=” is not followed with whitespace“=” 後面缺少空格。解決方法:在“=”後面加個空格

9“}” should be on the same line“}” 應該與下條語句位於同一行。解決方法:把“}”放到下一行的前面

10Unused @param tag for “unused”沒有參數“unused”,不需註釋

解決方法:“* @param unused parameter additional(參數名稱)” 把這行unused參數的註釋去掉“

11Variable “CA” missing javadoc變量“CA”缺少javadoc註釋

解決方法:在“CA“變量前添加javadoc註釋:/** CA. */(注意:一定記得加上“.”)

12Line longer than 80characters行長度超過80 。解決方法:把它分成多行寫。必要時候,可以ctrl+shift+f

13Line contains a tab character行含有”tab” 字符。快速解決方法:可以使用editplus中的format功能,把tab字符轉化爲空格,然後保存Editplus英文版安裝文件在我機子上有。需要的可以來拷貝。註冊Editplus,點擊安裝文件中註冊的文件


14Redundant “Public” modifier冗餘的“public” modifier 。解決方法:冗餘的“public”

15Final modifier out of order with the JSL suggestion Final modifier的順序錯誤

16Avoid using the “.*” form of importImport格式避免使用“.*”

17Redundant import from the same package從同一個包中Import內容

18Unused import-java.util.listImport進來的java.util.list沒有被使用。解決方法:去掉導入的多餘的類

19Duplicate import to line 13重複Import同一個內容 解決方法:去掉導入的多餘的類

20Import from illegal package從非法包中 Import內容

21“while” construct must use “{}” “while” 語句缺少“{}”

22Variable “sTest1” must be private and have accessor method變量“sTest1”應該是private的,並且有調用它的方法

23Variable “ABC” must match pattern “^[a-z][a-zA-Z0-9]*$”變量“ABC”不符合命名規則“^[a-z][a-zA-Z0-9]*$”解決方法:把這個命名改成符合規則的命名 “aBC”

24“(” is followed by whitespace“(” 後面不能有空格 25“)”is proceeded by whitespace“)” 前面不能有空格

解決方法:把前面或者後面的空格去掉

25、First sentence should end with a period.解決方法:你的註釋的第一行文字結束應該加上一個"."。


26、Redundant throws: 'NameNotFoundException' is subclass of 'NamingException'. 'NameNotFoundException '是'NamingException'的子類重複拋出異常。

解決方法:如果拋出兩個異常,一個異常類是另一個的子類,那麼只需要寫父類

去掉NameNotFoundException異常,對應的javadoc註釋異常註釋說明也需要去掉


27、Parameter docType should be final. 參數docType應該爲final類型 解決方法:在參數docType前面加個final


28、Line has trailing spaces. 多餘的空行 解決方法:去掉這行空行


29.Must have at least one statement. 至少一個聲明

解決方法:} catch (NumberFormatException nfe) {

LOG.error("Auto Renews the agreement failed", nfe);//異常捕捉裏面不能爲空,在異常裏面加一句話。如打印等等

30、'>' is not followed by whitespace.並且又有 '(' is preceded with whitespace.

定義集合和枚舉的時候的時候,最後一個“>”後面要有空格,“(”前面不容許有空格。解決方法:去掉泛型


31、Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag 'SystemException'.原因:不合理的throws。

解決方法:要確保某些類型,如某些類、接口不被throws。把聲明的異常去掉。在實現類中拋出異常


網上參考解決方法:1、這是CheckStyle報的錯。通常需要Refreh, clean/build這個Project. 如果不行,可以嘗試clean all projects, restart Eclipse.

2、因爲編譯好的類沒有在checkstyle的classpath中.所以, 只要將編譯好的class配置到在<checkstyle/>的classpath中就沒有這個問題了.另外, 還發現checkstyle的line length好像也有點問題, 明明沒有超過120個字符, 卻還是報錯.無奈, 我把Eclipse中java > code style > formatter中的Maximum line with改成了100, 然後format一下, 基本就沒有問題了


32、File does not end with a newline.解決方法:刪掉報錯的類,新建一個同名的類,把代碼全部複製過去


33、Utility classes should not have a public or default constructor. 接口中的內部類中不應該有公共的或者默認的構造方法

解決方法:在內部類中,定義一個私有的構造方法,然後內部類聲明爲final類型。如果前面有static,那麼final還必須放在static之後


34、Variable 'functionCode' must be private and have accessor methods.變量要改成private然後提供訪問的方法

解決方法:給這些變量的修飾符改成private,然後提供set,get方法,並加上對應的方法javadoc註釋、參數註釋。並在返回值和參數類型前添加final。並把調用了這個變量的地方改成通過方法訪問


35. 'X' hides a field.

public class Foo
{
private int bar;

public Foo(int bar)
{
this.bar = bar;
}

public final int getBar()
{
return bar;
}
}

全局private int bar;和局部public Foo(int bar)的bar變量名字重複。
解決方法:把方法裏面的參數名稱改變下就可以了public Foo(int newBar)
{
this.bar = newBar;
}。

36、Got an exception - Unexpected character 0xfffd in identifier

這是因爲CheckStyle不能識別制定的編碼格式。

網上參考解決方法:

1、Eclipse中可以配置,在Other-->checker中可以指定

2、可以修改checkstyle配置文件:

<module name="Checker">

<property name="severity" value="warning"/>

<property name="charset" value="UTF-8"/>

<module name="TreeWalker">

如果是UTF-8的話,就添加加粗的那條語句,就可以了。

37、 Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag *whatever*.
網上參考解決方法:選中CheckSytle的JavaDoc --> Method JavaDoc --> logLoadErrors。如果是CheckStyle自己加載時出錯的,打個Log就可以了,不要整出Errors嚇人。
還有一處也可能包出同樣的錯誤。Coding Problems --> Redundant Throws --> logLoadErrors選中即可


38、Expected @param tag for 'dataManager'. 缺少dataManager參數的註釋 解決方法:在註釋中添加@param dataManager DataManager

網上一些其他錯誤的解答:
1. Parameter X should be final.
public class Foo
{
private int bar;

public Foo(int bar)
{
this.bar = bar;
}

public final int getBar()
{
return bar;
}
}

解釋:public Foo(int bar)的局部變量,被認爲是不可改變的,檢查需要加上final關鍵字定義public Foo(final int bar)此錯誤,可以忽略不檢查。

2. Redundant 'X' modifier.

public interface CacheHRTreeService extends Manager {

/**
* Organization Tree
* @param orgDto
* @return
* @throws Exception
*/
public void setOrganization(OrganizationDTO orgDto) throws Exception;

/**
* Organization Tree
* @return
* @throws Exception
*/
public OrganizationDTO getOrganization() throws Exception;
......
}

解釋:多餘的字段。public OrganizationDTO getOrganization() throws Exception;此時public爲多餘的字段,因爲interface定義的時候,就是public的。

需要檢查。

3. - Class X should be declared as final.

解釋:對於單例設計模式,要求返回唯一的類對象。但是HRFactory和ContextFactory爲優化的兩個類,不需求檢查。
其他的單例類,依然需要進行檢查。

4. - Method 'addChildrenId' is not designed for extension - needs to be
abstract, final or empty.

解釋:通過父類繼承的,此類有點特殊可以忽略此類。

5. Variable 'id' must be private and have accessor methods.解釋:BaseHRDTO類,爲父類,屬性給子類繼承,比較特殊。但是其他的類,聲名需要加上範圍'private'關鍵字。需要檢查。

6. -Array brackets at illegal position.解釋:代碼寫法,習慣不一樣。需要檢查,僅僅提示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章