【Eclipse提高开发速度-插件篇】Checkstyle的使用

1.CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发者遵守某些编码规范的工具。

CheckStyle提供了大部分功能都是对于代码规范的检查

CheckStyle检验的主要内容 
·Javadoc凝视 
·命名约定 
·标题 
·Import语句 
·体积大小 
·空白 
·修饰符 
·块 
·代码问题 
·类设计 
·混合检查(包活一些实用的比方非必须的System.out和printstackTrace)


一、CheckStyle 安装方法


安装的的时候能够通过

Help》Eclipse Marketplace的 搜索方式安装

或者

Help》Install New Software 

name:eclipse-cs

Location:http://eclipse-cs.sourceforge.net/update


一些CheckStyle 的说明

1 Type is missing a javadoc commentClass  
缺少类型说明


2 “{” should be on the previous line    
“{” 应该位于前一行。解决方法:把“{”放到上一行去


3 Methos 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“}” 
应该与下条语句位于同一行。

解决方法:把“}”放到下一行的前面


10 Unused @param tag for “unused”
没有參数“unused”。不需凝视
解决方法:“* @param unused parameter additional(參数名称)” 把这行unused參数的凝视去掉“


11 Variable “CA” missing javadoc
变量“CA”缺少javadoc凝视
解决方法:在“CA“变量前加入javadoc凝视:/** CA. */(注意:一定记得加上“.”)


12 Line longer than 80characters
行长度超过80  。解决方法:把它分成多行写。必要时候,能够ctrl+shift+f


13 Line contains a tab character
行含有”tab” 字符。


14 Redundant “Public” modifier
冗余的“public” 修饰符 。解决方法:去掉“public”修饰符


15 Final modifier out of order with the JSL suggestion
Final modifier的顺序错误


16 Avoid using the “.*” form of import
import格式避免使用“.*”。解决方法:能够先不加入import,再须要用到时直接“ctrl + o”导入就可以。


17 Redundant import from the same package
从同一个包中Import内容。多余。删掉就可以


18 Unused import-java.util.list
Import进来的java.util.list没有被使用。解决方法:去掉导入的多余的类


19 Duplicate import to line 13
反复Import同一个内容。解决方法:去掉导入的多余的类


20 Import from illegal package
从非法包中 Import内容


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


22 Variable “sTest1” must be private and have accessor method
变量“sTest1”应该是private的,而且有调用它的方法


23 Variable “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


39 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)的局部变量,被觉得是不可改变的,检查须要加上finalkeyword定义public Foo(final int bar)此错误。能够忽略不检查。


40 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的。


须要检查。


41 Class X should be declared as final.


解释:对于单例设计模式,要求返回唯一的类对象。可是HRFactory和ContextFactory为优化的两个类。不需求检查。
其它的单例类,依旧须要进行检查。




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


解释:通过父类继承的,此类有点特殊能够忽略此类。


43 Variable 'id' must be private and have accessor methods.


解释:BaseHRDTO类,为父类。属性给子类继承,比較特殊。可是其它的类,声名须要加上范围'private'keyword。须要检查。


44 Array brackets at illegal position.


解释:代码写法,习惯不一样。

须要检查。只提示