IntelliJIDEA代码检查规范QAPlug 转⾃:
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 : Set()) {
//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;
bigdecimal转换为integer
}
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 = Parameter("v");
PrintWriter out = Writer();
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
此⽅法使⽤相同的代码,以实现两个有条件的分⽀。检查以确保这是不是⼀个编码错误
checkstyle常见提⽰速查
Checkstyle常见错误和警告提⽰见下表所⽰:
错误提⽰错误说明
缺少类注释
⾏长度超过X个字符(包括空格)
⼀个⽅法内的返回数量是X(最⼤值只能为3)
最⼤的if-else嵌套层数为X(最⼤只能为3)
数组的⽅括号“[]”的位置不正确(检查数组类型的定义是String[] args,⽽不是String
args[])
本⾏包含System.out.println语句
缩进不正确,⼀般是因为没有在Eclipse中使⽤4个空格代替tab键引起。
static修饰符没有按照JLS的建议来排序(eg.写成public 应该改成public static
final)
名称不符合正则表达式'^[A-Z][A-Z0-9][_A-Z0-9+]$'(即为⼤写字母,数字、下划线等)。正则表达式)
⼀般在静态变量没有⼤写时提⽰,包名不是全部消息时提⽰,类名不是⼤写开头时提⽰,
⽅法名不是⼩写开头时提⽰
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论