简体   繁体   English

FIndbug无法识别空指针异常

[英]FIndbug not identifying null pointer exception

I am using Findbugs integerated with eclipse. 我正在使用用Eclipse整数化的Findbugs。

When I run findbugs on my project the below code is not captured for possible null pointer exception. 当我在项目上运行findbugs时,可能的空指针异常未捕获以下代码。

In the below snippet the object test is prone to null pointer exception which is not identified by findbugs. 在下面的代码段中,对象测试易于出现空指针异常,该异常未被findbug标识。

@Override
    public boolean saveIrr(TestObject test) throws DuplicateRecordException {
        boolean status = false
        try {
            test.getAddDate();
            status = adhocMaintPopupMapper.saveIrr(IRRPopupMaint);
        } catch (DataIntegrityViolationException e) {
            logger.error("Error in Saving!", e);
            throw new TransactionDataException("Error in Saving!", e);
        }
        return status;
    }

Is there any configuration change required to make findbugs to identify this? 是否需要进行任何配置更改才能使findbug识别出这一点?

If you add @Nonnull to the parameter declaration, FindBugs will highlight anywhere you're passing a value that isn't checked for null . 如果将@Nonnull添加到参数声明中,FindBugs将在您传递未检查null的值的任何地方突出显示。 If you mark it @CheckForNull , FindBugs will highlight anywhere in the method that you access it without checking for null . 如果将其标记为@CheckForNull ,FindBugs将在您访问该方法的任何位置突出显示它,而不检查null

Which you do depends on the method's contract: does it tolerate null or not? 您做什么取决于方法的约定:它可以容忍null吗? Looking at its implementation it does not allow for null without throwing an unexpected exception. 看一下它的实现,不允许null而不会引发意外的异常。 Therefore, test should be marked @Nonnull so you can spot incorrect calls. 因此, test应标记为@Nonnull以便您可以发现错误的呼叫。

Update 更新

FindBugs will only check fields, parameters, method return values that are annotated with either @Nonnull or @CheckForNull . FindBugs将检查用@Nonnull@CheckForNull注释的字段,参数,方法返回值。 Anything without an annotation is assumed @Nullable which tells FindBugs to ignore it. 任何没有注释的东西都被假定为@Nullable ,它告诉FindBugs忽略它。

public boolean saveIrr(@Nonnull TestObject test) { ... }

public void dontCareAboutNull(TestObject value) {
    saveIrr(value); // no bug
}

public void mightBeNull(@CheckForNull TestObject value) {
    saveIrr(value); // bug
}

For this reason, we apply @Nonnull to all three types of values at the package level. 因此,我们在包级别将@Nonnull应用于所有三种类型的值。 Any value that needs to allow null must be annotated with @CheckForNull . 任何需要允许为null必须使用@CheckForNull进行注释。 We do not allow the use of @Nullable except in very few corner cases (eg @Autowired fields which Spring enforces). 除非在极少数情况下(例如,Spring强制使用@Autowired字段),否则我们不允许使用@Nullable

I noticed that you're missing a ; 我注意到您想念一个; in your code after "boolean status = false", this could be the reason why findbug has problems to parse your code. 在您的代码中,“布尔状态=假”之后,这可能是findbug无法解析您的代码的原因。

OK from what I understood : you want to identify that test has not beeing tested for null. 从我的理解中可以,您可以确定:您想确定该测试尚未通过null测试。 As far as I know there is no way to configure findbugs for doing this. 据我所知,没有办法配置findbugs来做到这一点。 Findbugs can warn you in 2 other cases : - NP_ARGUMENT_MIGHT_BE_NULL : if you call your method saveIrr with a and argument that has not been tested for null before. 在2种其他情况下,Findbug可能会警告您:-NP_ARGUMENT_MIGHT_BE_NULL:如果您使用和调用您的方法saveIrr saveIrr且之前未测试过null的参数。 - NP_NULL_INSTANCEOF : if findbug identified that your value is guaranteed to be null at a point. -NP_NULL_INSTANCEOF:如果findbug确定某个点的值一定为null。

You can check all the Null Pointer warnings here they are identified with NP: http://findbugs.sourceforge.net/bugDescriptions.html 您可以在此处检查所有用NP标识的空指针警告: http : //findbugs.sourceforge.net/bugDescriptions.html

I think that such a warning would result in a too huge amount of bugs detected : all methods with arguments would give warnings for arguments that would be used before beeing tested. 我认为,这样的警告会导致检测到太多的错误:所有带有参数的方法都会对在经过测试之前使用的参数给出警告。

What you can do is use the annotions of findbugs/jsr305. 你可以做的是使用annotions FindBugs的/ JSR305的。 So if you add @Nullable to the getDate() method in TestObject it may trigger a NP warning. 因此,如果将@Nullable添加到TestObject中的getDate()方法,则可能会触发NP警告。 If you want to use those annotions be sure that the jsr305.jar is in your classpath... 如果要使用这些注释,请确保jsr305.jar位于类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM