| 网站首页 | 测试咨讯 | 新手入门 | 专注性能 | 测试技术 | 测试管理 | 测试工具 | 开发社区 | 工具下载 | 资料下载 | 测试论坛 | 

[CNTester联盟群]交流群:34446273/21968356/64461572 白盒群:18400216 自动化群:2706508 性能群:4498858 外包群:59649884 管理群(需有管理经验):64442523

新手入门
 入门指南
 经验之谈
测试咨讯
 行业新闻
 网站动态
 人才就业
 CNTester基金
 标准规范
专注性能
 性能测试知识
 性能测试工具
 程序设计性能
 数据库性能
 Web应用服务器性能
 操作系统性能
 服务器与网络性能
测试技术
 用例设计
 性能测试
 自动化测试
 Web测试
 面向对象测试
 综合技术
 阶段性测试
 行业类测试
测试管理
 项目管理
 项目案例
 质量管理
 软件过程
测试工具
 Mercury系列
 Rational系列
 测试管理工具
 性能测试工具
 功能测试工具
 单元测试工具
 其它测试工具
开发社区
 Java技术
 DotNet技术
 C技术
 数据库技术
 操作系统
 服务器与中间件
工具下载
 测试工具
 开发工具
 企业工程
 网络通信
 媒体桌面
 游戏娱乐
 其他工具
资料下载
 测试资料
 程序设计
 数据库
 操作系统
 应用服务器
 电子期刊
 其他资料
 
 
您现在的位置: 中国测试员网站 >> 测试工具 >> 单元测试工具 >> Parasoft系列 >> Jtest >> 文章正文
  [推荐]JTest7.59定义——JAVA 编码标准规范       ★★★ 【字体:
JTest7.59定义——JAVA 编码标准规范
作者:梁建增    文章来源:本站原创    点击数:    更新时间:2007-4-29    

一、JAVA编码标准规则

1.   JavaBeans [BEAN]

2.   编码约定/ 惯例Coding Conventions [CODSTA]

3.   契约设计/ Design by Contract [DBC]

4.   Enterprise JavaBeans [EJB]

5.   异常/ Exceptions [EXCEPT]

6.   格式化/ Formatting [FORMAT]

7.   碎片回收集/ Garbage Collection [GC]

8.   全面静态分析/ Global Static Analysis [GLOBAL]

9.   初始化/ Initialization [INIT]

10.  国际化/ Internationalization [INTER]

11.  JAVA2 微型平台/ Java 2 Micro Edition [J2ME]

12.  JAVA文档注释/ Javadoc Comments [JAVADOC]

13.  JAVA数据库连接/ Java Database Connectivity [JDBC]

14.  JavaServer Pages [JSP]

15.  JavaServer Page Metrics [JMETRICS]

16.  Junit测试用例/ JUnit Test Case [JUNIT]

17.  类结构/ Class Metrics [METRICS]

18.  不同特性/ Miscellaneous [MISC]

19.  命名约定/惯例Naming Conventions [NAMING]

20.  面向对象编程/ Object Oriented Programming [OOP]

21.  最优化/ Optimization [OPT]

22.  可能存在的BUG/ Possible Bugs [PB]

23.  简便/ Portability [PORT]

24.  安全/ Security [SECURITY]

25.  序列化/ Serialization [SERIAL]

26.  Servlets [SERVLET]

27.  Struts 框架/ Struts Framework [STRUTS]

28.  线程与同步/ Threads & Synchronization [TRS]

29.  从未使用的代码/ Unused Code [UC]

30.  安全(必要的许可)/ Security (License Required) [SLR]

31.  安全策略规范Security/  Policy Rules (License Required) [SPR]

32.  Web安全(必要的许可)/ Web Security (License Required) [WSLR]

 

二、内置与自定义的标准组

1.  内置的Jtest配置/ Built-in Jtest Configurations

