From 703336a77fc5b639c2cf11d5c8416de737b27f5f Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 18 Jan 2021 16:44:53 +0100 Subject: [PATCH 1/2] Add ArrayInit.getSize(), improve documentation --- java/ql/src/semmle/code/java/Expr.qll | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/java/ql/src/semmle/code/java/Expr.qll b/java/ql/src/semmle/code/java/Expr.qll index 50f4a210d60..730c8c6c354 100755 --- a/java/ql/src/semmle/code/java/Expr.qll +++ b/java/ql/src/semmle/code/java/Expr.qll @@ -438,7 +438,7 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr { result.getIndex() = index } - /** Gets the initializer of this array creation expression. */ + /** Gets the initializer of this array creation expression, if any. */ ArrayInit getInit() { result.isNthChildOf(this, -2) } /** @@ -446,7 +446,7 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr { */ int getFirstDimensionSize() { if exists(getInit()) - then result = count(getInit().getAnInit()) + then result = getInit().getSize() else result = getDimension(0).(CompileTimeConstantExpr).getIntValue() } @@ -456,7 +456,17 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr { override string getAPrimaryQlClass() { result = "ArrayCreationExpr" } } -/** An array initializer occurs in an array creation expression. */ +/** + * An array initializer consisting of an opening and closing curly bracket and + * optionally containing expressions (which themselves can be array initializers) + * representing the elements of the array. For example: `{ 'a', 'b' }`. + * + * This expression type matches array initializers representing the values for + * annotation elements as well, despite the Java Language Specification considering + * them a separate type, *ElementValueArrayInitializer*. It does however not match + * values for an array annotation element which consist of a single element + * without enclosing curly brackets (as per JLS). + */ class ArrayInit extends Expr, @arrayinit { /** * An expression occurring in this initializer. @@ -469,6 +479,12 @@ class ArrayInit extends Expr, @arrayinit { /** Gets the initializer occurring at the specified (zero-based) position. */ Expr getInit(int index) { result = this.getAnInit() and result.getIndex() = index } + /** + * Gets the number of expressions in this initializer, i.e. the size the + * created array will have. + */ + int getSize() { result = count(getAnInit()) } + /** Gets a printable representation of this expression. */ override string toString() { result = "{...}" } From dde8d320f364aab86993b3b6c51ebe307b1b463f Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Tue, 19 Jan 2021 08:24:24 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Minor qldoc fixes. --- java/ql/src/semmle/code/java/Expr.qll | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/ql/src/semmle/code/java/Expr.qll b/java/ql/src/semmle/code/java/Expr.qll index 730c8c6c354..aef353088be 100755 --- a/java/ql/src/semmle/code/java/Expr.qll +++ b/java/ql/src/semmle/code/java/Expr.qll @@ -463,7 +463,7 @@ class ArrayCreationExpr extends Expr, @arraycreationexpr { * * This expression type matches array initializers representing the values for * annotation elements as well, despite the Java Language Specification considering - * them a separate type, *ElementValueArrayInitializer*. It does however not match + * them a separate type, `ElementValueArrayInitializer`. It does however not match * values for an array annotation element which consist of a single element * without enclosing curly brackets (as per JLS). */ @@ -480,7 +480,7 @@ class ArrayInit extends Expr, @arrayinit { Expr getInit(int index) { result = this.getAnInit() and result.getIndex() = index } /** - * Gets the number of expressions in this initializer, i.e. the size the + * Gets the number of expressions in this initializer, that is, the size the * created array will have. */ int getSize() { result = count(getAnInit()) }