Skip to content

Commit 9e20c82

Browse files
committed
BugInstanceMatcher should support local variable names
Also cleaned code a bit to reduce warnings.
1 parent a1bdd2d commit 9e20c82

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

test-harness/src/main/java/edu/umd/cs/findbugs/test/matcher/BugInstanceMatcher.java

+40-15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import edu.umd.cs.findbugs.BugInstance;
2929
import edu.umd.cs.findbugs.ClassAnnotation;
3030
import edu.umd.cs.findbugs.FieldAnnotation;
31+
import edu.umd.cs.findbugs.LocalVariableAnnotation;
3132
import edu.umd.cs.findbugs.MethodAnnotation;
3233
import edu.umd.cs.findbugs.SourceLineAnnotation;
3334
import edu.umd.cs.findbugs.annotations.Confidence;
@@ -40,6 +41,7 @@ public class BugInstanceMatcher extends BaseMatcher<BugInstance> {
4041
private final String className;
4142
private final String methodName;
4243
private final String fieldName;
44+
private final String variableName;
4345
private final Integer lineNumber;
4446
private final Integer lineNumberApprox;
4547
private final Confidence confidence;
@@ -49,28 +51,43 @@ public class BugInstanceMatcher extends BaseMatcher<BugInstance> {
4951
/**
5052
* All the parameters are optional. Only the non-null parameters are used.
5153
*
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)
6174
*/
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) {
6378
this.bugType = bugType;
6479
this.className = className;
6580
this.methodName = methodName;
6681
this.fieldName = fieldName;
82+
this.variableName = variableName;
6783
this.lineNumber = lineNumber;
6884
this.lineNumberApprox = lineNumberApprox;
6985
this.confidence = confidence;
7086
this.jspFile = jspFile;
7187
this.multipleChoicesLine = multipleChoicesLine;
7288
}
7389

90+
@SuppressWarnings("boxing")
7491
@Override
7592
public boolean matches(Object obj) {
7693
if (obj instanceof BugInstance) {
@@ -121,6 +138,13 @@ public boolean matches(Object obj) {
121138
}
122139
criteriaMatches &= fieldAnn.getFieldName().equals(fieldName);
123140
}
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+
}
124148
if (lineNumber != null) {
125149
SourceLineAnnotation srcAnn = extractBugAnnotation(bugInstance, SourceLineAnnotation.class);
126150
if (srcAnn == null) {
@@ -158,16 +182,14 @@ public boolean matches(Object obj) {
158182
criteriaMatches &= found;
159183
}
160184
return criteriaMatches;
161-
} else {
162-
return false;
163185
}
186+
return false;
164187
}
165188

166-
@SuppressWarnings("unchecked")
167-
private <T> T extractBugAnnotation(BugInstance bugInstance, Class<T> annotationType) {
189+
private static <T> T extractBugAnnotation(BugInstance bugInstance, Class<T> annotationType) {
168190
for (BugAnnotation annotation : bugInstance.getAnnotations()) {
169191
if (annotation.getClass().equals(annotationType)) {
170-
return (T) annotation;
192+
return annotationType.cast(annotation);
171193
}
172194
}
173195
return null;
@@ -188,6 +210,9 @@ public void describeTo(Description description) {
188210
if (fieldName != null) {
189211
description.appendText("fieldName=").appendValue(fieldName).appendText(",");
190212
}
213+
if (variableName != null) {
214+
description.appendText("variableName=").appendValue(variableName).appendText(",");
215+
}
191216
if (lineNumber != null) {
192217
description.appendText("lineNumber=").appendValue(lineNumber);
193218
}

test-harness/src/main/java/edu/umd/cs/findbugs/test/matcher/BugInstanceMatcherBuilder.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class BugInstanceMatcherBuilder {
3737
private String className;
3838
private String methodName;
3939
private String fieldName;
40+
private String variableName;
4041
private Integer lineNumber;
4142
private Integer lineNumberApprox;
4243
private Confidence confidence;
@@ -63,6 +64,11 @@ public BugInstanceMatcherBuilder atField(String fieldName) {
6364
return this;
6465
}
6566

67+
public BugInstanceMatcherBuilder atVariable(String variableName) {
68+
this.variableName = variableName;
69+
return this;
70+
}
71+
6672
public BugInstanceMatcherBuilder atLine(int lineNumber) {
6773
this.lineNumber = lineNumber;
6874
return this;
@@ -117,7 +123,7 @@ public BugInstanceMatcher build() {
117123
}
118124
}
119125

120-
return new BugInstanceMatcher(bugType, className, methodName, fieldName, lineNumber,
126+
return new BugInstanceMatcher(bugType, className, methodName, fieldName, variableName, lineNumber,
121127
lineNumberApprox, confidence, jspFile, multipleChoicesLine);
122128
}
123129

0 commit comments

Comments
 (0)