U2647's blog 一个热爱学习的 Java 程序员,喜欢 Vue,喜欢深度学习 Dubbo Flutter SpringBoot Debug Notes Java LeetCode Python Redis Android DesignPattern mdi-home-outline 首页 mdi-cloud-outline 标签云 mdi-timeline-text-outline 时间轴 mdi-draw-pen 文章总数 62
FindBugs 常见错误类型整理 FindBugs 常见错误类型整理 FindBugs mdi-cursor-default-click-outline 点击量 62

1.Dodgy code 糟糕的代码

1.1 Misuse of static fields

滥用静态字段

  • Write to static field from instance method
由于在方法内对静态属性进行赋值操作
class A{
    private static int count;

    public void init(int num){
        count = num;
    }
}

1.2 Null pointer dereference

引用null对象

  • Load of known null value
引用了一个为 NULL 的对象

class A{
    private static int count;

    public void init(int num){
        Integer num = null;
        num.intValue();
    }
}

1.3 Dead local store

本地变量存储了闲置不用的对象

  • Dead store to local variable
对变量进行赋值,但是后面的代码没有用到这个变量
class A{
    //第一种情况
    public void init(int a){
        int num = 3;
        num = a;
    }
    //第二种情况
    public void init(int a){
        int num = 3;
        //...
    }
}

1.4 Useless code

无用的代码

  • Useless object created
创建了无用的对象

class A{
    public void init(int a){
        List<String> list = new ArrayList<>();
    }
    
}
  • Useless non-empty void method
创建了无用的方法

class A{
    public void init(){
        List<String> list = new ArrayList<>();
        list.add("123");
        list.add("234");
    }
}

1.5 RuntimeException capture

(滥用)异常捕获

  • Exception is caught when Exception is not thrown
在不会抛出异常的代码里尝试捕获异常

class A{
    public void init(){
        try{
            List<String> list = new ArrayList<>();
        }catch (Exception e) {
            
        }
    }
}

1.6 Bad use of return value from method

方法的返回值使用不当

  • Return value of method without side effect is ignored
忽略没有副作用的方法的返回值

class A{
    public void init(){
        List<String> list = new ArrayList<>();
        list.size();
    }
}

1.7 Redundant comparison to null

对 null 值的冗余校验

  • Redundant nullcheck of value known to be non-null
对已知的非空对象进行null值校验

class A{
    public void init(){

        if(getList() != null){

        }else{

        }
    }

    private List<String> getList(){
        List<String> list = new ArrayList<>();
        return list
    }
}
  • Redundant nullcheck of value known to be null
对已知的空对象进行null值校验

class A{
    public void init(){

        if(getList() == null){

        }else{

        }
    }

    private List<String> getList(){
        return null
    }
}

1.7 Suspicious integer expression

可疑的整数表达式

  • Vacuous bit mask operation on integer value
整数值上的空位掩码操作

class A{
    public void init(int num){
        int a = num & 0xFFFFFFFF;
    }
}

1.8 Duplicate Branches

重复分支

  • Method uses the same code for two branches
两个分支内的代码一样

class A{
    public void init(int num){
        if(num>0){
            num = 0;
        }else{
            num = 0;
        }
    }
}

1.9 Dubious method invocation

可疑的方法调用

  • Code contains a hard coded reference to an absolute pathname
代码包含绝对路径的硬编码引用

class A{
    public void init(int num){
        File file = new File("D:\test\test.txt");
    }
}

1.10 Test for floating point equality

比较两个浮点数是否相等

  • Test for floating point equality
比较两个浮点数是否相等

class A{
    public void init(double a,double b){
        if (a == b){

        }
    }
}

1.11 Null pointer dereference

引用空对象

  • Load of known null value
引用已知为null的对象

class A{
    public void init(List<String> list){
        if (list == null){
            System.out.print(list.toString());
        }
    }
}

1.12 Switch case falls through

switch 语句使用不当

  • Switch statement found where default case is missing
switch 语句缺少 default 分支

class A{
    public void init(int a){
        switch a {
            case 1:
                break;
            case 2: 
                break;
            }
    }
}

2.Correctness 正确性

2.1 Null pointer dereference

引用空指针

  • Possible null pointer dereference
可能会抛出空指针异常

class A{
    public void init(List<String> list){
        System.out.print(list.size());
    }
}
  • Possible null pointer dereference in method on exception path
引用了某个异常控制的对象。

class A{
    public void init(){
        List<String> list = null;
        try{

            list = new ArrayList<>();
            
        }catch (Exception e) {
            
        }

        System.out.print(list.size());
    }
}
  • Method call passes null for non-null parameter
