1
1
package jp .skypencil .findbugs .slf4j ;
2
2
3
- import static org .apache .bcel .Const .INVOKEVIRTUAL ;
4
-
5
3
import com .google .common .base .Objects ;
4
+ import jp .skypencil .findbugs .slf4j .parameter .AbstractDetectorForParameterArray2 ;
5
+ import jp .skypencil .findbugs .slf4j .parameter .ArrayData ;
6
6
import edu .umd .cs .findbugs .BugInstance ;
7
7
import edu .umd .cs .findbugs .BugReporter ;
8
8
import edu .umd .cs .findbugs .OpcodeStack .CustomUserValue ;
9
9
import edu .umd .cs .findbugs .OpcodeStack .Item ;
10
10
import javax .annotation .Nullable ;
11
11
import jp .skypencil .findbugs .slf4j .parameter .AbstractDetectorForParameterArray ;
12
- import jp .skypencil .findbugs .slf4j .parameter .ArrayData ;
13
12
import jp .skypencil .findbugs .slf4j .parameter .ArrayDataHandler .Strategy ;
14
13
15
14
@ CustomUserValue
16
15
public final class ManualMessageDetector extends AbstractDetectorForParameterArray {
16
+
17
17
@ Item .SpecialKind
18
18
private final int isMessage = Item .defineSpecialKind ("message generated by throwable object" );
19
19
20
20
public ManualMessageDetector (BugReporter bugReporter ) {
21
- super (bugReporter );
21
+ super (bugReporter , "SLF4J_MANUALLY_PROVIDED_MESSAGE" );
22
22
}
23
23
24
24
@ Override
25
25
protected Strategy createArrayCheckStrategy () {
26
- return new Strategy () {
27
- @ Override
28
- public void store (Item storedItem , ArrayData arrayData , int index ) {
26
+ return (storedItem , arrayData , index ) -> {
29
27
if (arrayData == null ) {
30
28
return ;
31
29
}
@@ -39,8 +37,7 @@ public void store(Item storedItem, ArrayData arrayData, int index) {
39
37
if (index == arrayData .getSize () - 1 && !getThrowableHandler ().checkThrowable (storedItem )) {
40
38
arrayData .mark (false );
41
39
}
42
- }
43
- };
40
+ };
44
41
}
45
42
46
43
@ Override
@@ -53,6 +50,20 @@ public void afterOpcode(int seen) {
53
50
}
54
51
}
55
52
53
+
54
+ @ Override
55
+ protected int getIsOfInterestKind () {
56
+ return isMessage ;
57
+ }
58
+
59
+ @ Override
60
+ protected boolean isReallyOfInterest (Item storedItem , ArrayData arrayData , int index ) {
61
+ // Let developer logs exception message, only when argument does not have throwable instance
62
+ // https://github.com/KengoTODA/findbugs-slf4j/issues/31
63
+ return !(index == arrayData .getSize () - 1
64
+ && !getThrowableHandler ().checkThrowable (storedItem ));
65
+ }
66
+
56
67
private boolean isInvokingGetMessage (int seen ) {
57
68
return seen == INVOKEVIRTUAL
58
69
&& !stack .isTop ()
@@ -73,4 +84,13 @@ protected void onLog(@Nullable String format, @Nullable ArrayData arrayData) {
73
84
.addCalledMethod (this );
74
85
getBugReporter ().reportBug (bugInstance );
75
86
}
87
+
88
+ @ Override
89
+ protected boolean isWhatWeWantToDetect (int seen ) {
90
+ return seen == INVOKEVIRTUAL && !stack .isTop ()
91
+ && getThrowableHandler ().checkThrowable (getStack ().getStackItem (0 ))
92
+ && (Objects .equal ("getMessage" , getNameConstantOperand ())
93
+ || Objects .equal ("getLocalizedMessage" , getNameConstantOperand ()));
94
+ }
95
+
76
96
}
0 commit comments