28
28
import edu .umd .cs .findbugs .BugInstance ;
29
29
import edu .umd .cs .findbugs .ClassAnnotation ;
30
30
import edu .umd .cs .findbugs .FieldAnnotation ;
31
+ import edu .umd .cs .findbugs .LocalVariableAnnotation ;
31
32
import edu .umd .cs .findbugs .MethodAnnotation ;
32
33
import edu .umd .cs .findbugs .SourceLineAnnotation ;
33
34
import edu .umd .cs .findbugs .annotations .Confidence ;
@@ -40,6 +41,7 @@ public class BugInstanceMatcher extends BaseMatcher<BugInstance> {
40
41
private final String className ;
41
42
private final String methodName ;
42
43
private final String fieldName ;
44
+ private final String variableName ;
43
45
private final Integer lineNumber ;
44
46
private final Integer lineNumberApprox ;
45
47
private final Confidence confidence ;
@@ -49,28 +51,43 @@ public class BugInstanceMatcher extends BaseMatcher<BugInstance> {
49
51
/**
50
52
* All the parameters are optional. Only the non-null parameters are used.
51
53
*
52
- * @param bugType Expected bug type
53
- * @param className Class name
54
- * @param methodName Method name
55
- * @param fieldName Field name
56
- * @param lineNumber Line number
57
- * @param lineNumberApprox Approximate line for test samples that are unstable (Historically the JSP samples)
58
- * @param confidence Confidence
59
- * @param jspFile JSP file name
60
- * @param multipleChoicesLine At least of the line (JSP samples specific)
54
+ * @param bugType
55
+ * Expected bug type
56
+ * @param className
57
+ * Class name
58
+ * @param methodName
59
+ * Method name
60
+ * @param fieldName
61
+ * Field name
62
+ * @param variableName
63
+ * Variable name
64
+ * @param lineNumber
65
+ * Line number
66
+ * @param lineNumberApprox
67
+ * Approximate line for test samples that are unstable (Historically the JSP samples)
68
+ * @param confidence
69
+ * Confidence
70
+ * @param jspFile
71
+ * JSP file name
72
+ * @param multipleChoicesLine
73
+ * At least of the line (JSP samples specific)
61
74
*/
62
- public BugInstanceMatcher (String bugType , String className , String methodName , String fieldName , Integer lineNumber , Integer lineNumberApprox , Confidence confidence , String jspFile , List <Integer > multipleChoicesLine ) {
75
+ public BugInstanceMatcher (String bugType , String className , String methodName , String fieldName ,
76
+ String variableName , Integer lineNumber , Integer lineNumberApprox , Confidence confidence , String jspFile ,
77
+ List <Integer > multipleChoicesLine ) {
63
78
this .bugType = bugType ;
64
79
this .className = className ;
65
80
this .methodName = methodName ;
66
81
this .fieldName = fieldName ;
82
+ this .variableName = variableName ;
67
83
this .lineNumber = lineNumber ;
68
84
this .lineNumberApprox = lineNumberApprox ;
69
85
this .confidence = confidence ;
70
86
this .jspFile = jspFile ;
71
87
this .multipleChoicesLine = multipleChoicesLine ;
72
88
}
73
89
90
+ @ SuppressWarnings ("boxing" )
74
91
@ Override
75
92
public boolean matches (Object obj ) {
76
93
if (obj instanceof BugInstance ) {
@@ -121,6 +138,13 @@ public boolean matches(Object obj) {
121
138
}
122
139
criteriaMatches &= fieldAnn .getFieldName ().equals (fieldName );
123
140
}
141
+ if (variableName != null ) {
142
+ LocalVariableAnnotation localVarAnn = extractBugAnnotation (bugInstance , LocalVariableAnnotation .class );
143
+ if (localVarAnn == null ) {
144
+ return false ;
145
+ }
146
+ criteriaMatches &= localVarAnn .getName ().equals (variableName );
147
+ }
124
148
if (lineNumber != null ) {
125
149
SourceLineAnnotation srcAnn = extractBugAnnotation (bugInstance , SourceLineAnnotation .class );
126
150
if (srcAnn == null ) {
@@ -158,16 +182,14 @@ public boolean matches(Object obj) {
158
182
criteriaMatches &= found ;
159
183
}
160
184
return criteriaMatches ;
161
- } else {
162
- return false ;
163
185
}
186
+ return false ;
164
187
}
165
188
166
- @ SuppressWarnings ("unchecked" )
167
- private <T > T extractBugAnnotation (BugInstance bugInstance , Class <T > annotationType ) {
189
+ private static <T > T extractBugAnnotation (BugInstance bugInstance , Class <T > annotationType ) {
168
190
for (BugAnnotation annotation : bugInstance .getAnnotations ()) {
169
191
if (annotation .getClass ().equals (annotationType )) {
170
- return ( T ) annotation ;
192
+ return annotationType . cast ( annotation ) ;
171
193
}
172
194
}
173
195
return null ;
@@ -188,6 +210,9 @@ public void describeTo(Description description) {
188
210
if (fieldName != null ) {
189
211
description .appendText ("fieldName=" ).appendValue (fieldName ).appendText ("," );
190
212
}
213
+ if (variableName != null ) {
214
+ description .appendText ("variableName=" ).appendValue (variableName ).appendText ("," );
215
+ }
191
216
if (lineNumber != null ) {
192
217
description .appendText ("lineNumber=" ).appendValue (lineNumber );
193
218
}
0 commit comments