findbugs规则整理及解决办法

本文详细分析了FindBugs工具报告的常见问题,包括自动装箱拆箱、无效的哈希码处理、非静态成员类的优化、空指针风险等,并提供了相应的优化建议,以提升代码质量和效率。
摘要由CSDN通过智能技术生成
  1. A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method.
    修改建议:使用Long.ParseLong,避免自动装箱再拆箱
    问题原因:
    Long.ParseLong(String)方法,将 string 参数解析为有符号十进制 ,返回一个long的基本类型值
    Long.ValueOf(String) ,方法得到的值非常相似。只是最后被转换为一个Long的包装类
  2. This class is an inner class, but does not use its embedded reference to the object which created it. This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary. If possible, the class should be made static.
    修改建议:若成员类中未访问外围类的非静态成员,为避免额外的空间和时间开销,建议改用静态成员类。
    问题原因:
    非静态成员类和静态成员类的区别在于,非静态成员类是对象的,静态成员类是类的。非静态成员类可以访问外围类的任何成员,但前提是必须存在外围类对象。JAVA需要额外维护非静态成员类和外围类对象的关系。
  3. This code generates a hashcode and then computes the absolute value of that hashcode. If the hashcode is Integer.MIN_VALUE, then the result will be negative as well (since Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE).
    One out of 2^32 strings have a hashCode of Integer.MIN_VALUE, including “polygenelubricants” “GydZG_” and “”DESIGNING WORKHOUSES”.
    修改建议:在使用之前判断一下是否是为Integer.MIN_VALUE
    问题原因:
    此代码产生哈希码,然后计算该哈希码的绝对值。如果哈希码是Integer.MIN_VALUE的,那么结果将是负的(因为Math.abs(Integer.MIN_VALUE)==Integer.MIN_VALUE)。
  4. A boxed value is unboxed and then immediately reboxed.
    修改建议:三元运算符两个分支的返回类型保持一致。
    问题原因:
    装箱的值被拆箱,然后立刻重新装箱
  5. This class defines a field with the same name as a visible instanc

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值