-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement #390 #1256
Implement #390 #1256
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format your code by running ./gradlew spotlessApply
. I will check more with my IDE later.
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DontAssertInstanceofInTests.java
Show resolved
Hide resolved
@Override | ||
public void visitCode(Code obj) { | ||
if (isTest) | ||
super.visitCode(obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the purpose is skipping sawOpcode(int)
, it could be better to implement beforeOpcode(int)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does beforeOpcode
control if we want to skip processing an entire method? The intention was to avoid processing any methods that weren't annotated as tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. The javadoc describes its responsibility as return false if we should skip calling sawOpcode
, and the implementation is like below:
spotbugs/spotbugs/src/main/java/edu/umd/cs/findbugs/visitclass/DismantleBytecode.java
Lines 877 to 879 in c195f7e
if (beforeOpcode(opcode)) { | |
sawOpcode(opcode); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding, beforeOpcode would run on every opcode of a method.
Now that I look, there's a shouldVisitCode
method in BytecodeScanningDetector
which would do exactly what I was trying to do, so I'll just override that instead of that visitCode
hack.
It appears that OpcodeStackDetector.visitCode
uses BytecodeScanningDetector.shouldVisitCode
:
spotbugs/spotbugs/src/main/java/edu/umd/cs/findbugs/bcel/OpcodeStackDetector.java
Lines 60 to 62 in c195f7e
if (!shouldVisitCode(obj)) { | |
return; | |
} |
so I changed my detector to be an OpcodeStackDetector
instead.
Following the advice of the documentation and using the instanceOf matcher, then proceeding the test by casting the object to that type in order to inspect it will then instead trigger a |
I've implemented #390 as a detector,
DontAssertInstanceofInTests
.This only covers usages of
Assert.assertTrue(obj instanceof Class)
which are present in the body of an@Test
annotated method.I would like to add usages such as
assertEquals(true, <instanceof expression>)
but I am not completely sure of the best way to go about it.CHANGELOG.md