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)
incl. adjustments for code review feedback * delete useless inline comment * add @Item.SpecialKind annotation * rename getIsOfInterestKind to getKindOfInterest * rename isGetStackTrace to getStackTraceKind * rename AbstractDetectorForParameterArray2 to AbstractExtendedDetectorForParameterArray
- Loading branch information
Showing
13 changed files
with
199 additions
and
131 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
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 @@ | ||
/.apt_generated_tests/ |
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
package jp.skypencil.findbugs.slf4j; | ||
|
||
import static org.apache.bcel.Const.INVOKEVIRTUAL; | ||
|
||
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.AbstractExtendedDetectorForParameterArray; | ||
|
||
/** | ||
* FindBugs (now SpotBugs) Detector for (ab)use of {@link Throwable#getStackTrace()} in SFL4j logger. | ||
* | ||
* @author Michael Vorburger.ch | ||
*/ | ||
public class ManualGetStackTraceDetector extends AbstractExtendedDetectorForParameterArray { | ||
|
||
@Item.SpecialKind | ||
private final int getStackTraceKind = Item.defineNewSpecialKind("use of throwable getStackTrace"); | ||
|
||
public ManualGetStackTraceDetector(BugReporter bugReporter) { | ||
super(bugReporter, "SLF4J_GET_STACK_TRACE"); | ||
} | ||
|
||
@Override | ||
@Item.SpecialKind | ||
protected int getKindOfInterest() { | ||
return getStackTraceKind; | ||
} | ||
|
||
@Override | ||
protected boolean isWhatWeWantToDetect(int seen) { | ||
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
65 changes: 0 additions & 65 deletions
65
...c/main/java/jp/skypencil/findbugs/slf4j/parameter/AbstractDetectorForParameterArray2.java
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
...java/jp/skypencil/findbugs/slf4j/parameter/AbstractExtendedDetectorForParameterArray.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,65 @@ | ||
package jp.skypencil.findbugs.slf4j.parameter; | ||
|
||
import edu.umd.cs.findbugs.BugInstance; | ||
import edu.umd.cs.findbugs.BugReporter; | ||
import edu.umd.cs.findbugs.OpcodeStack.Item; | ||
import javax.annotation.Nullable; | ||
import jp.skypencil.findbugs.slf4j.parameter.ArrayDataHandler.Strategy; | ||
|
||
public abstract class AbstractExtendedDetectorForParameterArray extends AbstractDetectorForParameterArray { | ||
|
||
private final String bugPatternName; | ||
|
||
public AbstractExtendedDetectorForParameterArray(BugReporter bugReporter, String bugPatternName) { | ||
super(bugReporter); | ||
this.bugPatternName = bugPatternName; | ||
} | ||
|
||
@Override | ||
protected final Strategy createArrayCheckStrategy() { | ||
return (storedItem, arrayData, index) -> { | ||
if (arrayData == null) { | ||
return; | ||
} | ||
|
||
if (storedItem.getSpecialKind() == getKindOfInterest()) { | ||
arrayData.mark(true); | ||
} | ||
|
||
if (!isReallyOfInterest(storedItem, arrayData, index)) { | ||
arrayData.mark(false); | ||
} | ||
}; | ||
} | ||
|
||
protected boolean isReallyOfInterest(Item storedItem, ArrayData arrayData, int index) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public final void afterOpcode(int seen) { | ||
boolean isInvokingGetMessage = isWhatWeWantToDetect(seen); | ||
super.afterOpcode(seen); | ||
|
||
if (isInvokingGetMessage && !stack.isTop()) { | ||
stack.getStackItem(0).setSpecialKind(getKindOfInterest()); | ||
} | ||
} | ||
|
||
@Override | ||
protected final void onLog(@Nullable String format, @Nullable ArrayData arrayData) { | ||
if (arrayData == null || !arrayData.isMarked()) { | ||
return; | ||
} | ||
BugInstance bugInstance = new BugInstance(this, | ||
bugPatternName, NORMAL_PRIORITY) | ||
.addSourceLine(this).addClassAndMethod(this) | ||
.addCalledMethod(this); | ||
getBugReporter().reportBug(bugInstance); | ||
} | ||
|
||
abstract protected boolean isWhatWeWantToDetect(int seen); | ||
|
||
abstract protected int getKindOfInterest(); | ||
|
||
} |
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 @@ | ||
/.apt_generated_tests/ |
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: {}; cause: {}", 27, ex.getStackTrace()); | ||
} | ||
} |
Oops, something went wrong.