checkStyle 錯誤普及


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“)” 前面不能有空格

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

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

 

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

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

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

 

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

 

28Line 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.

 

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

31Got 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、因為編譯好的類沒有在checkstyleclasspath.所以只要將編譯好的class配置到在<checkstyle/>classpath中就沒有這個問題了.另外還發現checkstyleline length好像也有點問題明明沒有超過120個字符卻還是報錯.無奈我把Eclipsejava > code style > formatter中的Maximum line with改成了100, 然后format一下基本就沒有問題了

 

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

 

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

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

 

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

解決方法:給這些變量的修飾符改成private,然后提供setget方法,並加上對應的方法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;
    }

 

 

36Got an exception - Unexpected character 0xfffd in identifier

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

網上參考解決方法:

1Eclipse中可以配置,在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*.
    
網上參考解決方法:選中CheckSytleJavaDoc --> Method JavaDoc --> logLoadErrors。如果是CheckStyle自己加載時出錯的,打個Log就可以了,不要整出Errors嚇人。
   
還有一處也可能包出同樣的錯誤。Coding Problems --> Redundant Throws --> logLoadErrors選中即可

 

38Expected @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.解釋:代碼寫法,習慣不一樣。需要檢查,僅僅提示


免責聲明!

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



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