方法调用对非null参数传递null值

class A{
    public void init(String str){
        //compareTo方法要求参数不能为null,否则会抛出NullPointerException
        "abc".compareTo(str)
    }
}
  • Null value is guaranteed to be dereferenced
保证不能引用空值

class A{
    public void init(boolean flag){
        List<String> list = null;
        if(flag){
            list.add("abc");
        }
    }
}

2.2 Useless/non-informative string generated

生成了无用/非信息的字符串

  • Invocation of toString on an array
无效的 toString 方法

class A{
    public void init(int[] arr){
        //会打印出 arr 的哈希值,而不是数组的的内容
        System.out.print(arr);
    }
}

2.3 Bad use of return value from method

方法的返回值使用不当

  • Method ignores return value
该方法的返回值应该进行检查

class A{
    public void init(int[] arr){
        String dateString = getHeaderField(name);
        dateString.trim();
    }
}

PS:这种警告通常出现在调用一个不可变对象的方法,认为它更新了对象的值。

程序员似乎以为trim()方法将更新dateString引用的字符串。

但由于字符串是不可改变的,trim()函数返回一个新字符串值,在这里它是被忽略了。

该代码应更正:

String dateString = getHeaderField(name);
dateString = dateString.trim();

2.4 Format string problem

字符串格式化问题

  • More arguments are passed than are actually used in the format string
传递的参数多于格式字符串中实际使用的参数

class A{
    public void init(String num1,String num2){
        //错误,这种写法不会格式化字符串
        String dateString = String.format("num1: {},num2:{}", num1,num2)
        String dateString2 = String.format("num1: {0},num2:{1}", num1,num2)
        //正确的写法
        String dateString3 = String.format("num1: %s,num2:%s", num1,num2)
    }
}

2.5 Comparing incompatible types for equality

比较不兼容的类型是否相等

  • Call to equals() comparing different types
调用equals()比较不同的类型

class A{
    public void init(String str){
        Integer num = new Integer()
        if(str.equals(num)){

        }
    }
}

2.6 Questionable use of reference equality rather than calling equals

对象的比较使用了 “==” 而不是调用 equals 方法

  • Suspicious reference comparison
可疑的比较

class A{
    public void init(String str){
        if("abc" == str){
        }
    }
}

2.7 Redundant comparison to null

与null值的冗余比较

  • Nullcheck of value previously dereferenced
没有对参数进行空值校验

class A{
    public void init(String str){
        System.out.println(getStr().size())
    }
    private List<String> getList(){
        return null;
    }
}

3.Bad practice 不好的做法

3.1 Stream not closed on all paths

IO流没有关闭

  • Method may fail to close stream
方法体内没有对IO流进行关闭

3.2 Dropped or ignored exception

忽略了异常

  • Method might ignore exception
方法忽略了异常

class A{
    public void init(){
        try{
            
        }catch (Exception e) {
            //当抛出异常时什么都没有做
        }
    }
}

3.3 Bad use of return value from method

方法返回值使用不当(多为忽略了方法返回值)

  • Method ignores exceptional return value
忽略了方法的异常返回值

class A{
    public void init(){
        File file = new File("/aaa")
        //文件mkdir可能返回false
        file.getParentFile().mkdirs()
    }
}

3.4 Incorrect definition of Serializable class

序列化的类定义不正确

  • Non-transient non-serializable instance field in serializable class
可序列化的类中存在不可序列化的属性

class A implements Serializable{
    //List对象不能直接序列化
    private List<String> list;
}

3.5 Checking String equality using == or !=

Stirng 对象的比较使用了 == 或 !=

  • Comparison of String parameter using == or !=
Stirng 对象的比较使用了 == 或 !=

String对象的比较应该使用 equals() 方法

3.6 Confusing method name

不规范的方法名

  • Method names should start with a lower case letter
方法名应该以小写字母开头,符合驼峰式命名格式

4 Malicious code vulnerability 恶意代码漏洞

4.1 Method returning array may expose internal representation

返回数组的方法可以暴露内部表示

  • May expose internal representation by returning reference to mutable object
可以通过返回的可变对象的引用来公开内部表示

class A{
    private List<String> list = new ArrayList<>();
    public List<String> getList(){
        return list;
    }
}
  • May expose internal representation by incorporating reference to mutable object
可以通过引用可变对象来公开内部表示

class A{
    private List<String> list;
    public void setList(List<String> list){
        this.list = list;
    }
}
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
我的GitHub 我的LeetCode 我的掘金
Powered by Hexo Powered by three-cards
Copyright © 2017 - {{ new Date().getFullYear() }} 某ICP备xxxxxxxx号