1
1
package jp .skypencil .findbugs .slf4j ;
2
2
3
+ import static org .apache .bcel .Const .INVOKEVIRTUAL ;
4
+
3
5
import com .google .common .base .Objects ;
4
- import jp .skypencil .findbugs .slf4j .parameter .AbstractDetectorForParameterArray2 ;
5
- import jp .skypencil .findbugs .slf4j .parameter .ArrayData ;
6
- import edu .umd .cs .findbugs .BugInstance ;
7
6
import edu .umd .cs .findbugs .BugReporter ;
8
7
import edu .umd .cs .findbugs .OpcodeStack .CustomUserValue ;
9
8
import edu .umd .cs .findbugs .OpcodeStack .Item ;
10
- import javax .annotation .Nullable ;
11
- import jp .skypencil .findbugs .slf4j .parameter .AbstractDetectorForParameterArray ;
12
- import jp .skypencil .findbugs .slf4j .parameter .ArrayDataHandler .Strategy ;
9
+ import jp .skypencil .findbugs .slf4j .parameter .AbstractExtendedDetectorForParameterArray ;
10
+ import jp .skypencil .findbugs .slf4j .parameter .ArrayData ;
13
11
14
12
@ CustomUserValue
15
- public final class ManualMessageDetector extends AbstractDetectorForParameterArray {
13
+ public final class ManualMessageDetector extends AbstractExtendedDetectorForParameterArray {
16
14
17
15
@ Item .SpecialKind
18
16
private final int isMessage = Item .defineSpecialKind ("message generated by throwable object" );
@@ -22,38 +20,8 @@ public ManualMessageDetector(BugReporter bugReporter) {
22
20
}
23
21
24
22
@ Override
25
- protected Strategy createArrayCheckStrategy () {
26
- return (storedItem , arrayData , index ) -> {
27
- if (arrayData == null ) {
28
- return ;
29
- }
30
-
31
- if (storedItem .getSpecialKind () == isMessage ) {
32
- arrayData .mark (true );
33
- }
34
-
35
- // Let developer logs exception message, only when argument does not have throwable instance
36
- // https://github.com/KengoTODA/findbugs-slf4j/issues/31
37
- if (index == arrayData .getSize () - 1 && !getThrowableHandler ().checkThrowable (storedItem )) {
38
- arrayData .mark (false );
39
- }
40
- };
41
- }
42
-
43
- @ Override
44
- public void afterOpcode (int seen ) {
45
- boolean isInvokingGetMessage = isInvokingGetMessage (seen );
46
- super .afterOpcode (seen );
47
-
48
- if (isInvokingGetMessage && !stack .isTop ()) {
49
- stack .getStackItem (0 ).setSpecialKind (isMessage );
50
- }
51
- }
52
-
53
-
54
- @ Override
55
- protected int getIsOfInterestKind () {
56
- return isMessage ;
23
+ protected int getKindOfInterest () {
24
+ return isMessage ;
57
25
}
58
26
59
27
@ Override
@@ -64,33 +32,11 @@ protected boolean isReallyOfInterest(Item storedItem, ArrayData arrayData, int i
64
32
&& !getThrowableHandler ().checkThrowable (storedItem ));
65
33
}
66
34
67
- private boolean isInvokingGetMessage (int seen ) {
68
- return seen == INVOKEVIRTUAL
69
- && !stack .isTop ()
70
- && getThrowableHandler ().checkThrowable (getStack ().getStackItem (0 ))
71
- && (Objects .equal ("getMessage" , getNameConstantOperand ())
72
- || Objects .equal ("getLocalizedMessage" , getNameConstantOperand ()));
73
- }
74
-
75
35
@ Override
76
- protected void onLog (@ Nullable String format , @ Nullable ArrayData arrayData ) {
77
- if (arrayData == null || !arrayData .isMarked ()) {
78
- return ;
36
+ protected boolean isWhatWeWantToDetect (int seen ) {
37
+ return seen == INVOKEVIRTUAL && !stack .isTop ()
38
+ && getThrowableHandler ().checkThrowable (getStack ().getStackItem (0 ))
39
+ && (Objects .equal ("getMessage" , getNameConstantOperand ())
40
+ || Objects .equal ("getLocalizedMessage" , getNameConstantOperand ()));
79
41
}
80
- BugInstance bugInstance =
81
- new BugInstance (this , "SLF4J_MANUALLY_PROVIDED_MESSAGE" , NORMAL_PRIORITY )
82
- .addSourceLine (this )
83
- .addClassAndMethod (this )
84
- .addCalledMethod (this );
85
- getBugReporter ().reportBug (bugInstance );
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
-
96
42
}
0 commit comments