-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
186ad19
commit b4a884e
Showing
6 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/ba/Issue390Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package edu.umd.cs.findbugs.ba; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.Test; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
|
||
public class Issue390Test extends AbstractIntegrationTest { | ||
|
||
@Test | ||
public void test() { | ||
performAnalysis("ghIssues/Issue390.class"); | ||
BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder().bugType("JUA_DONT_ASSERT_INSTANCEOF_IN_TESTS").build(); | ||
MatcherAssert.assertThat(getBugCollection(), containsExactly(1, bugTypeMatcher)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DontAssertInstanceofInTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.BytecodeScanningDetector; | ||
|
||
import org.apache.bcel.Const; | ||
import org.apache.bcel.classfile.AnnotationEntry; | ||
import org.apache.bcel.classfile.Code; | ||
import org.apache.bcel.classfile.Method; | ||
|
||
/** | ||
* A detector that checks for lines in JUnit tests that look like `assertTrue(object instanceof Class)` and discourages them. | ||
* | ||
* It may be more useful to observe error messages from bad casts, as this may reveal more information regarding the | ||
* circumstances of the exception than a "false is not true" assert message. | ||
* | ||
* This detector reports bugs of the type `JUA_DONT_ASSERT_INSTANCEOF_IN_TESTS`. | ||
*/ | ||
public class DontAssertInstanceofInTests extends BytecodeScanningDetector { | ||
private boolean isTest; | ||
|
||
private BugInstance currBug = null; | ||
|
||
BugReporter bugReporter; | ||
|
||
public DontAssertInstanceofInTests(BugReporter bugReporter) { | ||
this.bugReporter = bugReporter; | ||
} | ||
|
||
@Override | ||
public void visitMethod(Method obj) { | ||
isTest = false; | ||
for (AnnotationEntry entry : obj.getAnnotationEntries()) { | ||
String entryType = entry.getAnnotationType(); | ||
isTest |= "Lorg/junit/Test;".equals(entryType); | ||
} | ||
} | ||
|
||
@Override | ||
public void visitCode(Code obj) { | ||
if (isTest) | ||
super.visitCode(obj); | ||
} | ||
|
||
@Override | ||
public void sawOpcode(int seen) { | ||
|
||
switch (seen) { | ||
case Const.INSTANCEOF: { | ||
// Initialise the bug instance here. | ||
currBug = new BugInstance(this, "JUA_DONT_ASSERT_INSTANCEOF_IN_TESTS", NORMAL_PRIORITY); | ||
String operand = getDottedClassConstantOperand(); | ||
currBug.addTypeOfNamedClass(operand); // {0} | ||
|
||
break; | ||
} | ||
case Const.INVOKESTATIC: { | ||
if (getPrevOpcode(1) == Const.INSTANCEOF) { | ||
String ciOp = getClassConstantOperand(); | ||
String ncOp = getNameConstantOperand(); | ||
|
||
if ("org/junit/Assert".equals(ciOp) && | ||
"assertTrue".equals(ncOp)) { | ||
// This condition only triggers if the previous opcode was instanceof, | ||
// so currBug is guaranteed to be a new BugInstance. | ||
currBug.addClassAndMethod(this) // {2} | ||
.addSourceLine(this); // {3} | ||
bugReporter.reportBug(currBug); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ghIssues; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public class Issue390 { | ||
|
||
@Test | ||
public void test() { | ||
Integer intVal = 1; | ||
assertTrue(intVal instanceof Integer); | ||
} | ||
} |