Findbugs使用

本文介绍了FindBugs插件在Android Studio中的安装与使用方法,并详细解析了通过FindBugs检测到的常见代码问题及其解决方案,包括Switch语句缺少default分支、整数除法转换错误等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:
AndroidStudio 插件 之 Findbugs 安装与简单使用教程
FindBugs规则整理
详解FindBugs的各项检测器

下载插件

这里写图片描述

下载插件并重启android studio。

分析

类或者包右键

这里写图片描述

选择一个种类分析。

分析结果 :

这里写图片描述

dodgy code 糟糕的代码 [ˈdɒdʒi]
bad practice 坏习惯
internationalization 国际  ['ɪntə,næʃənəlaɪ'zeɪʃən]
performance 表现 [pəˈfɔ:məns]
correctness 正确性  [kə'rɛktnɪs]
experimental 实验性  [ɛk'spɛrɪ'mɛntəl]
malicious code vulnerability 恶意代码漏洞  [mə'lɪʃəs]   [,vʌlnərə'bɪlətɪ]
multithreaded correctness 多线程正确性

问题一:

Switch statement found where default case is missing
Switch没有默认情况下执行的case语句。添加default即可。

switch末尾添加:

default:
     break;

问题二:

casting from integer values:
Integral division result cast to double or float
整形数除法强制转换为double或者float类型。

int x = 2;
int y = 5;
// Wrong: yields result 0.0
double value1 = x / y;
// Right: yields result 0.4
double value2 = x / (double) y;

问题三:

misuse of static fields:滥用静态变量
Write to static field UserCenterActivity.sHead from instance method onActivityResult(int, int, Intent)

    private static Bitmap sHead;

去掉static即可。

问题四:

Boxing/unboxing to parse a primitive toApplyReg()

//valueOf 返回 Integer
int code = Integer.valueOf(applyRegModel.code);//wrong
//parseInt 返回 int
int code = Integer.parseInt(applyRegModel.code);//right

问题五:

The code contains a conditional test is performed twice, one right after the other (e.g., x == 0 || x == 0).
Perhaps the second occurrence is intended to be something else (e.g., x == 0 || y == 0).

    if (password_one.length() > 0 && password_two.length() > 0 && password_two.length() > 0) {
            mButtonOK.setBackground(getResources().getDrawable(R.drawable.bg_btn_enabled));
            mButtonOK.setEnabled(true);
    } else {
        mButtonOK.setBackground(getResources().getDrawable(R.drawable.bg_btn_disabled));
        mButtonOK.setEnabled(false);
    }

password_two.length判断重复,去掉一个即可。

问题六:

Method concatenates strings using + in a loop
在循环中构建一个String对象时从性能上讲使用StringBuffer来代替String对象

  // This is bad
  String s = "";
  for (int i = 0; i < field.length; ++i) {
    s = s + field[i];
  }

  // This is better
  StringBuffer buf = new StringBuffer();
  for (int i = 0; i < field.length; ++i) {
    buf.append(field[i]);
  }
  String s = buf.toString();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值