Skip to content

Commit 703336a

Browse files
authored
Add ArrayInit.getSize(), improve documentation
1 parent fc2fe6c commit 703336a

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

java/ql/src/semmle/code/java/Expr.qll

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,15 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr {
438438
result.getIndex() = index
439439
}
440440

441-
/** Gets the initializer of this array creation expression. */
441+
/** Gets the initializer of this array creation expression, if any. */
442442
ArrayInit getInit() { result.isNthChildOf(this, -2) }
443443

444444
/**
445445
* Gets the size of the first dimension, if it can be statically determined.
446446
*/
447447
int getFirstDimensionSize() {
448448
if exists(getInit())
449-
then result = count(getInit().getAnInit())
449+
then result = getInit().getSize()
450450
else result = getDimension(0).(CompileTimeConstantExpr).getIntValue()
451451
}
452452

@@ -456,7 +456,17 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr {
456456
override string getAPrimaryQlClass() { result = "ArrayCreationExpr" }
457457
}
458458

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+
*/
460470
class ArrayInit extends Expr, @arrayinit {
461471
/**
462472
* An expression occurring in this initializer.
@@ -469,6 +479,12 @@ class ArrayInit extends Expr, @arrayinit {
469479
/** Gets the initializer occurring at the specified (zero-based) position. */
470480
Expr getInit(int index) { result = this.getAnInit() and result.getIndex() = index }
471481

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+
472488
/** Gets a printable representation of this expression. */
473489
override string toString() { result = "{...}" }
474490

0 commit comments

Comments
 (0)