Skip to content

Commit d1a2c62

Browse files
committed
factor AbstractDetectorForParameterArray2 out of ManualMessageDetector
so that in the next commit the upcoming ManualGetStackTraceDetector for a SLF4J_GET_STACK_TRACE (issue KengoTODA#70) does not have to copy/paste 3/4 of the ManualMessageDetector
1 parent 55693e4 commit d1a2c62

File tree

2 files changed

+79
-47
lines changed

2 files changed

+79
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,44 @@
11
package jp.skypencil.findbugs.slf4j;
22

3-
import javax.annotation.Nullable;
4-
5-
import jp.skypencil.findbugs.slf4j.parameter.AbstractDetectorForParameterArray;
3+
import jp.skypencil.findbugs.slf4j.parameter.AbstractDetectorForParameterArray2;
64
import jp.skypencil.findbugs.slf4j.parameter.ArrayData;
7-
import jp.skypencil.findbugs.slf4j.parameter.ArrayDataHandler.Strategy;
8-
95
import com.google.common.base.Objects;
106

11-
import edu.umd.cs.findbugs.BugInstance;
127
import edu.umd.cs.findbugs.BugReporter;
138
import edu.umd.cs.findbugs.OpcodeStack.CustomUserValue;
149
import edu.umd.cs.findbugs.OpcodeStack.Item;
1510

1611
@CustomUserValue
17-
public final class ManualMessageDetector extends AbstractDetectorForParameterArray {
12+
public final class ManualMessageDetector extends AbstractDetectorForParameterArray2 {
13+
1814
@Item.SpecialKind
1915
private final int isMessage = Item.defineNewSpecialKind("message generated by throwable object");
2016

2117
public ManualMessageDetector(BugReporter bugReporter) {
22-
super(bugReporter);
18+
super(bugReporter, "SLF4J_MANUALLY_PROVIDED_MESSAGE");
2319
}
2420

2521
@Override
26-
protected Strategy createArrayCheckStrategy() {
27-
return new Strategy() {
28-
@Override
29-
public void store(Item storedItem, ArrayData arrayData, int index) {
30-
if (arrayData == null) {
31-
return;
32-
}
33-
34-
if (storedItem.getSpecialKind() == isMessage) {
35-
arrayData.mark(true);
36-
}
37-
38-
// Let developer logs exception message, only when argument does not have throwable instance
39-
// https://github.com/KengoTODA/findbugs-slf4j/issues/31
40-
if (index == arrayData.getSize() - 1
41-
&& !getThrowableHandler().checkThrowable(storedItem)) {
42-
arrayData.mark(false);
43-
}
44-
}
45-
};
22+
protected int getIsOfInterestKind() {
23+
return isMessage;
4624
}
4725

4826
@Override
49-
public void afterOpcode(int seen) {
50-
boolean isInvokingGetMessage = isInvokingGetMessage(seen);
51-
super.afterOpcode(seen);
52-
53-
if (isInvokingGetMessage && !stack.isTop()) {
54-
stack.getStackItem(0).setSpecialKind(isMessage);
55-
}
27+
protected boolean isReallyOfInterest(Item storedItem, ArrayData arrayData, int index) {
28+
// Let developer logs exception message, only when argument does not have throwable instance
29+
// https://github.com/KengoTODA/findbugs-slf4j/issues/31
30+
return !(index == arrayData.getSize() - 1
31+
&& !getThrowableHandler().checkThrowable(storedItem));
5632
}
5733

58-
private boolean isInvokingGetMessage(int seen) {
34+
35+
@Override
36+
protected boolean isWhatWeWantToDetect(int seen) {
5937
return seen == INVOKEVIRTUAL
6038
&& !stack.isTop()
6139
&& getThrowableHandler().checkThrowable(getStack().getStackItem(0))
6240
&& (Objects.equal("getMessage", getNameConstantOperand()) ||
6341
Objects.equal("getLocalizedMessage", getNameConstantOperand()));
6442
}
6543

66-
@Override
67-
protected void onLog(@Nullable String format, @Nullable ArrayData arrayData) {
68-
if (arrayData == null || !arrayData.isMarked()) {
69-
return;
70-
}
71-
BugInstance bugInstance = new BugInstance(this,
72-
"SLF4J_MANUALLY_PROVIDED_MESSAGE", NORMAL_PRIORITY)
73-
.addSourceLine(this).addClassAndMethod(this)
74-
.addCalledMethod(this);
75-
getBugReporter().reportBug(bugInstance);
76-
}
7744
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package jp.skypencil.findbugs.slf4j.parameter;
2+
3+
import edu.umd.cs.findbugs.BugInstance;
4+
import edu.umd.cs.findbugs.BugReporter;
5+
import edu.umd.cs.findbugs.OpcodeStack.Item;
6+
import javax.annotation.Nullable;
7+
import jp.skypencil.findbugs.slf4j.parameter.ArrayDataHandler.Strategy;
8+
9+
public abstract class AbstractDetectorForParameterArray2 extends AbstractDetectorForParameterArray {
10+
11+
private final String bugPatternName;
12+
13+
public AbstractDetectorForParameterArray2(BugReporter bugReporter, String bugPatternName) {
14+
super(bugReporter);
15+
this.bugPatternName = bugPatternName;
16+
}
17+
18+
@Override
19+
protected final Strategy createArrayCheckStrategy() {
20+
return (storedItem, arrayData, index) -> {
21+
if (arrayData == null) {
22+
return;
23+
}
24+
25+
if (storedItem.getSpecialKind() == getIsOfInterestKind()) {
26+
arrayData.mark(true);
27+
}
28+
29+
if (!isReallyOfInterest(storedItem, arrayData, index)) {
30+
arrayData.mark(false);
31+
}
32+
};
33+
}
34+
35+
protected boolean isReallyOfInterest(Item storedItem, ArrayData arrayData, int index) {
36+
return true;
37+
}
38+
39+
@Override
40+
public final void afterOpcode(int seen) {
41+
boolean isInvokingGetMessage = isWhatWeWantToDetect(seen);
42+
super.afterOpcode(seen);
43+
44+
if (isInvokingGetMessage && !stack.isTop()) {
45+
stack.getStackItem(0).setSpecialKind(getIsOfInterestKind());
46+
}
47+
}
48+
49+
@Override
50+
protected final void onLog(@Nullable String format, @Nullable ArrayData arrayData) {
51+
if (arrayData == null || !arrayData.isMarked()) {
52+
return;
53+
}
54+
BugInstance bugInstance = new BugInstance(this,
55+
bugPatternName, NORMAL_PRIORITY)
56+
.addSourceLine(this).addClassAndMethod(this)
57+
.addCalledMethod(this);
58+
getBugReporter().reportBug(bugInstance);
59+
}
60+
61+
abstract protected boolean isWhatWeWantToDetect(int seen);
62+
63+
abstract protected int getIsOfInterestKind();
64+
65+
}

0 commit comments

Comments
 (0)