31

I would like to suppress FindBugs warnings for specific fields or local variables. FindBugs documents that the Target can be Type, Field, Method, Parameter, Constructor, Package for its edu.umd.cs.findbugs.annotations.SuppressWarning annotation [1]. But it does not work for me to annotate the field, only when I annotate the method the warning gets suppressed.

Annotating a whole method seems to broad to me. Is there any way to suppress warnings on specific fields? There is another related question [2], but no answer.

[1] http://findbugs.sourceforge.net/manual/annotations.html

[2] Suppress FindBugs warnings in Eclipse

Demo code:

public class SyncOnBoxed
{
    static int counter = 0;
    // The following SuppressWarnings does NOT prevent the FindBugs warning
    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE")
    final static Long expiringLock = new Long(System.currentTimeMillis() + 10);
    
    public static void main(String[] args) {
        while (increment(expiringLock)) {
            System.out.println(counter);
        }
    }
    
    // The following SuppressWarnings prevents the FindBugs warning
    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE")
    protected static boolean increment(Long expiringLock)
    {
        synchronized (expiringLock) { // <<< FindBugs warning is here: Synchronization on Long in SyncOnBoxed.increment()
            counter++;
        }
        return expiringLock > System.currentTimeMillis(); // return false when lock is expired
    }
}
Lii
  • 11,553
  • 8
  • 64
  • 88
Christian Esken
  • 472
  • 1
  • 4
  • 11
  • Maybe a duplicate of: [Is there a way to ignore a single FindBugs warning?](http://stackoverflow.com/questions/1829904/is-there-a-way-to-ignore-a-single-findbugs-warning) – MrSmith42 Jan 24 '13 at 13:57
  • 1
    Not really. The examples also work on method level only. Probably FindBugs only works like that. But why does the SuppressWarning annotation documentation then say its Target can also be Field. – Christian Esken Jan 24 '13 at 17:23
  • Even worse, when suppressing warnings in Scala (at least for Find-Sec-Bugs, which is a security plugin for FindBugs), sometimes the annotation `@SuppressFBWarnings` only works *at the class level*, which is definitely unacceptable. – Andres F. Jul 26 '16 at 15:06
  • Warning! Code that synchronises on primitive wrappers will likely break in a future Java release, throwing `IllegalMonitorStateException`! As part of project Valhalla, which will add custom value types to Java, the primitive wrapper classes will be changed to become value types and it will not be possible to syncronise on them anymore. – Lii Nov 13 '20 at 10:32

2 Answers2

35

@SuppressFBWarnings on a field only suppresses findbugs warnings reported for that field declaration, not every warning associated with that field.

For example, this suppresses the "Field only ever set to null" warning:

@SuppressFBWarnings("UWF_NULL_FIELD")
String s = null;

I think the best you can do is isolate the code with the warning into the smallest method you can, then suppress the warning on the whole method.

Note: @SuppressWarnings was marked deprecated in favor of @SuppressFBWarnings

Oliv
  • 10,221
  • 3
  • 55
  • 76
TimK
  • 4,635
  • 2
  • 27
  • 27
  • The explanation of "suppresses [...] not every warning associated with that field" seems to be correct. I will mark this answer as correct. A warning to other readers though: I was not able to suppress "UWF_NULL_FIELD" on the field as TimK explained. So probably one must **always** annotate on method level. – Christian Esken Jan 25 '13 at 10:30
  • 4
    And don't forget that @SuppressWarnings is a findbugs annotation from: com.google.code.findbugs annotations 2.0.2 – user1050755 Feb 28 '14 at 12:29
  • 6
    Note that `@SuppressWarnings` has been [deprecated](http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/SuppressWarnings.html) and replaced with `@SuppressFBWarnings`. – Ben Oct 31 '14 at 14:08
  • Ah, deprecated for the version. This annotation in the java.lang package is still good (java 8). – Prancer Jan 30 '15 at 12:42
  • @Ben It appears to work for me in eclipse with `java.lang.SuppressWarnings`. I haven't been able to find a verified source to explain why. – 00500005 Feb 04 '16 at 14:47
  • 3
    `java.lang.SuppressWarnings` can't work. It has source retention, so is not visible to findbugs. – Philip Aston Mar 22 '16 at 09:31
  • building tools should never intervene business code. so adding annotations is a bad bad bad idea. – Tiina Dec 05 '17 at 08:45
3

Check http://findbugs.sourceforge.net/manual/filter.html#d0e2318 There is a Local tag that can be used with the Method tag. Here you can specify which bug should be excluded for a specific local variable. Example:

<FindBugsFilter>
  <Match>
        <Class name="<fully-qualified-class-name>" />
        <Method name="<method-name>" />
        <Local name="<local-variable-name-in-above-method>" />
        <Bug pattern="DLS_DEAD_LOCAL_STORE" />
  </Match>
</FindBugsFilter>
Jyothi
  • 97
  • 5