forked from KengoTODA/findbugs-slf4j
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new SLF4J_GET_STACK_TRACE detector (fixes KengoTODA#70)
- Loading branch information
Showing
7 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
bug-pattern/src/main/java/jp/skypencil/findbugs/slf4j/ManualGetStackTraceDetector.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,36 @@ | ||
package jp.skypencil.findbugs.slf4j; | ||
|
||
import com.google.common.base.Objects; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.OpcodeStack.Item; | ||
import jp.skypencil.findbugs.slf4j.parameter.AbstractDetectorForParameterArray2; | ||
|
||
/** | ||
* FindBugs (now SpotBugs) Detector for (ab)use of {@link Throwable#getStackTrace()} in SFL4j logger. | ||
* | ||
* @author Michael Vorburger.ch | ||
*/ | ||
public class ManualGetStackTraceDetector extends AbstractDetectorForParameterArray2 { | ||
|
||
@Item.SpecialKind | ||
private final int isGetStackTrace = Item.defineNewSpecialKind("use of throwable getStackTrace"); | ||
|
||
public ManualGetStackTraceDetector(BugReporter bugReporter) { | ||
super(bugReporter, "SLF4J_GET_STACK_TRACE"); | ||
} | ||
|
||
@Override | ||
protected int getIsOfInterestKind() { | ||
return isGetStackTrace; | ||
} | ||
|
||
@Override | ||
protected boolean isWhatWeWantToDetect(int seen) { | ||
// return false; | ||
return seen == INVOKEVIRTUAL | ||
&& !stack.isTop() | ||
&& getThrowableHandler().checkThrowable(getStack().getStackItem(0)) | ||
&& Objects.equal("getStackTrace", getNameConstantOperand()); | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pkg; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class UsingGetStackTrace { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
void method(RuntimeException ex) { | ||
logger.error("Failed to determine The Answer to Life, The Universe and Everything; {}", 27, ex.getStackTrace()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test-case/src/test/java/jp/skypencil/findbugs/slf4j/BugInstanceMatcher.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,31 @@ | ||
package jp.skypencil.findbugs.slf4j; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import java.util.function.Predicate; | ||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
|
||
/** | ||
* Hamcrest matcher for FindBugs (now SpotBugs) {@link BugInstance}. | ||
* | ||
* @author Michael Vorburger.ch | ||
*/ | ||
public final class BugInstanceMatcher extends BaseMatcher<BugInstance> { | ||
|
||
private final Predicate<BugInstance> predicate; | ||
|
||
public BugInstanceMatcher(Predicate<BugInstance> predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public final boolean matches(Object item) { | ||
return predicate.test((BugInstance) item); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("BugInstance must match expected"); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
test-case/src/test/java/jp/skypencil/findbugs/slf4j/UsingGetStackTraceTest.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,25 @@ | ||
package jp.skypencil.findbugs.slf4j; | ||
|
||
import com.youdevise.fbplugins.tdd4fb.DetectorAssert; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.Detector; | ||
import org.junit.jupiter.api.Test; | ||
import pkg.UsingGetStackTrace; | ||
|
||
/** | ||
* Test for {@link ManualGetStackTraceDetector}. | ||
* | ||
* @author Michael Vorburger.ch | ||
*/ | ||
public class UsingGetStackTraceTest { | ||
|
||
@Test | ||
public void testExceptionMethods() throws Exception { | ||
BugReporter bugReporter = DetectorAssert.bugReporterForTesting(); | ||
Detector detector = new ManualGetStackTraceDetector(bugReporter); | ||
DetectorAssert.assertNoBugsReported(UsingGetStackTrace.class, detector, bugReporter); | ||
DetectorAssert.assertBugReported(UsingGetStackTrace.class, detector, bugReporter, new BugInstanceMatcher( | ||
bugInstance -> bugInstance.getBugPattern().getAbbrev().equals("SLF4J_GET_STACK_TRACE"))); | ||
} | ||
|
||
} |