0
# Warning Suppression
1
2
Control SpotBugs warning generation with flexible matching strategies for suppressing false positives while maintaining important analysis coverage.
3
4
## Capabilities
5
6
### SuppressFBWarnings Annotation
7
8
Primary annotation for suppressing FindBugs/SpotBugs warnings with detailed control over matching behavior.
9
10
```java { .api }
11
/**
12
* Used to suppress FindBugs warnings. Should be used instead of
13
* @SuppressWarnings to avoid conflicts with java.lang.SuppressWarnings.
14
*/
15
@Retention(RetentionPolicy.CLASS)
16
@interface SuppressFBWarnings {
17
/**
18
* The set of FindBugs warnings that are to be suppressed in
19
* annotated element. The value can be a bug category, kind or pattern.
20
*/
21
String[] value() default {};
22
23
/**
24
* Optional documentation of the reason why the warning is suppressed
25
*/
26
String justification() default "";
27
28
/**
29
* By default SuppressFBWarnings annotations suppress bugs by prefix,
30
* for instance @SuppressFBWarnings(value = "EI_EXPO", justification = "It's OK")
31
* will suppress bugs of type EI_EXPOSE_REP and EI_EXPOSE_REP2.
32
*
33
* You might use @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "It's OK", matchType=EXACT)
34
* to suppress EI_EXPOSE_REP, but not EI_EXPOSE_REP2.
35
*
36
* Regular expressions are also supported with matchType=REGEX.
37
*/
38
SuppressMatchType matchType() default SuppressMatchType.DEFAULT;
39
}
40
```
41
42
**Usage Examples:**
43
44
```java
45
// Suppress specific warning with justification
46
@SuppressFBWarnings(value = "EI_EXPOSE_REP",
47
justification = "Deliberate exposure for performance reasons")
48
public Date[] getImportantDates() {
49
return importantDates;
50
}
51
52
// Suppress multiple warnings
53
@SuppressFBWarnings(value = {"NP_NULL_ON_SOME_PATH", "RCN_REDUNDANT_NULLCHECK"},
54
justification = "Complex null handling verified by extensive tests")
55
public String processData(String input) {
56
// Complex processing logic
57
return result;
58
}
59
60
// Use exact matching to suppress only specific warning
61
@SuppressFBWarnings(value = "EI_EXPOSE_REP",
62
matchType = SuppressMatchType.EXACT,
63
justification = "Only suppress exact match, not EI_EXPOSE_REP2")
64
public Object[] getArray() {
65
return array;
66
}
67
68
// Use regex matching for complex patterns
69
@SuppressFBWarnings(value = ".*_UNCHECKED.*",
70
matchType = SuppressMatchType.REGEX,
71
justification = "All unchecked warnings suppressed in this legacy code")
72
public void legacyMethod() {
73
// Legacy code with unavoidable unchecked operations
74
}
75
```
76
77
### SuppressMatchType Enum
78
79
Matching strategies for @SuppressFBWarnings annotation.
80
81
```java { .api }
82
enum SuppressMatchType {
83
/**
84
* Default bug suppression using a mixed prefixed / case insensitive match depending on the criterion.
85
* Suppress bugs matching any of:
86
* - the given bug type with: String.startsWith(String)
87
* - the given bug category with: String.equalsIgnoreCase(String)
88
* - the given bug abbreviation with: String.equalsIgnoreCase(String)
89
*/
90
DEFAULT,
91
92
/**
93
* Exact (case sensitive match).
94
* Suppress bugs matching any of:
95
* - the given bug type with: String.equals(Object)
96
* - the given bug category with: String.equals(Object)
97
* - the given bug abbreviation with: String.equals(Object)
98
*/
99
EXACT,
100
101
/**
102
* Suppress bugs whose type, category or abbreviation match the given regular expression.
103
*/
104
REGEX
105
}
106
```
107
108
### Legacy SuppressWarnings (Deprecated)
109
110
Legacy annotation that conflicts with `java.lang.SuppressWarnings`. Use `@SuppressFBWarnings` instead.
111
112
```java { .api }
113
/**
114
* @deprecated Use @SuppressFBWarnings instead to avoid conflicts with java.lang.SuppressWarnings
115
*/
116
@interface SuppressWarnings {
117
String[] value() default {};
118
String justification() default "";
119
}
120
```
121
122
## Common Warning Types
123
124
Here are some common SpotBugs warning types you might need to suppress:
125
126
- `EI_EXPOSE_REP` - Exposing internal representation by returning reference to mutable object
127
- `EI_EXPOSE_REP2` - Exposing internal representation by incorporating reference to mutable object
128
- `NP_NULL_ON_SOME_PATH` - Possible null pointer dereference
129
- `RCN_REDUNDANT_NULLCHECK` - Redundant nullcheck of value known to be non-null
130
- `DM_DEFAULT_ENCODING` - Reliance on default encoding
131
- `URF_UNREAD_FIELD` - Unread field
132
- `UWF_UNWRITTEN_FIELD` - Unwritten field
133
- `SE_BAD_FIELD` - Non-transient non-serializable instance field in serializable class
134
135
## Best Practices
136
137
1. **Always provide justification**: Include a clear explanation of why the warning is being suppressed
138
2. **Use specific patterns**: Suppress only the specific warnings you need to, not broad categories
139
3. **Consider exact matching**: Use `SuppressMatchType.EXACT` when you want to suppress only a specific warning type
140
4. **Review regularly**: Periodically review suppressed warnings to see if the underlying issues can be fixed
141
5. **Document in code**: Add comments explaining the suppression when the justification alone isn't sufficient