Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new detector for rule ERR07 (#1956)
* Added detector and tests for ThrowingExceptions * Issue893 - removed throws clause * Issue1338 - specialized throws clause * Ran gradlew spotlessApply * docs: move CHANGELOG entry under the "Unreleased" Co-authored-by: Bálint Dominik Orosz <bck909@INF.ELTE.HU> Co-authored-by: Kengo TODA <skypencil@gmail.com> Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
- Loading branch information
1 parent
3155f38
commit 818b14c
Showing
8 changed files
with
216 additions
and
2 deletions.
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
49 changes: 49 additions & 0 deletions
49
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/CheckThrowingExceptions.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,49 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
|
||
public class CheckThrowingExceptions extends AbstractIntegrationTest { | ||
@Test | ||
public void throwingExceptionsTests() { | ||
performAnalysis("MethodsThrowingExceptions.class"); | ||
|
||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", 1); | ||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION", 1); | ||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_CLAUSE_THROWABLE", 1); | ||
|
||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", 1, "MethodsThrowingExceptions", "isCapitalizedThrowingRuntimeException"); | ||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", 0, "MethodsThrowingExceptions", "isCapitalizedThrowingSpecializedException"); | ||
|
||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION", 1, "MethodsThrowingExceptions", "methodThrowingBasicException"); | ||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION", 0, "MethodsThrowingExceptions", "methodThrowingIOException"); | ||
|
||
assertNumOfTHROWSBugs("THROWS_METHOD_THROWS_CLAUSE_THROWABLE", 1, "MethodsThrowingExceptions", "methodThrowingThrowable"); | ||
} | ||
|
||
private void assertNumOfTHROWSBugs(String bugType, int num) { | ||
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType(bugType) | ||
.build(); | ||
assertThat(getBugCollection(), containsExactly(num, bugTypeMatcher)); | ||
} | ||
|
||
private void assertNumOfTHROWSBugs(String bugType, int num, String className, String method) { | ||
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType(bugType) | ||
.inClass(className) | ||
.inMethod(method) | ||
.build(); | ||
if (num > 0) { | ||
assertThat(getBugCollection(), hasItem(bugTypeMatcher)); | ||
} | ||
assertThat(getBugCollection(), containsExactly(num, 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
51 changes: 51 additions & 0 deletions
51
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/ThrowingExceptions.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,51 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.OpcodeStack; | ||
import edu.umd.cs.findbugs.ba.XMethod; | ||
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector; | ||
|
||
import org.apache.bcel.Const; | ||
import org.apache.bcel.classfile.ExceptionTable; | ||
import org.apache.bcel.classfile.Method; | ||
|
||
|
||
public class ThrowingExceptions extends OpcodeStackDetector { | ||
private final BugReporter bugReporter; | ||
|
||
public ThrowingExceptions(BugReporter bugReporter) { | ||
this.bugReporter = bugReporter; | ||
} | ||
|
||
@Override | ||
public void visit(Method obj) { | ||
ExceptionTable exceptionTable = obj.getExceptionTable(); | ||
if (exceptionTable != null) { | ||
String[] exceptionNames = exceptionTable.getExceptionNames(); | ||
for (String exception : exceptionNames) { | ||
if ("java.lang.Exception".equals(exception)) { | ||
reportBug("THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION", getXMethod()); | ||
} else if ("java.lang.Throwable".equals(exception)) { | ||
reportBug("THROWS_METHOD_THROWS_CLAUSE_THROWABLE", getXMethod()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void sawOpcode(int seen) { | ||
if (seen == Const.ATHROW) { | ||
OpcodeStack.Item item = stack.getStackItem(0); | ||
if (item != null) { | ||
if ("Ljava/lang/RuntimeException;".equals(item.getSignature())) { | ||
reportBug("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", getXMethod()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void reportBug(String bugName, XMethod method) { | ||
bugReporter.reportBug(new BugInstance(this, bugName, NORMAL_PRIORITY).addClass(this).addMethod(method)); | ||
} | ||
} |
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,39 @@ | ||
import java.io.IOException; | ||
|
||
public class MethodsThrowingExceptions { | ||
boolean isCapitalizedThrowingRuntimeException(String s) { | ||
if (s == null) { | ||
throw new RuntimeException("Null String"); | ||
} | ||
if (s.equals("")) { | ||
return true; | ||
} | ||
String first = s.substring(0, 1); | ||
String rest = s.substring(1); | ||
return first.equals(first.toUpperCase()) && rest.equals(rest.toLowerCase()); | ||
} | ||
|
||
boolean isCapitalizedThrowingSpecializedException(String s) { | ||
if (s == null) { | ||
throw new NullPointerException(); | ||
} | ||
if (s.equals("")) { | ||
return true; | ||
} | ||
String first = s.substring(0, 1); | ||
String rest = s.substring(1); | ||
return first.equals(first.toUpperCase()) && rest.equals(rest.toLowerCase()); | ||
} | ||
|
||
private void methodThrowingBasicException() throws Exception { | ||
System.out.println("Method throwing Exception"); | ||
} | ||
|
||
private void methodThrowingIOException() throws IOException { | ||
System.out.println("Method throwing IOException"); | ||
} | ||
|
||
private void methodThrowingThrowable() throws Throwable { | ||
System.out.println("Method throwing Throwable"); | ||
} | ||
} |
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