@@ -72,67 +72,73 @@ public class UsageWriteLogEventDiagnostic extends AbstractVisitorDiagnostic {
72
72
);
73
73
74
74
private static final int WRITE_LOG_EVENT_METHOD_PARAMS_COUNT = 5 ;
75
+ public static final int COMMENTS_PARAM_INDEX = 4 ;
76
+ public static final String NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK = "noErrorLogLevelInsideExceptBlock" ;
77
+ public static final String NO_DETAIL_ERROR_DESCRIPTION = "noDetailErrorDescription" ;
78
+ public static final String WRONG_NUMBER_MESSAGE = "wrongNumberMessage" ;
79
+ public static final String NO_SECOND_PARAMETER = "noSecondParameter" ;
80
+ public static final String NO_COMMENT = "noComment" ;
75
81
76
82
@ Override
77
- public ParseTree visitGlobalMethodCall (BSLParser .GlobalMethodCallContext ctx ) {
83
+ public ParseTree visitGlobalMethodCall (BSLParser .GlobalMethodCallContext context ) {
78
84
79
- if (!checkMethodName (ctx )) {
80
- return super .visitGlobalMethodCall (ctx );
85
+ if (!checkMethodName (context )) {
86
+ return super .visitGlobalMethodCall (context );
81
87
}
82
88
83
- checkParams (ctx );
89
+ checkParams (context );
84
90
85
- return super .visitGlobalMethodCall ( ctx );
91
+ return super .defaultResult ( );
86
92
}
87
93
88
- private void checkParams (BSLParser .GlobalMethodCallContext ctx ) {
89
- final var callParams = ctx .doCall ().callParamList ().callParam ();
90
- if (!checkFirstParams (ctx , callParams )){
94
+ private void checkParams (BSLParser .GlobalMethodCallContext context ) {
95
+ final var callParams = context .doCall ().callParamList ().callParam ();
96
+ if (!checkFirstParams (context , callParams )){
91
97
return ;
92
98
}
93
99
94
- if (isInsideExceptBlock (ctx )) {
100
+ if (isInsideExceptBlock (context )) {
95
101
96
102
final var logLevelCtx = callParams .get (1 );
97
103
if (!hasErrorLogLevel (logLevelCtx )) {
98
- fireIssue (ctx , "noErrorLogLevelInsideExceptBlock" );
104
+ fireIssue (context , NO_ERROR_LOG_LEVEL_INSIDE_EXCEPT_BLOCK );
99
105
return ;
100
106
}
101
107
102
- final var commentCtx = callParams .get (4 );
108
+ final var commentCtx = callParams .get (COMMENTS_PARAM_INDEX );
103
109
if (!isCommentCorrect (commentCtx )) {
104
- fireIssue (ctx , "noDetailErrorDescription" );
110
+ fireIssue (context , NO_DETAIL_ERROR_DESCRIPTION );
105
111
}
106
112
}
107
113
}
108
114
109
- private boolean checkFirstParams (BSLParser .GlobalMethodCallContext ctx , List <? extends BSLParser .CallParamContext > callParams ) {
115
+ private boolean checkFirstParams (BSLParser .GlobalMethodCallContext context , List <? extends BSLParser .CallParamContext > callParams ) {
110
116
if (callParams .size () < WRITE_LOG_EVENT_METHOD_PARAMS_COUNT ) {
111
- fireIssue (ctx , "wrongNumberMessage" );
117
+ fireIssue (context , WRONG_NUMBER_MESSAGE );
112
118
return false ;
113
119
}
114
120
115
121
final BSLParser .CallParamContext secondParamCtx = callParams .get (1 );
116
122
if (secondParamCtx .getChildCount () == 0 ) {
117
- fireIssue (ctx , "noSecondParameter" );
123
+ fireIssue (context , NO_SECOND_PARAMETER );
118
124
return false ;
119
125
}
120
126
121
- final BSLParser .CallParamContext commentCtx = callParams .get (4 );
127
+ final BSLParser .CallParamContext commentCtx = callParams .get (COMMENTS_PARAM_INDEX );
122
128
if (commentCtx .getChildCount () == 0 ) {
123
- fireIssue (ctx , "noComment" );
129
+ fireIssue (context , NO_COMMENT );
124
130
return false ;
125
131
}
126
132
return true ;
127
133
}
128
134
129
- private static boolean checkMethodName (BSLParser .GlobalMethodCallContext ctx ) {
130
- return WRITELOGEVENT .matcher (ctx .methodName ().getText ()).matches ();
135
+ private static boolean checkMethodName (BSLParser .GlobalMethodCallContext context ) {
136
+ return WRITELOGEVENT .matcher (context .methodName ().getText ()).matches ();
131
137
}
132
138
133
- private void fireIssue (BSLParser .GlobalMethodCallContext ctx , String messageKey ) {
139
+ private void fireIssue (BSLParser .GlobalMethodCallContext context , String messageKey ) {
134
140
var diagnosticMessage = info .getResourceString (messageKey );
135
- diagnosticStorage .addDiagnostic (ctx , diagnosticMessage );
141
+ diagnosticStorage .addDiagnostic (context , diagnosticMessage );
136
142
}
137
143
138
144
private static boolean hasErrorLogLevel (BSLParser .CallParamContext callParamContext ) {
@@ -208,50 +214,50 @@ private static boolean isValidExpression(
208
214
209
215
private static boolean isErrorDescriptionCallCorrect (Collection <ParseTree > globalCalls ) {
210
216
return globalCalls .stream ()
211
- .filter (ctx -> ctx instanceof BSLParser .GlobalMethodCallContext )
217
+ .filter (context -> context instanceof BSLParser .GlobalMethodCallContext )
212
218
.map (BSLParser .GlobalMethodCallContext .class ::cast )
213
- .filter (ctx -> isAppropriateName (ctx , PATTERN_DETAIL_ERROR_DESCRIPTION ))
219
+ .filter (context -> isAppropriateName (context , PATTERN_DETAIL_ERROR_DESCRIPTION ))
214
220
.anyMatch (UsageWriteLogEventDiagnostic ::hasFirstDescendantGlobalCall );
215
221
}
216
222
217
223
private static boolean isAppropriateName (
218
- BSLParser .GlobalMethodCallContext ctx ,
224
+ BSLParser .GlobalMethodCallContext context ,
219
225
Pattern patternDetailErrorDescription
220
226
) {
221
- return patternDetailErrorDescription .matcher (ctx .methodName ().getText ()).matches ();
227
+ return patternDetailErrorDescription .matcher (context .methodName ().getText ()).matches ();
222
228
}
223
229
224
230
private static boolean hasFirstDescendantGlobalCall (BSLParser .GlobalMethodCallContext globalCallCtx ) {
225
231
return Trees .findAllRuleNodes (globalCallCtx , BSLParser .RULE_globalMethodCall ).stream ()
226
232
.map (BSLParser .GlobalMethodCallContext .class ::cast )
227
- .anyMatch (ctx -> isAppropriateName (ctx , PATTERN_ERROR_INFO ));
233
+ .anyMatch (context -> isAppropriateName (context , PATTERN_ERROR_INFO ));
228
234
}
229
235
230
236
private static boolean hasSimpleErrorDescription (Collection <ParseTree > globalCalls ) {
231
237
return globalCalls .stream ()
232
- .filter (ctx -> ctx instanceof BSLParser .GlobalMethodCallContext )
233
- .anyMatch (ctx -> isAppropriateName ((BSLParser .GlobalMethodCallContext ) ctx , PATTERN_SIMPLE_ERROR_DESCRIPTION ));
238
+ .filter (context -> context instanceof BSLParser .GlobalMethodCallContext )
239
+ .anyMatch (context -> isAppropriateName ((BSLParser .GlobalMethodCallContext ) context , PATTERN_SIMPLE_ERROR_DESCRIPTION ));
234
240
}
235
241
236
242
private static boolean hasBriefErrorDescription (Collection <ParseTree > globalCalls ) {
237
243
return globalCalls .stream ()
238
- .filter (ctx -> ctx instanceof BSLParser .GlobalMethodCallContext )
239
- .anyMatch (ctx -> isAppropriateName ((BSLParser .GlobalMethodCallContext ) ctx , PATTERN_BRIEF_ERROR_DESCRIPTION ));
244
+ .filter (context -> context instanceof BSLParser .GlobalMethodCallContext )
245
+ .anyMatch (context -> isAppropriateName ((BSLParser .GlobalMethodCallContext ) context , PATTERN_BRIEF_ERROR_DESCRIPTION ));
240
246
}
241
247
242
- private static boolean isValidExpression (BSLParser .ExpressionContext ctx , BSLParser .CodeBlockContext codeBlock ,
248
+ private static boolean isValidExpression (BSLParser .ExpressionContext context , BSLParser .CodeBlockContext codeBlock ,
243
249
boolean checkPrevAssignment ) {
244
- return ctx .member ().stream ()
250
+ return context .member ().stream ()
245
251
.allMatch (memberContext -> isValidExpression (memberContext , codeBlock , checkPrevAssignment ));
246
252
}
247
253
248
- private static boolean isValidExpression (BSLParser .MemberContext ctx , BSLParser .CodeBlockContext codeBlock ,
254
+ private static boolean isValidExpression (BSLParser .MemberContext context , BSLParser .CodeBlockContext codeBlock ,
249
255
boolean checkPrevAssignment ) {
250
- if (!isInsideExceptBlock (codeBlock ) && ctx .constValue () != null ) {
256
+ if (!isInsideExceptBlock (codeBlock ) && context .constValue () != null ) {
251
257
return true ;
252
258
}
253
259
if (checkPrevAssignment ) {
254
- final var complexIdentifier = ctx .complexIdentifier ();
260
+ final var complexIdentifier = context .complexIdentifier ();
255
261
if (complexIdentifier != null ) {
256
262
return isValidVarAssignment (complexIdentifier , codeBlock );
257
263
}
@@ -282,8 +288,8 @@ private static Optional<BSLParser.AssignmentContext> getAssignment(
282
288
.filter (assignmentContext -> assignmentContext .lValue ().getText ().equalsIgnoreCase (varName ));
283
289
}
284
290
285
- private static boolean isInsideExceptBlock (BSLParserRuleContext ctx ) {
286
- return Trees .getRootParent (ctx , BSLParser .RULE_exceptCodeBlock ) != null ;
291
+ private static boolean isInsideExceptBlock (BSLParserRuleContext context ) {
292
+ return Trees .getRootParent (context , BSLParser .RULE_exceptCodeBlock ) != null ;
287
293
}
288
294
289
295
}
0 commit comments