-
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
Add detector for catching NullPointerExceptions #1740
Add detector for catching NullPointerExceptions #1740
Conversation
147384a
to
477ed97
Compare
Hi, |
spotbugs/etc/messages.xml
Outdated
<Detector class="edu.umd.cs.findbugs.detect.DontCatchNullPointerException"> | ||
<Details> | ||
<![CDATA[ | ||
<p> Nullponter exceptions or its ancestors should not be caught.</p> |
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.
Misspelled "pointer"
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.
<p> Nullponter exceptions or its ancestors should not be caught.</p> | |
<p>Nullpointer exceptions or its ancestors should not be caught.</p> |
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.
Current implementation checks NullPointerException
only, so it's better to remove or its ancestors
.
Note that NullPointerException
has three throwable ancestors:
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
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.
Good catch, fixed.
spotbugs/etc/messages.xml
Outdated
<Detector class="edu.umd.cs.findbugs.detect.DontCatchNullPointerException"> | ||
<Details> | ||
<![CDATA[ | ||
<p> Nullponter exceptions or its ancestors should not be caught.</p> |
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.
<p> Nullponter exceptions or its ancestors should not be caught.</p> | |
<p>Nullpointer exceptions or its ancestors should not be caught.</p> |
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DontCatchNullPointerException.java
Outdated
Show resolved
Hide resolved
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DontCatchNullPointerException.java
Outdated
Show resolved
Hide resolved
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DontCatchNullPointerException.java
Outdated
Show resolved
Hide resolved
import edu.umd.cs.findbugs.visitclass.PreorderVisitor; | ||
|
||
public class DontCatchNullPointerException | ||
extends PreorderVisitor implements Detector { |
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.
PreorderVisitor
has already implemented Detector
so no need to implement it again.
extends PreorderVisitor implements Detector { | |
extends PreorderVisitor { |
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.
👍
spotbugs/etc/messages.xml
Outdated
<Detector class="edu.umd.cs.findbugs.detect.DontCatchNullPointerException"> | ||
<Details> | ||
<![CDATA[ | ||
<p> Nullponter exceptions or its ancestors should not be caught.</p> |
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.
Current implementation checks NullPointerException
only, so it's better to remove or its ancestors
.
Note that NullPointerException
has three throwable ancestors:
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
Thanks for the quick response! I have applied the suggestions as best I could. I will ask it here as well, do you prefer the approach where I add every amendment in separate commits (and squash at the end), or should I just rebase every time when it is necessary? |
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.
Mostly LGTM, but please fix a minor problem in the CHANGELOG.
.constantToString(getConstantPool() | ||
.getConstant(type)); | ||
|
||
if (name.equals(NULLPOINTER_EXCEPTION_FQCN)) { |
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.
memo: Confirmed that name
is not null-able. So it's safe to invoke instance method.
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.
Thanks a lot! I have also just quickly checked the source ( https://commons.apache.org/proper/commons-bcel/apidocs/src-html/org/apache/bcel/classfile/ConstantPool.html#line.96 ), and every path seems fine.
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.
LGTM, thanks for your contribution!
I will keep this PR open for days to ask other teammates to review.
Make sure these boxes are checked before submitting your PR -- thank you!
CHANGELOG.md
if you have changed SpotBugs code