代码规范手册
代码规范使用checkStyle 插件来辅助开发。
Check Style 安装方法如下:
打开 Myeclipse->Help->Search for new features to install ->new Remote site ->输入URL:http://eclipse-cs.sourceforge.net/update ->安装->重启Myeclipse->完成
编码要求 |
是否 可控 |
备注 |
|
版权信息 |
Y |
/** * Test1.java * Cheney([email protected]) * 2010-06-29 * copyright : Copyright (c) 2010 FESCO Inc. All Rights Reserved. * * modifications: * 这仅仅是个测试类 * */ |
|
包名规范 |
Y |
全为小写字母 |
|
命名规范 |
类定义 |
Y |
首字母大写 |
接口定义 |
Y |
首字母大写 |
|
抽象类定义 |
Y |
首字母大写,并且前面必须有Abstract或结尾以Factory例如:AbstractItest.java或者ItestFactory |
|
常量 |
Y |
全为大写字母 |
|
Static final变量规范 |
Y |
全为大写字母 |
|
普通变量 |
Y |
首字母小写 |
|
Variable declare |
数组定义 |
Y |
采用java数组方式定义; 例如:int [] array; |
列表定义 |
存在bug |
存在的bug在于,是否要是使用泛型。如果使用泛型会提示空格问题。 New List < String > (); 如果不使用泛型就不会出现警告。 原因是因为,’>’ 被认为是逻辑运算符号,要求逻辑运算符前后都要有空格。但是’(‘要求不需要有空格。所以冲突。 如不使用泛型,解决办法: 在方法和属性的上一行加上一句注释即可。 @SuppressWarnings("unchecked") |
|
Collection定义 |
存在bug |
同上 |
|
Java Doc |
Class Doc Comment |
Y |
强制必须写注释 |
Variable Doc Comment |
Y |
变量注释 |
|
Method Doc Comment |
Y |
方法注释包括入参param,return例如: /** * 描述. * @param test 是一个入参 * @return tag 返回一个字符串 */ |
|
Coding syntax |
if |
Y |
关键字后需要一个空格,逻辑运算符前后需要一个空格,左括号需要前需要一个空格,右括弧后面需要一个空格。等号两边需要有空格。 一下同理 if (idd > 6) { idd = 100; } else { idd = -100; } |
switch |
Y |
此处沿用上面介绍的格式要求,唯一特例是,每个switch必须要有一个default switch (idd) { case 1: idd = 3; break; case 2: idd = 4; break; default: idd = 0; break; } |
|
For |
Y |
此处沿用上面介绍的格式要求 for (int i = 0; i < 10; i++) {} |
|
While |
Y |
此处沿用上面介绍的格式要求 while (idd < 10) { idd--; } |
|
Do..while |
Y |
此处沿用上面介绍的格式要求 do { idd++; } while (idd < -1); |
|
Iterator |
Y |
此处沿用上面介绍的格式要求 while (iter.hasNext()) { iter.next(); } |
|
convert |
Y |
同上 |
|
Coding Style |
缩进要求 |
Y |
注意一点:由于习惯使用tab缩进,tab缩进是使用4个字符的制表符,但是check Style认为可能其他编译器不支持tab字符,会影响代码的编译,所以要使用空格代替。在eclipse设置方法如下: 设置步骤:窗口(windows)->首选项(preferences...), 在左边选Java->代码样式(code style)->格式化程序(Formatter), 右边点“显示(show)”按钮,选“缩进(Indentation)”选项卡, 在“常规设置(general settings)”里的“跳格策略(Tab policy)” 下拉列表选“仅空格(Spaces only)” 。 在eclipse中设置tab size的地方有多个 1:window——preference——General——Editor——Text Editor设置页面:Display Tab Width 2:window——preference——Java——Code Style——Formatter设置页面,Edit,在弹出的Editor profile窗口中,Indentation卡片,设置Indentation Size和Tab Size。 3:如果安装了Myeclipse,那么在window——preference——Myeclipse——Editor——Common Editor Preference中Apperance卡片,设置Tab Size。 对Java文件,第2中是可行的。设置1、3没有用。 对jsp文件,还有一个位置可以设置: 4、打开jsp文件,打开右键菜单,进入“Preferences”,里面有Display tab width。 |
Checkstyle常见的错误提示
1 Type is missing a javadoc commentClass 缺少类型说明
2“{” should be on the previous line “{” 应该位于前一行
3Methos is missing a javadoc comment方法前面缺少javadoc注释
4Expected @throws tag for “Exception”在注释中希望有@throws的说明
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”,不需注释
11Variable “CA” missing javadoc变量“CA”缺少javadoc注释
12Line longer than 80characters行长度超过80
13Line contains a tab character行含有”tab” 字符
14Redundant “Public” modifier冗余的“public” modifier
15Final modifier out of order with the JSL suggestionFinal 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]*$”
24“(” is followed by whitespace“(” 后面不能有空格 25“)” is proceeded by whitespace“)” 前面不能有空格
配置文件,将其拷贝保存为xxx.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
This configuration file was written by the eclipse-cs plugin configuration editor
-->
<!--
Checkstyle-Configuration: TEST
Description:
TEST
-->
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
<module name="JavadocMethod">
<property name="scope" value="public"/>
<property name="logLoadErrors" value="true"/>
<property name="suppressLoadErrors" value="true"/>
</module>
<module name="JavadocType"/>
<module name="JavadocVariable"/>
<module name="JavadocStyle">
<property name="scope" value="public"/>
<property name="checkFirstSentence" value="false"/>
<property name="tokens" value="CLASS_DEF,CTOR_DEF,INTERFACE_DEF,METHOD_DEF,VARIABLE_DEF"/>
</module>
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>
<module name="AvoidStarImport"/>
<module name="IllegalImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<module name="FileLength"/>
<module name="LineLength"/>
<module name="MethodLength">
<property name="max" value="200"/>
<property name="countEmpty" value="false"/>
</module>
<module name="ParameterNumber"/>
<module name="EmptyForIteratorPad"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter">
<property name="tokens" value="ARRAY_INIT,BNOT,DEC,INC,LNOT,UNARY_MINUS,UNARY_PLUS,TYPECAST"/>
</module>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad">
<property name="tokens" value="RPAREN,TYPECAST"/>
</module>
<module name="TabCharacter"/>
<module name="WhitespaceAfter">
<property name="tokens" value="COMMA,SEMI"/>
</module>
<module name="WhitespaceAround"/>
<module name="ModifierOrder"/>
<module name="RedundantModifier">
<property name="severity" value="ignore"/>
</module>
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>
<module name="AvoidInlineConditionals"/>
<module name="DoubleCheckedLocking"/>
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber">
<property name="severity" value="ignore"/>
<property name="ignoreNumbers" value="-1, 0, 1, 2,3,4,5,6,7,8,9,10"/>
</module>
<module name="MissingSwitchDefault"/>
<module name="RedundantThrows">
<property name="logLoadErrors" value="true"/>
<property name="suppressLoadErrors" value="true"/>
</module>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier"/>
<module name="ArrayTypeStyle"/>
<module name="GenericIllegalRegexp"/>
<module name="TodoComment"/>
<module name="UpperEll"/>
<module name="Indentation">
<property name="caseIndent" value="0"/>
</module>
<module name="Header">
<property name="headerFile" value="D:\cheney\MyEclipse 6.5\myeclipse\eclipse\plugins\com.atlassw.tools.eclipse.checkstyle_4.4.3\java.header"/>
<property name="ignoreLines" value="2,3,4,6,8,9"/>
</module>
<module name="j2ee.FinalStatic"/>
<module name="UnnecessaryParentheses"/>
<module name="EmptyForInitializerPad"/>
<module name="AbstractClassName"/>
<module name="PackageName">
<property name="format" value="^[a-z]+(\.[a-z_][a-z0-9_]*)*$"/>
</module>
</module>
<module name="NewlineAtEndOfFile"/>
<module name="Translation"/>
</module>
导入方法
WindowàpreferencesàCheck style
点击New按钮
选择External Configuration File,然后填写name,description。点击browse选择xxx.xml文件。
然后ok,添加完成。回到管理界面。你可以将自己创建的设置为默认的。
或者,指定项目使用此检测分析。
针对某一个project使用。
选择该project的properties点击check style
点击configuration按钮可以配置验证规则。如果修改验证规则,建议采用此种方式。不推荐直接修改xml文件。因为如果修改1是没有提示信息,2是还需要重新导入xml。