Java: Make loop classes extend LoopStmt and use getBody instead of getStmt.

This commit is contained in:
Anders Schack-Mulligen
2026-02-04 14:30:00 +01:00
parent 6f40ac15b4
commit 4fcf3fbff8
5 changed files with 73 additions and 59 deletions

View File

@@ -140,7 +140,7 @@ class IfStmt extends ConditionalStmt, @ifstmt {
}
/** A `for` loop. */
class ForStmt extends ConditionalStmt, @forstmt {
class ForStmt extends ConditionalStmt, LoopStmtImpl, @forstmt {
/**
* Gets an initializer expression of the loop.
*
@@ -167,8 +167,15 @@ class ForStmt extends ConditionalStmt, @forstmt {
index = result.getIndex() - 3
}
/**
* DEPRECATED: Use getBody() instead.
*
* Gets the body of this `for` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this and result.getIndex() = 2 }
/** Gets the body of this `for` loop. */
Stmt getStmt() { result.getParent() = this and result.getIndex() = 2 }
override Stmt getBody() { result.getParent() = this and result.getIndex() = 2 }
/**
* Gets a variable that is used as an iteration variable: it is defined,
@@ -191,7 +198,7 @@ class ForStmt extends ConditionalStmt, @forstmt {
this.getCondition().getAChildExpr*() = result.getAnAccess()
}
override string pp() { result = "for (...;...;...) " + this.getStmt().pp() }
override string pp() { result = "for (...;...;...) " + this.getBody().pp() }
override string toString() { result = "for (...;...;...)" }
@@ -201,17 +208,24 @@ class ForStmt extends ConditionalStmt, @forstmt {
}
/** An enhanced `for` loop. (Introduced in Java 5.) */
class EnhancedForStmt extends Stmt, @enhancedforstmt {
class EnhancedForStmt extends LoopStmtImpl, @enhancedforstmt {
/** Gets the local variable declaration expression of this enhanced `for` loop. */
LocalVariableDeclExpr getVariable() { result.getParent() = this }
/** Gets the expression over which this enhanced `for` loop iterates. */
Expr getExpr() { result.isNthChildOf(this, 1) }
/** Gets the body of this enhanced `for` loop. */
Stmt getStmt() { result.getParent() = this }
/**
* DEPRECATED: Use getBody() instead.
*
* Gets the body of this enhanced `for` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this }
override string pp() { result = "for (... : ...) " + this.getStmt().pp() }
/** Gets the body of this enhanced `for` loop. */
override Stmt getBody() { result.getParent() = this }
override string pp() { result = "for (... : ...) " + this.getBody().pp() }
override string toString() { result = "for (... : ...)" }
@@ -221,14 +235,21 @@ class EnhancedForStmt extends Stmt, @enhancedforstmt {
}
/** A `while` loop. */
class WhileStmt extends ConditionalStmt, @whilestmt {
class WhileStmt extends ConditionalStmt, LoopStmtImpl, @whilestmt {
/** Gets the boolean condition of this `while` loop. */
override Expr getCondition() { result.getParent() = this }
/** Gets the body of this `while` loop. */
Stmt getStmt() { result.getParent() = this }
/**
* DEPRECATED: Use getBody() instead.
*
* Gets the body of this `while` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this }
override string pp() { result = "while (...) " + this.getStmt().pp() }
/** Gets the body of this `while` loop. */
override Stmt getBody() { result.getParent() = this }
override string pp() { result = "while (...) " + this.getBody().pp() }
override string toString() { result = "while (...)" }
@@ -238,14 +259,21 @@ class WhileStmt extends ConditionalStmt, @whilestmt {
}
/** A `do` loop. */
class DoStmt extends ConditionalStmt, @dostmt {
class DoStmt extends ConditionalStmt, LoopStmtImpl, @dostmt {
/** Gets the condition of this `do` loop. */
override Expr getCondition() { result.getParent() = this }
/** Gets the body of this `do` loop. */
Stmt getStmt() { result.getParent() = this }
/**
* DEPRECATED: Use getBody() instead.
*
* Gets the body of this `do` loop.
*/
deprecated Stmt getStmt() { result.getParent() = this }
override string pp() { result = "do " + this.getStmt().pp() + " while (...)" }
/** Gets the body of this `do` loop. */
override Stmt getBody() { result.getParent() = this }
override string pp() { result = "do " + this.getBody().pp() + " while (...)" }
override string toString() { result = "do ... while (...)" }
@@ -258,30 +286,16 @@ class DoStmt extends ConditionalStmt, @dostmt {
* A loop statement, including `for`, enhanced `for`,
* `while` and `do` statements.
*/
class LoopStmt extends Stmt {
LoopStmt() {
this instanceof ForStmt or
this instanceof EnhancedForStmt or
this instanceof WhileStmt or
this instanceof DoStmt
}
abstract private class LoopStmtImpl extends Stmt {
/** Gets the body of this loop statement. */
Stmt getBody() {
result = this.(ForStmt).getStmt() or
result = this.(EnhancedForStmt).getStmt() or
result = this.(WhileStmt).getStmt() or
result = this.(DoStmt).getStmt()
}
abstract Stmt getBody();
/** Gets the boolean condition of this loop statement. */
Expr getCondition() {
result = this.(ForStmt).getCondition() or
result = this.(WhileStmt).getCondition() or
result = this.(DoStmt).getCondition()
}
Expr getCondition() { none() }
}
final class LoopStmt = LoopStmtImpl;
/** A `try` statement. */
class TryStmt extends Stmt, @trystmt {
/** Gets the block of the `try` statement. */