FindBugs和静态初始化顺序 [英] FindBugs and static initialization order

查看:139
本文介绍了FindBugs和静态初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Java代码:

I have the following Java code:

public class Something {

    static {
        new Something();
    } 

    public static final int[] EMPTY_INT_ARRAY = new int[0];
}

我正在使用 FindBugs 查找代码错误,但始终不会出现以下错误:

I'm using FindBugs to look for code errors, but the following error is never raised:

SI:静态初始化程序在所有静态final字段之前创建实例 已分配(SI_INSTANCE_BEFORE_FINALS_ASSIGNED)

SI: Static initializer creates instance before all static final fields assigned (SI_INSTANCE_BEFORE_FINALS_ASSIGNED)

该类的静态初始化程序在创建该类的实例之前 所有静态的final字段均已分配.

The class's static initializer creates an instance of the class before all of the static final fields are assigned.

这是应该显示此问题的正确案例吗?为什么不选择此代码问题?

Is this the correct case that should demonstrate this issue? why is it not picking up this code issue?

推荐答案

未尝试过,但我认为示例如下:

Not tried, but I think an example would be:

public class Something {

    static {
        new Something();
    }

    public static final int CONST = 42;
}

正如麦角酸发现的那样,该错误在FindBugs中的严重性较低(16).您必须手动降低最小阈值才能显示它.

As lysergic-acid has found out, this error has a lower severity in FindBugs (16). You have to manually lower the min threshold to make it show.

这篇关于FindBugs和静态初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