|
1 | 1 | package jp.skypencil.findbugs.slf4j;
|
2 | 2 |
|
3 |
| -import javax.annotation.Nullable; |
4 |
| - |
5 |
| -import jp.skypencil.findbugs.slf4j.parameter.AbstractDetectorForParameterArray; |
| 3 | +import jp.skypencil.findbugs.slf4j.parameter.AbstractDetectorForParameterArray2; |
6 | 4 | import jp.skypencil.findbugs.slf4j.parameter.ArrayData;
|
7 |
| -import jp.skypencil.findbugs.slf4j.parameter.ArrayDataHandler.Strategy; |
8 |
| - |
9 | 5 | import com.google.common.base.Objects;
|
10 | 6 |
|
11 |
| -import edu.umd.cs.findbugs.BugInstance; |
12 | 7 | import edu.umd.cs.findbugs.BugReporter;
|
13 | 8 | import edu.umd.cs.findbugs.OpcodeStack.CustomUserValue;
|
14 | 9 | import edu.umd.cs.findbugs.OpcodeStack.Item;
|
15 | 10 |
|
16 | 11 | @CustomUserValue
|
17 |
| -public final class ManualMessageDetector extends AbstractDetectorForParameterArray { |
| 12 | +public final class ManualMessageDetector extends AbstractDetectorForParameterArray2 { |
| 13 | + |
18 | 14 | @Item.SpecialKind
|
19 | 15 | private final int isMessage = Item.defineNewSpecialKind("message generated by throwable object");
|
20 | 16 |
|
21 | 17 | public ManualMessageDetector(BugReporter bugReporter) {
|
22 |
| - super(bugReporter); |
| 18 | + super(bugReporter, "SLF4J_MANUALLY_PROVIDED_MESSAGE"); |
23 | 19 | }
|
24 | 20 |
|
25 | 21 | @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; |
46 | 24 | }
|
47 | 25 |
|
48 | 26 | @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)); |
56 | 32 | }
|
57 | 33 |
|
58 |
| - private boolean isInvokingGetMessage(int seen) { |
| 34 | + |
| 35 | + @Override |
| 36 | + protected boolean isWhatWeWantToDetect(int seen) { |
59 | 37 | return seen == INVOKEVIRTUAL
|
60 | 38 | && !stack.isTop()
|
61 | 39 | && getThrowableHandler().checkThrowable(getStack().getStackItem(0))
|
62 | 40 | && (Objects.equal("getMessage", getNameConstantOperand()) ||
|
63 | 41 | Objects.equal("getLocalizedMessage", getNameConstantOperand()));
|
64 | 42 | }
|
65 | 43 |
|
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 |
| - } |
77 | 44 | }
|
0 commit comments