静态检查规范
Avoid Array Loops
数组之间的拷贝使用System.arrayCopy更加高效
byte[] ReceiveBytes = new byte[length1+ length2];
for (int i = 0; i < length1; i++) {
ReceiveBytes[i] =ReceiveBytes_temp1[i];
}
Big Integer Instantiation
避免创建已经存在的Big Integer对象 ,如:(BigDecimal.ZERO,BigDecimal.ONE, BigDecimal.TEN)
Boolean Instantiation
避免创建已经存在的Boolean 对象;如:Boolean.TRUE, Boolean.FALSE
Final field Could Be Static
有Final修饰符的成员变量必须是静态的
Explicitly invokes garbage collection
避免显示调用垃圾回收
Inefficient use of keySet iterator instead of entrySetiterator
低效利用使用keySet迭代器而不是entrySet迭代器。
使用entrySet效率会比keySet高
for (String key : map.keySet()) {
//to do some thing
}
for (Entry entry : map.entrySet()) {
//to do some thing
}
Method Concatenates String Using + In Loop
避免在循环中使用“+” 连接字符串。使用Stringbuffer 或Stringbuilder
private method is never called
定义为Private类型方法从未被调用,应该被删除
Use ArrayList Instead Of Vector
使用ArrayList 替换Vector
Use Arrays As List
如果从数组转换成一个List,用Arrays. AsList() 替换遍历数组的形式转换
UnnecessarilyLocal Before Return
避免创建无用的局部变量,如:
String s = getxx();
return s;
直接替换为return getxx();
Use IndexOf Char
当参数是单个字符的时候使用 String.indexOf(char)替换String.indexOf(String)
比如:用s.indexOf(‘a‘) 代替s.indexOf(“a”)
Method Invokes inefficient new String() constructor
如:String s = new String();
正确写法 String s = “”;
Method Invokes inefficient new String(string) constructor
如:String s = new String(“test”);
正确写法 String s = “test”;
Method Invokes inefficient Number constructor
Long, Integer, Short, Character, and Byte 使用valueOf代替直接实例化
Number 类型从-128 到127会缓存到常量池,可以节省内存
Integer i = new Integer(4);
Integer j = new Integer(4);
System.out.println(i==j);// false
i = Integer.valueOf(4);
j = Integer.valueOf(4);
System.out.println(i==j); // true
i = Integer.valueOf(128);
j = Integer.valueOf(128);
System.out.println(i==j);//false
Primitive value is boxed then unboxed to perform primitivecoercion
对原始值进行装箱然后立即把它强制转换为另外一种原始类型。例如:
new Double(d).intValue()应该直接进行强制转换例如:(int)d
Class defines equals() but not hashCode()
Unused import
无用的包导入
Unused local variable
无用的局部变量
Unused formal parameter
未用的常规参数:避免传递给方法或构造器不使用的参数
TODO Comment
代码中包含TODO注释
Empty Statement
避免使用空代码块
Don't Import Java.Lang
Collapsible If Statements
有时候两个 if 语句可以通过布尔短路操作符分隔条件表达式组合成一条语句
如:
If(a==b){
If(c==1){
//do some thing
}
}
Avoid Decimal Literals In BigDecimal Constructor
避免在 BigDecimal 类型的构造方法中用小数类型的字面量:人们常常以
为”new BigDecimal(0.1)”能精确等于 0.1, 其实不然,它等于“ 0. 1000000000000000055511151231257827021181583404541015625 ”,这 种状况的原因是 0.1 不能精确的表示双精度类型,因此,传入构造器的 long 类型不等于 0.1 ,而传入 String 类型的构造器 new BigDecimal(“0.1”) 可以精确等于 0.1, 故推荐这种情形时用 String 类型的构造器
Broken Null Check
破坏空检查:如果自身抛出空指针异常空检查就会遭到破坏,比如你使用 || 代替 && ,反之亦然。
if (string!=null ||!string.equals("")) { // 这里应该是&&
return string;
}
Close Resource
关闭资源:确保这些资源(譬如:Connection,Statement,和 ResultSet 对象)总在使用后被关闭
Compare Objects With Equals
对象相等性比较:使用 equals()比较对象的引用,避免使用”==”来比较
"." used for regular expression
String的split,replaceAll等方法传递的参数是正则表达式,正则表达式本身用到的字符需要转义,如:句点符号“.”,美元符号“$”,乘方符号“^”,大括号“{}”,方括号“[]”,圆括号“()”,竖线“|”,星号“*”,加号“+”,问号“?”等等,这些需要在前面加上“\\”转义符。
如:s = s.replaceAll(".", "/"); 应该使用s =s.replaceAll("\\.", "/");
An apparent infinite loop
明显的无限循环
An apparent infinite recursive loop
明显的无限迭代循环,将导致堆栈溢出
Equals And HashCode
重写equals 后必须重写hashCode
Equals Null
避免equals()方法和 null 比较
Junit TestShould Include Assert
单元测试必须包含断言,而不是简单的打印结果后看输出。
Useless Operation On Immutable
对于不变类型的无用操作:对于不变类型对象 (String,BigDecimal 或BigInteger) 的操作不会改变对象本身,但操作结果是产生新的对象,所以操作的结果是错的
如:BigDecimal a=new BigDecimal(10);
a.add(newBigDecimal(5));
正确的写法:
BigDecimal bd=new BigDecimal(10);
bd = bd.add(new BigDecimal(5));
String Literals Equality
避免用== or != 比较 String
StringBuffer Instantiation With Char
StringBuffer sb = new StringBuffer('c');
字符 c 会转换为 int 值,作为 StringBuffer 的初始化大小参数
Servlet reflected cross site scripting vulnerability
public void doGet(HttpServletRequestrequest,HttpServletResponse response)throws ServletException,IOException{
String v = request.getParameter("v");
PrintWriter out = response.getWriter();
out.print("协议版本号不对,v="+v);
out.close();
}
这里字符串v没有作过滤,直接返回给用户,有可能操作XSS攻击
JSP reflected cross site scripting vulnerability
在代码中在JSP输出中直接写入一个HTTP参数,这会造成一个跨站点的脚本漏洞
Call to static DateFormat
private static SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd");
避免使用静态的DateFormat,DateFormat 是非线程安全的
Call to static Calendar
同上
Don’t use removeAll to clear a collection
清空集合使用clear() 代替removeAll()
Avoid printStackTrace
在代码中避免使用e.printStackTrace,使用logger代替
System.println
代码中禁止使用System.println
While For loop If Else Stmts Must use braces
While for 循环If Else 代码块必须使用大括号
Dead store to local variable
为局部变量赋值,但在其后的没有对她做任何使用。通常,这表明一个错误,因为值从未使用过。
Method uses the same code for two branches
此方法使用相同的代码,以实现两个有条件的分支。检查以确保这是不是一个编码错误