Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package checks;

class NPEThrowCheckSample {
void foo() throws NullPointerException { // Noncompliant {{Throw some other exception here, such as "IllegalArgumentException".}}
void foo() throws NullPointerException { // Noncompliant {{Remove NullPointerException from the throws clause.}}
// ^^^^^^^^^^^^^^^^^^^^
}
void bar() {
throw new
NullPointerException // Noncompliant {{Throw some other exception here, such as "IllegalArgumentException".}}
NullPointerException // Noncompliant {{Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.}}
();
}
void baz() {
throw new java.lang.NullPointerException(); // Noncompliant {{Throw some other exception here, such as "IllegalArgumentException".}}
throw new java.lang.NullPointerException(); // Noncompliant {{Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
void withMessage(Object o) {
throw new NullPointerException("o must not be null"); // Compliant, a message is provided
}
void withComputedMessage(String name) {
throw new java.lang.NullPointerException(name + " must not be null"); // Compliant
}
void qix() throws IllegalArgumentException {
throw new IllegalArgumentException();
}
Expand Down
15 changes: 11 additions & 4 deletions java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,27 @@ public List<Tree.Kind> nodesToVisit() {
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.THROW_STATEMENT)) {
ExpressionTree expressionTree = ((ThrowStatementTree) tree).expression();
raiseIssueOnNpe(expressionTree, expressionTree.symbolType());
if (hasArguments(expressionTree)) {
return;
}
raiseIssueOnNpe(expressionTree, expressionTree.symbolType(), "Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.");
} else {
for (TypeTree throwClause : ((MethodTree) tree).throwsClauses()) {
raiseIssueOnNpe(throwClause, throwClause.symbolType());
raiseIssueOnNpe(throwClause, throwClause.symbolType(), "Remove NullPointerException from the throws clause.");
}
}
}

private void raiseIssueOnNpe(Tree tree, Type type) {
private void raiseIssueOnNpe(Tree tree, Type type, String message) {
if (type.is("java.lang.NullPointerException")) {
reportIssue(treeAtFault(tree), "Throw some other exception here, such as \"IllegalArgumentException\".");
reportIssue(treeAtFault(tree), message);
}
}

private static boolean hasArguments(ExpressionTree expressionTree) {
return expressionTree.is(Tree.Kind.NEW_CLASS) && !((NewClassTree) expressionTree).arguments().isEmpty();
}

private static Tree treeAtFault(Tree tree) {
return tree.is(Tree.Kind.NEW_CLASS) ? ((NewClassTree) tree).identifier() : tree;
}
Expand Down
Loading