Skip to content

Commit

Permalink
Use OpcodeStackDetector to take advantage of shouldVisitCode.
Browse files Browse the repository at this point in the history
  • Loading branch information
WarpspeedSCP authored and KengoTODA committed Aug 17, 2020
1 parent 3d5d1b1 commit 5328d7a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Expand Up @@ -2,8 +2,8 @@

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;

import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
import org.apache.bcel.Const;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.Code;
Expand All @@ -17,7 +17,7 @@
*
* This detector reports bugs of the type `JUA_DONT_ASSERT_INSTANCEOF_IN_TESTS`.
*/
public class DontAssertInstanceofInTests extends BytecodeScanningDetector {
public class DontAssertInstanceofInTests extends OpcodeStackDetector {
private boolean isTest;

private BugInstance currBug = null;
Expand All @@ -33,14 +33,14 @@ public void visitMethod(Method obj) {
isTest = false;
for (AnnotationEntry entry : obj.getAnnotationEntries()) {
String entryType = entry.getAnnotationType();
// Visit the method only if it's a JUnit test.
isTest |= "Lorg/junit/Test;".equals(entryType);
}
}

@Override
public void visitCode(Code obj) {
if (isTest)
super.visitCode(obj);
public boolean shouldVisitCode(Code obj) {
return isTest;
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions spotbugsTestCases/src/java/ghIssues/Issue390.java
Expand Up @@ -6,6 +6,12 @@

public class Issue390 {

// Should not trigger the lint.
public void not_test() {
Integer intVal = 1;
assertTrue(intVal instanceof Integer);
}

@Test
public void test() {
Integer intVal = 1;
Expand Down

0 comments on commit 5328d7a

Please sign in to comment.