views:

57

answers:

1

Is there any way to let FindBugs check and warn me if a CheckForNull annotation is present on the implementation of a method in a class, but not on the declaration of the method in the interface?

import javax.annotation.CheckForNull;

interface Foo {
    public String getBar();
}

class FooImpl implements Foo {

    @CheckForNull 
    @Override
    public String getBar() {
        return null;
    }   
}

public class FindBugsDemo {
    public static void main(String[] args) {
        Foo foo = new FooImpl();
        System.out.println(foo.getBar().length());
    }
}

I just discovered a bug in my application due to a missing null check that was not spotted by FindBugs because CheckForNull was only present on FooImpl, but not on Foo, and I don't want to spot all other locations of this problem manually.

A: 

Yes you can write your own detectors, and package them in your own jar plugin file. See fb-contrib.sf.net as an example of an auxillary findbugs plugin.

The one issue is that FindBugs relies on bcel 5.2 which doesn't have annotation support yet. You still get the attributes passed to you that represent the annotations, but it's more manual coding than will be the case in 5.3 (or whatever the next version of bcel is).

MeBigFatGuy