@@ -438,15 +438,15 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr {
438
438
result .getIndex ( ) = index
439
439
}
440
440
441
- /** Gets the initializer of this array creation expression. */
441
+ /** Gets the initializer of this array creation expression, if any . */
442
442
ArrayInit getInit ( ) { result .isNthChildOf ( this , - 2 ) }
443
443
444
444
/**
445
445
* Gets the size of the first dimension, if it can be statically determined.
446
446
*/
447
447
int getFirstDimensionSize ( ) {
448
448
if exists ( getInit ( ) )
449
- then result = count ( getInit ( ) .getAnInit ( ) )
449
+ then result = getInit ( ) .getSize ( )
450
450
else result = getDimension ( 0 ) .( CompileTimeConstantExpr ) .getIntValue ( )
451
451
}
452
452
@@ -456,7 +456,17 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr {
456
456
override string getAPrimaryQlClass ( ) { result = "ArrayCreationExpr" }
457
457
}
458
458
459
- /** An array initializer occurs in an array creation expression. */
459
+ /**
460
+ * An array initializer consisting of an opening and closing curly bracket and
461
+ * optionally containing expressions (which themselves can be array initializers)
462
+ * representing the elements of the array. For example: `{ 'a', 'b' }`.
463
+ *
464
+ * This expression type matches array initializers representing the values for
465
+ * annotation elements as well, despite the Java Language Specification considering
466
+ * them a separate type, *ElementValueArrayInitializer*. It does however not match
467
+ * values for an array annotation element which consist of a single element
468
+ * without enclosing curly brackets (as per JLS).
469
+ */
460
470
class ArrayInit extends Expr , @arrayinit {
461
471
/**
462
472
* An expression occurring in this initializer.
@@ -469,6 +479,12 @@ class ArrayInit extends Expr, @arrayinit {
469
479
/** Gets the initializer occurring at the specified (zero-based) position. */
470
480
Expr getInit ( int index ) { result = this .getAnInit ( ) and result .getIndex ( ) = index }
471
481
482
+ /**
483
+ * Gets the number of expressions in this initializer, i.e. the size the
484
+ * created array will have.
485
+ */
486
+ int getSize ( ) { result = count ( getAnInit ( ) ) }
487
+
472
488
/** Gets a printable representation of this expression. */
473
489
override string toString ( ) { result = "{...}" }
474
490
0 commit comments