2.  测试驱动开发(TDD/ Test Driven Development (TDD)

3.  Parasoft的自动错误预防组/ Parasoft's AEP group

4.  Scope Examples group

5.  其他配置/ Other configurations

6.       自定义编码标准分析:概要/ Customizing coding standard analysis: overview

 

一、JAVA编码标准规则

 

1.  JavaBeans [BEAN]

1-1.JavaBean类中为listener方法名使用适当的签名/Use appropriate signatures for listener method names in JavaBean classes [BEAN.BLNC-1]

1-2.确保JavaBean类实现了'java.io.Serializable'/Ensure that JavaBean classes implement 'java.io.Serializable' [BEAN.SERIALIZABLE-2]

1-3.JavaBean类中覆写'Object.equals()' /Override 'Object.equals()' in JavaBean classes [BEAN.EQUALS-4]

1-4.JavaBean类中不使用JDBC代码/Do not use JDBC code in JavaBean classes [BEAN.JDBC-4]

1-5.在私有字段中定义getset方法/Define get and set methods for each private field [BEAN.NFM-4]

 

2.  Coding Conventions [CODSTA]

2-1.避免在其他表达式中嵌套赋值或内部赋值/ Avoid nested assignments or assignments embedded in other expressions [CODSTA.NEA-1]

2-2.适当性的定制编辑单元成分/ Order compilation unit elements appropriately [CODSTA.ORCU-1]

2-3.在一个语句中不要断言不同类型中的多个变量/ Do not declare multiple variables of different types in one statement [CODSTA.VDT-1]

2-4.Cloneable类声明'clone() throws CloneNotSupportedException'/ Declare 'clone() throws CloneNotSupportedException' for Cloneable class [CODSTA.CLONE-2]

2-5.确保没有终结Cloneable类中的'clone()'方法声明了'CloneNotSupportedException'/ Ensure 'clone()' method of non-final Cloneable class declared to throw 'CloneNotSupportedException' [CODSTA.CLONET-2]

2-6.避免在长整型常量后有小字母的"l"或字母的"1"/ Avoid having a lower-case "l" or the number "1" at the end of a "long" integer constant [CODSTA.LONG-2]

2-7.避免字面意义的常量/ Avoid literal constants [CODSTA.USN-2]

2-8.不要使用"break" 和(或则) "continue"与符号/ Do not use "break" and/or "continue" with labels [CODSTA.ABCL-3]

2-9."if"语句中不要使用复杂的条件表达式/ Do not use complicated conditional expressions in "if" statements [CODSTA.ACCS-3]

2-10.在一个catch块中不要使用instanceof去检查异常类型/ Do not use instanceof in a catch block to check the exception type [CODSTA.AIOC-3]

2-11.不要覆写父类中的非抽象方法/ Do not override non "abstract" methods of a parent class with "abstract" methods [CODSTA.AMMO-3]

2-12.用较少的特定类型实现松散耦合/ Use less specific types to accomplish loose coupling [CODSTA.AUVT-3]

2-13."final"以大写字符名声明字段/ Declare fields with uppercase character names as "final" [CODSTA.CFNF-3]

2-14.在类中使用链构造器实现多个构造器/ Use chain constructors in classes with multiple constructors [CODSTA.CHAIN-3]

2-15.在嵌套的表达式中避免使用增量或减量的算法/ Avoid using increment or decrement operators in nested expressions [CODSTA.CID-3]

2-16.使用'clone()'方法仅仅是实现'Cloneable'接口/ Use the 'clone()' method only to implement 'Cloneable' interface [CODSTA.CLONE2-3]

2-17.强制或避免使用条件算法/ Enforce or avoid usage of conditional operators [CODSTA.CX-3]

2-18.只要可能就定义一个非自变量构造器/ Define a no argument constructor whenever possible [CODSTA.DCTOR-3]

2-19.适当的定制类元素/ Order class elements appropriately [CODSTA.FO-3]

2-20.不要在一个语句中声明多个变量Do not declare multiple variables in one statement [CODSTA.MVOS-3]

2-21.在构造器执行期间不要调用方法那样可能会引起意料不到的NullPointerExceptions/ Do not call methods that might cause unexpected NullPointerExceptions during constructor execution [CODSTA.NCNFC-3]

2-22.避免"throws"异常;而用'Exception'的子几集/ Avoid "throws" Exceptions; use subclasses of 'Exception' [CODSTA.NTX-3]

2-23.以名字来组织方法/ Organize methods by name [CODSTA.OGM-3]

2-24.当你覆写'Object.hashCode()'时也覆写'Object.equals()'/ Override 'Object.equals()' when you override 'Object.hashCode()' [CODSTA.OVERRIDE2-3]

2-25.确保一个类中仅有的"private"构造器声明为"final"/ Ensure that a class which has only "private" constructors is declared as "final" [CODSTA.PCF-3]

2-26.避免"switch"语句有多个"case"语句/ Avoid "switch" statements with many "case" statements [CODSTA.SMC-3]

2-27.每个文件中至多有一个类型/ Do not have more than one type in each file [CODSTA.AMOC-4]

2-28.不要使用符号语句/ Do not use label statements [CODSTA.AULS-4]

2-29.避免嵌套的块/ Avoid nested blocks [CODSTA.BLOCK-4]

2-30.在比较中适当的一边放置常量/ Place constants on the appropriate side of comparisons [CODSTA.CS-4]

2-31.所有形式参数声明为"final"/ Declare all formal parameters as "final" [CODSTA.FPF-4]

2-32.避免或强制在import语句中使用'*'/ Avoid or enforce usage of '*' form of import statements [CODSTA.IMPTD-4]

2-33.在一个"abstract"类中不要从构造器中调用一个"abstract"方法/ Do not call an "abstract" method from a constructor in an "abstract" class [CODSTA.NCAC-4]

2-34.不要使用'Exception', 'RuntimeException', 或者 'Throwable' "catch"语句中/Do not use 'Exception', 'RuntimeException', or 'Throwable' in "catch" statement [CODSTA.NCE-4]

2-35.避免非静态初始化/ Avoid non-static initializers [CODSTA.NSI-4]

2-36.避免多个负载的方法有同样数字的参数/ Avoid multiple overloaded methods with the same number of parameters [CODSTA.OVERLOAD-4]

2-37.当你覆写'Object.equals()'时也覆写'Object.hashCode()'/ Override 'Object.hashCode()' when you override 'Object.equals()' [CODSTA.OVERRIDE-4]

2-38.在最后放置'main()'方法/ Place the 'main() 'method last [CODSTA.PML-4]

2-39.避免或强制使用"this""super"表达式/ Avoid or enforce the use of "this" and "super" expressions [CODSTA.UATS-4]

2-40.避免所有的类型有一个非默认的包名/ Ensure all types have a non default package name [CODSTA.UNDPN-4]

2-41.if-else中避免使用负逻辑/ Avoid using negative logic in if-else statement [CODSTA.ANL-5]

2-42.在一个"interface"应定义常量/ Define constants in an "interface" [CODSTA.DCI-5]

2-43.在单一的方法中不要使用过多的'!'否定算法/ Do not use too many negation operators '!' in a single method [CODSTA.DUN-5]

2-44.不要使用一个"interface"去定义常量/ Do not use an "interface" to define constants [CODSTA.ISACF-5]

2-45."public""protected"方法间放置'finalize()'方法/ Place 'finalize()' methods between "public" and "protected" methods [CODSTA.ORFIM-5]

2-46.以整齐的字母序列呈现"import"语句/ Present "import" statements in alphabetical order [CODSTA.ORIMP-5]

2-47.覆写'toString ()'/ Override 'toString ()' [CODSTA.OTOSM-5]

 

3.  Design by Contract [DBC]

3-1.在契约设计中使用正确的约定语法/ Use correct syntax in the DbC contracts [DBC.SYNTAX-1]

3-2.为所有"public"方法提供一个'@post'约定/ Provide an '@post' contract for all "public" methods [DBC.PUBMPOST-2]

3-3.为所有"public" 类提供一个'@invariant'约定/ Provide an '@invariant' contract for all "public" classes [DBC.PUBC-2]

3-4.为所有"public"方法提供一个'@pre'约定/ Provide an '@pre' contract for all "public" methods [DBC.PUBMPRE-2]

3-5.为所有"protected"类提供一个'@invariant'约定/Provide an '@invariant' contract for all "protected" classes [DBC.PROC-3]

3-6.为所有"protected"方法提供一个'@post'约定/ Provide an '@post' contract for all "protected" methods [DBC.PROMPOST-3]

3-7.为所有"protected"方法提供一个'@pre'约定/ Provide an '@pre' contract for all "protected" methods [DBC.PROMPRE-3]

3-8.为所有私有类包提供一个'@invariant'约定/ Provide an '@invariant' contract for all package-private classes [DBC.PKGC-4]

3-9.为所有私有方法包提供一个'@post'约定/ Provide an '@post' contract for all package-private methods [DBC.PKGMPOST-4]

3-10.为所有私有方法提供一个'@pre'约定/ Provide an '@pre' contract for all package-private methods [DBC.PKGMPRE-4]

3-11.为所有"private"类提供一个'@pre'约定/ Provide an '@invariant' contract for all "private" classes [DBC.PRIC-5]

3-12.为所有"private"方法提供一个'@post'约定/ Provide an '@post' contract for all "private" methods [DBC.PRIMPOST-5]

3-13.为所有"private"方法提供一个'@pre'约定Provide an '@pre' contract for all "private" methods [DBC.PRIMPRE-5]

 

4.  Enterprise JavaBeans [EJB]

4-1.声明bean类为"public"/ Declare bean classes "public" [EJB.CDP-1]

4-2.不要以"abstract"声明bean/ Do not declare bean classes as "abstract" [EJB.CNDA-1]

4-3.不要以"final"声明bean/ Do not declare bean classes as "final" [EJB.CNDF-1]

4-4.声明'ejbCreate()'方法为"public",但即不能为"static"也不能为"final"/ Declare 'ejbCreate()' methods "public", but neither "static" nor "final" [EJB.CRTE-1]

4-5.声明finder方法为"public",不过即不能为"static"也不能为"final"/ Declare finder methods "public" and neither "final" nor "static" [EJB.FNDM-1]

4-6.在一个bean类中实现一个或多个'ejbCreate ()'方法/ Implement one or more 'ejbCreate ()' methods in bean classes [EJB.IECM-1]

4-7.在一个实体bean类中实现一个或多个'ejbPostCreate()'方法/ Implement one or more 'ejbPostCreate()' methods in EntityBean classes [EJB.IEPM-1]

4-8.在所有消息驱动bean类中实现'ejbCreate()'方法/ Implement the 'ejbCreate()' method for all Message-driven bean classes [EJB.MDBC-1]

4-9.在实体bean类中为每个'ejbCreate'方法定义一个匹配的'ejbPostCreate()'方法/ Define a matching 'ejbPostCreate()' method for each 'ejbCreate' method in entity bean classes [EJB.MEC-1]

4-10.bean类中不要定义'finalize ()'方法/ Do not define 'finalize ()' method in bean classes [EJB.MNDF-1]

4-11.为远程interface和远程home interface的方法抛出'java.rmi.RemoteException'/ Throw 'java.rmi.RemoteException' in the methods of remote interface and remote home interface [EJB.MRE-1]

4-12.避免在消息驱动Bean'ejbCreate()'方法有自变量/ Avoid arguments in MessageDrivenBeans' 'ejbCreate()' method [EJB.NAC-1]

4-13.传入无参数则声明一个"public"构造器/ Declare a "public" constructor that takes no parameters [EJB.NFDC-1]

4-14.声明'ejbPostCreate()'"public",不过即不能为"static"也不能为"final"/ Declare 'ejbPostCreate()' "public" and neither "static" nor "final" [EJB.PCRTE-1]

4-15.在一个bean的局部interface和局部home interface中不要抛出'java.rmi.RemoteException'/ Do not throw 'java.rmi.RemoteException' in a bean's local interface and local home interface [EJB.RILH-1]

4-16.构造finder方法返回类型为primary key或一个primary key集合/ Make finder methods' return type the primary key or a collection of primary keys [EJB.RT-1]

4-17.SessionBeansMessageDrivenBeans'ejbCreate()'方法构造返回类型"void"/ Make the return type "void" for SessionBeans or MessageDrivenBeans' 'ejbCreate()' methods [EJB.RTC-1]

4-18.'ejbPostCreate()'方法构造返回类型"void"/ Make the return type "void" for the 'ejbPostCreate()' method [EJB.RTP-1]

4-19.Home interface中为EJB remote, home interfacesbean类和确保包括所有business方法和'create''finder'方法的EJB bean遵循格式化名/ Follow the name format for EJB remote, home interfaces, and bean classes and ensure EJB bean contains all the business methods and 'create' and 'finder' methods in the Home interface [EJB.STD-1]

4-20.远程home或局部home interfaces在创建方法时抛出'javax.ejb.CreateException'/ Throw 'javax.ejb.CreateException' in create methods of remote home or local home interfaces [EJB.TCE-1]

4-21.远程home或局部home interfacesfinder方法时抛出'javax.ejb.FinderException'/ Throw 'javax.ejb.FinderException' in finder methods of remote home or local home interfaces [EJB.TFE-1]

4-22.自变量避免通过"this"引用/ Avoid passing the "this" reference as an argument [EJB.THISARG-1]

4-23.不要返回"this"/ Do not return "this" [EJB.THISRET-1]

4-24.不要访问或修改安全配置对象/ Do not access or modify security configuration objects [EJB.AMSC-2]

4-25.'ejbLoad()'中不要调用finder方法/ Do not call finder methods in the 'ejbLoad()' [EJB.EJBLOAD-2]

4-26.在一个Bean中避免加载原生库/ Avoid loading native libraries in a Bean class [EJB.LNL-2]

4-27.EJB组件"final"时声明所有"static"字段/ Declare all "static" fields in the EJB component "final" [EJB.NFS-2]

4-28.重新利用EJB homes/ Reuse EJB homes [EJB.RUH-2]

4-29.在任何地方避免开始,停止或管理线程/ Avoid starting, stopping, or managing threads in any way [EJB.THREAD-2]

4-30.使用Session外观去控制访问实体beans/ Use a Session Facade to manage access to entity beans [EJB.USF-2]

4-31.EJB类中不要使用Servlet代码/ Do not use Servlet code in EJB classes [EJB.ABCS-3]

4-32.java swingervlet类中不要使用EJB代码/ Do not use EJB code in java swing or servlet classes [EJB.ADCB-3]

4-33.避免过多的inter-entity bean通讯/ Avoid excessive inter-entity bean communication [EJB.AIEBC-3]

4-34.EJB类内不要使用JDBC代码/ Do not use JDBC code inside of EJB classes [EJB.AJDBC-3]

4-35.session beansentity beans间避免一对一的映射/ Avoid one-to-one mapping between session beans and entity beans [EJB.AOTO-3]

4-36.可再利用的高速缓存JNDI资源将减少使用高代价的运算/ Cache reusable JNDI resources to minimize the use of expensive operations [EJB.UCIC-3]

4-37.fine-grained对象不要使用Entity Beans/ Do not use Entity Beans as fine-grained objects [EJB.ABFG-4]

4-38.使用有价值对象减少对服务器的调用/ Use value Objects to reduce the granularity of calls to the server [EJB.UVO-4]

 

5.  Exceptions [EXCEPT]

5-1.所有字段中用户定义的'Exception'声明为"final"/ Declare all fields of user-defined 'Exception' as "final" [EXCEPT.IMMEX-3]

5-2.不要捕获'java.lang.Error'对象/ Do not catch the 'java.lang.Error' object [EXCEPT.NCERR-3]

5-3.不要捕获'NullPointerException'/ Do not catch 'NullPointerException' [EXCEPT.NCNPE-3]

5-4.不要抛出'java.lang.Error'对象/ Do not throw the 'java.lang.Error' object [EXCEPT.NTERR-3]

5-5.在控制流的语句中不要滥用异常/ Do not abuse exceptions as flow control statements [EXCEPT.AEFC-4]

5-6.不要抛出'NullPointerException'/ Do not throw 'NullPointerException' [EXCEPT.NTNPE-5]

 

6.  Formatting [FORMAT]

6-1.每行书写一个语句/ Write one statement per line [FORMAT.OSPL-1]

6-2.强制在主要的模块间执行数个空行/ Enforce number of blank line(s) between major sections [FORMAT.U2BL-1]

6-3.强制缩进执行数个空格/ Enforce number of space(s) for indentation [FORMAT.IND-2]

6-4.限制每行的最大长度/ Limit the maximum length of a line [FORMAT.LL-2]

6-5.每个源文件提供一个正确的文件标题/ Provide a correct file header comment for each source file [FORMAT.MCH-2]

6-6.条件表达式采用首选格式化/ Use the preferred formatting for conditional expressions [FORMAT.TE-2]

6-7.使用'()'分隔复杂的表达式/ Use '()' to separate complex expressions [FORMAT.APAREN-3]

6-8.在独有的行放置大括号/ Place a closing brace on its own line [FORMAT.CBRACE-3]

6-9.类型分配后放置一个单独的空格符号或否/ Place a single space character or no space character after type casting [FORMAT.CMS-3]

6-10.用空格代替'Tabs'/ Use spaces instead of 'Tabs' [FORMAT.DUT-3]

6-11.'[]'括号声明arrays在队列类型后和变量名前/ Declare arrays with '[]' brackets after the array type and before the variable name(s) [FORMAT.IAD-3]

6-12.在一个方法名和打开的"("圆括号间放置一个单独的空格字符或否/ Place a single space character or no space character between a method name and the opening "(" parenthesis [FORMAT.MSP-3]

6-13.不要在'{'打开括号相同的行放置语句/ Do not place statements on the same line as the '{' opening brace [FORMAT.NSAB-3]

6-14.强制在所有逗号后执行数个空格字符/ Enforce number of space character(s) after every comma [FORMAT.SAC-3]

6-15.强制在赋值算法每一边执行数个空格字符/ Enforce number of space character(s) on each side of an assignment operator [FORMAT.SAOP-3]

6-16.强制在条件运算的打开圆括号"("后执行数个空格字符/  Enforce number of space character(s) after the opening parenthesis "(" of a conditional statement [FORMAT.SAP-3]

6-17.强制在所有分号后执行数个空格字符/ Enforce number of space character(s) after every semicolon [FORMAT.SAS-3]

6-18.强制在一个前缀一元操作和操他的作数间执行数个空格字符/ Enforce number of space character(s) between a prefixed unary operator and its operand [FORMAT.SAUOP-3]

6-19.强制在位运算每一边执行数个空格字符/ Enforce number of space character(s) on each side of a bitwise operator [FORMAT.SBOP-3]

6-20.强制在一个后缀一元操作和操他的作数间执行数个空格字符/Enforce number of space character(s) between a postfixed unary operator and its operand [FORMAT.SBUOP-3]

6-21.强制在条件算法"?"前和后执行数个空格字符/ Enforce number of space character(s) before and after the "?" conditional operator [FORMAT.SCOP-3]

6-22.强制在逻辑运算每一边执行数个空格字符/ Enforce number of space character(s) on each side of a logical operator [FORMAT.SLOP-3]

6-23.强制在关系运算每一边执行数个空格字符/ Enforce number of space character(s) on each side of a relational operator [FORMAT.SROP-3]

6-24.避免不必要的圆括号/ Avoid unnecessary parentheses [FORMAT.UP-3]

6-25.强制在类型声明前执行数个空行/ Enforce number of blank line(s) before type declarations [FORMAT.BLCD-4]

6-26.强制执行数个空行去分隔不同的"imports"/ Enforce number of blank line(s) to separate "imports" from different packages [FORMAT.BLSIM-4]

6-27.强制执行'{'大括号的位置/ Enforce the position of '{' brace [FORMAT.FCB-4]

6-28.放置跟第一个类型有相同文件名的类型/ Place the type that has the same name as the file as the first type [FORMAT.FCN-4]

6-29.强制定制注释和修改人/ Enforce the order of annotations and modifiers [FORMAT.MO-4]

6-30.确信所有文件终止于换行字符/ Make sure all files are terminated with a newline character [FORMAT.TNL-4]

6-31.不要在行末放置空个空格区字段/ Do not place empty whitespace at the end of a line [FORMAT