findbugs检测提示详解
1、Comparison of String objects using == or !=
例,override equals方法时容易犯错 if(this.topic != key.getTopic()) return false;
2、Dead store to newStatusRecord
定义局部变量后没有引用
3、Invocation of toString on values
直接调用数组的toString方法
public Query createQuery(String hql, Object values[],Session session){
logger.debug(values); logger.debug((new
StringBuilder()).append(\\}
正确的例子,调用Arrays.toString()和Arrays.deepToString()方法。 import java.util.Arrays; class A{ }
class B{ @Override
public String toString() { return \ } }
public class Test {
public static void main(String[] args) {
Object [] a = {new Integer(0),new Boolean(true),true,new A(),new B()};
Object[][]b ={{new A(),new B()},{new A(),new B()},{new A(),new B()}}; System.out.println(Arrays.deepToString(b)); } }
4、ignores exceptional return value of
java.io.File.mkdirs()
忽略了返回值,应当含有返回值
public void initFolder() { if (!exitDir.isDirectory()) { exitDir.mkdirs();
logger.info(\ } }
This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.
5、不使用new String()定义空的字符串
String alarmCodeCond = new String(); 应当
String alarmCodeCond = \
6、invokes inefficient new Short(short) constructor; use Short.valueOf(short) instead JVM缓存数字常量
Short aShort = new Short(12); 应当
Short aShort = Short.valueOf(12);
7、方法命名习惯,首字母小写
The method name LaneHandShakeService(Short) doesn't start with a lower case letter
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
8、一个primtive的类型的值经过box后马上unbox
Primitive value is boxed then unboxed to perform primitive coercion
exitRecord.setEnOperatorId(new
Long(transactRecord.getEnoperatorID()).intValue()); 应当直接强制类型转换
exitRecord.setEnOperatorId((int)transactRecord.getEnoperatorID());
9、Call to equals() comparing different types 使用equals()方法比较不同的类,
反例
StringBuilder builder = new StringBuilder(\ String string = \ builder.equals(string);
10、Check for oddness that won't work for negative numbers
检查奇数的方法:
反例
if (i % 2 == 1) { //... }
The code uses x % 2 == 1 to check to see if a value is odd, but this won't work for negative numbers (e.g., (-5) % 2 == -1). If this code is intending to check for oddness, consider using x & 1 == 1, or x % 2 != 0.
11、Load of known null value,null值的不当使用
反例:
if (devIds == null && devIds.size() == 0) { //... }
if (null != tempList || tempList.size() != 0) { //... }
if (batchNo == null) {
throw new Exception(\ + \ }
12、Method call passes null for nonnull parameter
对参数为null的情况没做处理
例
public void method1() { String ip = null; try {
ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); }
long ipCount = countIpAddress(ip); // 可能会传入空引用 }
long countIpAddress(String ip) { long ipNum = 0;
String[] ipArray = ip.split(\} 修改后:
public void method1() { String ip = null; try {
ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); }
long ipCount = countIpAddress(ip); // 可能会传入空引用 }
long countIpAddress(String ip) { long ipNum = 0; if (ip == null) {
return 0; //或者抛出异常 }
String[] ipArray = ip.split(\
}
注意:函数入口需要交验入参的合法性。
//...
//... //...
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库findbugs检测提示详解在线全文阅读。
相关推荐: