Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0cb7f568d5313422bbeb34a833d021f0840efe97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation7
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.validation.internal.el.diagnostics;

import java.text.MessageFormat;

import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.jst.jsf.validation.internal.ELValidationPreferences;
import org.eclipse.osgi.util.NLS;

/**
 * Factory for all operator diagnostics.  Class should
 * not be implemented, instantiated or sub-classed.
 * 
 * @author cbateman
 *
 */
public final class DiagnosticFactory 
{
    
    /**
     * Problem id
     */
    public final static int BINARY_OP_BOTH_OPERANDS_NULL_ID = 0;
    /**
     * Problem id
     */
    public final static int BINARY_OP_POSSIBLE_DIVISION_BY_ZERO_ID = 1;
    /**
     * Problem id
     */
    public final static int BINARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION_ID = 2;
    /**
     * Problem id
     */
    public final static int BINARY_OP_CONSTANT_EXPRESSION_ALWAYS_EVAL_SAME_ID = 3;
    /**
     * Problem id
     */
    public final static int BINARY_OP_EQUALITY_COMP_WITH_NULL_ALWAYS_EVAL_SAME_ID = 4;
    /**
     * Problem id
     */
    public final static int BINARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN_ID = 5;
    /**
     * Problem id
     */
    public final static int BINARY_OP_FIRST_ARGUMENT_SHORT_CIRCUITS_ID = 6;
    /**
     * Problem id
     */
    public final static int BINARY_OP_SECOND_ARGUMENT_ALWAYS_EVAL_SAME_ID = 7;
    /**
     * Problem id
     */
    public final static int BINARY_OP_NO_AVAILABLE_TYPE_COERCION_ID = 8;
    /**
     * Problem id
     */
    public final static int BINARY_OP_COULD_NOT_COERCE_LITERALS_TO_NUMBERS_ID = 9;
    /**
     * Problem id
     */
    public final static int UNARY_OP_CONSTANT_EXPRESSION_EVAL_SAME_ID = 10;
    /**
     * Problem id
     */
    public final static int UNARY_OP_EMPTY_ALWAYS_FALSE_ON_TYPE_ID = 11;
    /**
     * Problem id
     */
    public final static int UNARY_OP_MINUS_ON_NULL_ALWAYS_ZERO_ID = 12;
    /**
     * Problem id
     */
    public final static int UNARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION_ID = 13;
    /**
     * Problem id
     */
    public final static int UNARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN_ID = 14;
    /**
     * Problem id
     */
    public final static int TERNARY_OP_CHOICE_IS_ALWAYS_SAME_ID = 15;
    /**
     * Problem id
     */
    public final static int TERNARY_OP_CANNOT_COERCE_CHOICE_TO_BOOLEAN_ID = 16; 
    /**
     * Problem id
     */
    public final static int UNARY_OP_STRING_CONVERSION_NOT_GUARANTEED_ID = 17;
    /**
     * Problem id
     */
    public final static int CANNOT_APPLY_OPERATOR_TO_METHOD_BINDING_ID = 18;
    /**
     * Problem id
     */
    public final static int MEMBER_NOT_FOUND_ID = 19;
    /**
     * Problem id
     */
    public final static int VARIABLE_NOT_FOUND_ID = 20;
    /**
     * Problem id
     */
    public final static int MISSING_CLOSING_EXPR_BRACKET_ID = 21;
    /**
     * Problem id
     */
    public final static int GENERAL_SYNTAX_ERROR_ID = 22;
    /**
     * Problem id
     */
    public final static int EMPTY_EL_EXPRESSION_ID = 23;
    
    /**
     * 
     */
    public final static int BINARY_OP_DOT_WITH_VALUEB_NULL_ID = 24;
    
    /**
     * Identifies a problem where we have x.y and x is a map.  In this case
     * the recommend syntax is x['y']
     */
    public final static int BINARY_OP_DOT_WITH_DOTTED_KEY_SHOULD_USE_ARRAY_ID = 25;
    
    /**
     * Indicates that a numeric array accessor is being used with a value
     * which is either < 0 or may be greater than the assumed size of the array
     */
    public final static int POSSIBLE_ARRAY_INDEX_OUT_OF_BOUNDS_ID = 26;
    
    /**
     * Identifies the problem where an expr like bean.enumProp == 'blah'
     * but 'blah' does not match any of the possible enum constants, meaning
     * that the operation will always resolve to a constant value (in this case false)
     */
    public final static int BINARY_COMPARISON_WITH_ENUM_ALWAYS_SAME_ID = 27;
    
    /**
     * Identifies a problem where two enum variables are compared but the enums are
     * not compatible.  i.e. bean.enum1 < bean.enum2.  Enum.compareTo() may throw
     * an exception in this case (CCE).
     */
    public final static int BINARY_OP_COMPARISON_OF_ENUMS_INCOMPATIBLE_ID = 28;
    
    /**
     * Count of ids
     */
    public final static int NUM_IDS = 29;
    
    /**
     * @param operatorName
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_BOTH_OPERANDS_NULL(final String operatorName)
    {
        final String message = NLS.bind(
                Messages.BINARY_OP_BOTH_OPERANDS_NULL, operatorName);
        return create(BINARY_OP_BOTH_OPERANDS_NULL_ID, message);
    }
    
    private static Diagnostic DIAGNOSTIC_POSSIBLE_DIV_BY_ZERO;
    
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_POSSIBLE_DIVISION_BY_ZERO()
    {
        if (DIAGNOSTIC_POSSIBLE_DIV_BY_ZERO == null)
        {
            final String message = Messages.POSSIBLE_DIV_BY_ZERO;
            DIAGNOSTIC_POSSIBLE_DIV_BY_ZERO = 
                create(BINARY_OP_POSSIBLE_DIVISION_BY_ZERO_ID, message);
        }
        return DIAGNOSTIC_POSSIBLE_DIV_BY_ZERO;
    }
    
    /**
     * @param operatorName
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION(final String operatorName)
    {
        final String message = 
            NLS.bind(Messages.BINARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION, operatorName);
        return create(BINARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION_ID, message);
    }
    
    /**
     * @param operatorName
     * @param value
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_CONSTANT_EXPRESSION_ALWAYS_EVAL_SAME(final String operatorName, final String value)
    {
        final String message =  
            NLS.bind(Messages.BINARY_OP_CONSTANT_EXPRESSION_ALWAYS_EVAL_SAME,operatorName, value);
        return create(BINARY_OP_CONSTANT_EXPRESSION_ALWAYS_EVAL_SAME_ID, message);
    }
    
    /**
     * @param value
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_EQUALITY_COMP_WITH_NULL_ALWAYS_EVAL_SAME(final String value)
    {
        final String message = 
            NLS.bind(Messages.BINARY_OP_EQUALITY_COMP_WITH_NULL_ALWAYS_EVAL_SAME, value);
        return create(BINARY_OP_EQUALITY_COMP_WITH_NULL_ALWAYS_EVAL_SAME_ID, message);
    }

    /**
     * @param whichArgument
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN(final String whichArgument)
    {
        final String message = 
            NLS.bind(Messages.BINARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN, whichArgument);
        return create(BINARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN_ID, message);
    }
    
    /**
     * @param shortCircuitValue
     * @param operatorName
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_FIRST_ARGUMENT_SHORT_CIRCUITS(boolean shortCircuitValue, String operatorName)
    {
        final String message = 
            NLS.bind(Messages.BINARY_OP_FIRST_ARGUMENT_SHORT_CIRCUITS, 
                    Boolean.valueOf(shortCircuitValue), operatorName);
        return create(BINARY_OP_FIRST_ARGUMENT_SHORT_CIRCUITS_ID, message); 
    }
    
    /**
     * @param shortCircuitValue
     * @param operatorName
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_SECOND_ARGUMENT_ALWAYS_EVAL_SAME(final boolean shortCircuitValue, final String operatorName)
    {
        final String message =
            NLS.bind(Messages.BINARY_OP_SECOND_ARGUMENT_ALWAYS_EVAL_SAME, 
                    Boolean.valueOf(shortCircuitValue), operatorName);
        
        return create(BINARY_OP_SECOND_ARGUMENT_ALWAYS_EVAL_SAME_ID, message); 
    }

    private static Diagnostic DIAGNOSTIC_NO_AVAILABLE_TYPE_COERCION;
        
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_NO_AVAILABLE_TYPE_COERCION()
    {
        if (DIAGNOSTIC_NO_AVAILABLE_TYPE_COERCION == null)
        {
            DIAGNOSTIC_NO_AVAILABLE_TYPE_COERCION = 
              create(BINARY_OP_NO_AVAILABLE_TYPE_COERCION_ID, 
                    Messages.BINARY_OP_NO_AVAILABLE_TYPE_COERCION);
        }
        
        return DIAGNOSTIC_NO_AVAILABLE_TYPE_COERCION;
    }

    private static Diagnostic DIAGNOSTIC_COULD_NOT_COERCE_LITERALS;
    
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_COULD_NOT_COERCE_LITERALS_TO_NUMBERS()
    {
        if (DIAGNOSTIC_COULD_NOT_COERCE_LITERALS == null)
        {
            DIAGNOSTIC_COULD_NOT_COERCE_LITERALS = 
                create(BINARY_OP_COULD_NOT_COERCE_LITERALS_TO_NUMBERS_ID, 
                    Messages.BINARY_OP_COULD_NOT_COERCE_LITERALS_TO_NUMBERS);
        }
        
        return DIAGNOSTIC_COULD_NOT_COERCE_LITERALS;
    }
    
    /**
     * @param operatorName
     * @param value
     * @return a configured diagnostic
     */
    public Diagnostic create_UNARY_OP_CONSTANT_EXPRESSION_EVAL_SAME_ID(final String operatorName, final String value)
    {
        final String message = 
            NLS.bind(Messages.UNARY_OP_CONSTANT_EXPRESSION_EVAL_SAME, 
                    operatorName, value);
        return create(UNARY_OP_CONSTANT_EXPRESSION_EVAL_SAME_ID, message);
    }
    
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_UNARY_OP_EMPTY_ALWAYS_FALSE_ON_TYPE()
    {
        return create(UNARY_OP_EMPTY_ALWAYS_FALSE_ON_TYPE_ID, 
             Messages.UNARY_OP_EMPTY_ALWAYS_FALSE_ON_TYPE);
    }
    
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_UNARY_OP_MINUS_ON_NULL_ALWAYS_ZERO()
    {
        return create(UNARY_OP_MINUS_ON_NULL_ALWAYS_ZERO_ID, 
            Messages.UNARY_OP_MINUS_ON_NULL_ALWAYS_ZERO);
    }
    
    /**
     * @param operatorName
     * @return a configured diagnostic
     */
    public Diagnostic create_UNARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION(final String operatorName)
    {
        final String message = 
            NLS.bind(Messages.UNARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION,
                    operatorName);
        return create(UNARY_OP_COULD_NOT_MAKE_NUMERIC_COERCION_ID, message);
    }
    
    /**
     * @param operatorName
     * @return a configured diagnostic
     */
    public Diagnostic create_UNARY_OP_STRING_CONVERSION_NOT_GUARANTEED(final String operatorName)
    {
        final String message =
            NLS.bind(Messages.UNARY_OP_STRING_CONVERSION_NOT_GUARANTEED,
                                operatorName);
        return create(UNARY_OP_STRING_CONVERSION_NOT_GUARANTEED_ID, message);
    }
    
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_UNARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN()
    {
        return create(UNARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN_ID, 
                Messages.UNARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN);
    }
    
    /**
     * @param result
     * @param whichSelected
     * @return a configured diagnostic
     */
    public Diagnostic create_TERNARY_OP_CHOICE_IS_ALWAYS_SAME(boolean result, String whichSelected)
    {
        final String message =
            NLS.bind(Messages.TERNARY_OP_CHOICE_IS_ALWAYS_SAME, 
                    Boolean.valueOf(result), whichSelected);
        return create(TERNARY_OP_CHOICE_IS_ALWAYS_SAME_ID, message);
    }
    
    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_TERNARY_OP_CANNOT_COERCE_CHOICE_TO_BOOLEAN()
    {
        return create(TERNARY_OP_CANNOT_COERCE_CHOICE_TO_BOOLEAN_ID, 
                Messages.TERNARY_OP_CANNOT_COERCE_CHOICE_TO_BOOLEAN_ID);
    }
    
    /**
     * Method bindings cannot participate in expressions involving operators
     * Per JSP 1.1 spec section 5.2.1
     * 
     * @return a configured message
     */
    public Diagnostic create_CANNOT_APPLY_OPERATOR_TO_METHOD_BINDING()
    {
        return create(CANNOT_APPLY_OPERATOR_TO_METHOD_BINDING_ID, 
                Messages.CANNOT_APPLY_OPERATORS_TO_MB);
    }

    /**
     * @param curMemberSymbol
     * @param owningMember
     * @return a configured message
     */
    public Diagnostic create_MEMBER_NOT_FOUND(String curMemberSymbol, String owningMember)
    {
        return create(MEMBER_NOT_FOUND_ID,
                NLS.bind(Messages.VM_PROP_NAME_NOT_FOUND, curMemberSymbol, owningMember));
    }

    /**
     * @param variableName
     * @return a configured message
     */
    public Diagnostic create_VARIABLE_NOT_FOUND(String variableName)
    {
        return create(VARIABLE_NOT_FOUND_ID
                , NLS.bind(Messages.VM_ROOT_NAME_NOT_FOUND, variableName));
    }

    /**
     * @return a configured message
     */
    public Diagnostic create_MISSING_CLOSING_EXPR_BRACKET()
    {
        return create(MISSING_CLOSING_EXPR_BRACKET_ID
                , Messages.MISSING_CLOSING_EXPR_BRACKET);
    }
    
    /**
     * @return a configured message
     */
    public Diagnostic create_GENERAL_SYNTAX_ERROR()
    {
        return create(GENERAL_SYNTAX_ERROR_ID, Messages.GENERAL_SYNTAX_ERROR);
    }

    /**
     * @return a configured message
     */
    public Diagnostic create_EMPTY_EL_EXPRESSION()
    {
        return create(EMPTY_EL_EXPRESSION_ID, Messages.EMPTY_EL_EXPRESSION);
    }

    /**
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_DOT_WITH_VALUEB_NULL()
    {
        return create(BINARY_OP_DOT_WITH_VALUEB_NULL_ID
                , Messages.BINARY_OP_DOT_WITH_VALUEB_NULL);
    }
    
    /**
     * @param valAName
     * @param valueBName
     * @return a configured diagnostic
     */
    public Diagnostic create_BINARY_OP_DOT_WITH_VALUEA_MAP_SHOULD_USE_ARRAY(final String valAName, final String valueBName)
    {
        final Object[] formatArgs = new Object[] {valAName, valueBName};
        return create(BINARY_OP_DOT_WITH_DOTTED_KEY_SHOULD_USE_ARRAY_ID,
                MessageFormat.format(Messages.BINARY_OP_DOT_WITH_DOTTED_KEY_SHOULD_USE_ARRAY,formatArgs));
    }
    
    /**
     * @param value
     * @return an array index out of bounds diagnostic
     */
    public Diagnostic create_POSSIBLE_ARRAY_INDEX_OUT_OF_BOUNDS(final Integer value)
    {
        final Object[] formatArgs = new Object[] {value};

        return create(POSSIBLE_ARRAY_INDEX_OUT_OF_BOUNDS_ID
                , MessageFormat.format(Messages.POSSIBLE_ARRAY_INDEX_OUT_OF_BOUNDS,formatArgs));
    }
    
    /**
     * @param operatorName
     * @param invariantResult
     * @param enumName
     * @param fieldName
     * @return a diagnostic
     */
    public Diagnostic create_BINARY_COMPARISON_WITH_ENUM_AND_CONST_ALWAYS_SAME(final String operatorName, final boolean invariantResult, final String enumName,  final String fieldName)
    {
        return create(BINARY_COMPARISON_WITH_ENUM_ALWAYS_SAME_ID
                , MessageFormat.format(Messages.BINARY_COMPARISON_WITH_ENUM_AND_CONST_ALWAYS_SAME
                                      , new Object[] {operatorName
                                      , Boolean.valueOf(invariantResult), enumName, fieldName}));
    }

    /**
     * @param operatorName
     * @param invariantResult
     * @param enumName1
     * @param enumName2
     * @return a diagnostic
     */
    public Diagnostic create_BINARY_COMPARISON_WITH_TWO_ENUMS_ALWAYS_SAME(final String operatorName, final boolean invariantResult, final String enumName1, final String enumName2)
    {
        return create(BINARY_COMPARISON_WITH_ENUM_ALWAYS_SAME_ID
                , MessageFormat.format(Messages.BINARY_COMPARISON_WITH_TWO_ENUMS_ALWAYS_SAME
                                      , new Object[] {operatorName
                                      , Boolean.valueOf(invariantResult), enumName1, enumName2}));
    }
    
    /**
     * @param operatorName
     * @param invariantResult
     * @param enumName
     * @param nonEnum
     * @return a diagnostic
     */
    public Diagnostic create_BINARY_COMPARISON_WITH_ENUM_AND_UNCOERCABLE_NONCONST_ALWAYS_SAME(final String operatorName, final boolean invariantResult, final String enumName, final String nonEnum)
    {
        return create(BINARY_COMPARISON_WITH_ENUM_ALWAYS_SAME_ID
                , MessageFormat.format(Messages.BINARY_COMPARISON_WITH_ENUM_AND_UNCOERCABLE_NONCONST_ALWAYS_SAME
                                      , new Object[] {operatorName
                                      , Boolean.valueOf(invariantResult), enumName, nonEnum}));
    }
    
    /**
     * @return a diagnostic
     */
    public Diagnostic create_BINARY_OP_COMPARISON_OF_ENUMS_INCOMPATIBLE()
    {
        return create(BINARY_OP_COMPARISON_OF_ENUMS_INCOMPATIBLE_ID
                , Messages.BINARY_OP_COMPARISON_OF_ENUMS_INCOMPATIBLE);
    }

    private BasicDiagnostic create(int diagnosticId, String message)
    {
        final int severity = ELValidationPreferences.getDefaultSeverity(diagnosticId);
        return new BasicDiagnostic(severity, "" //$NON-NLS-1$
                , diagnosticId
                , message
                , null);
    }
}

Back to the top