Merge pull request #8360 from JLLeitschuh/feat/JLL/compile_time_constant_getStringified

[Java] Add CompileTimeConstantExpr.getStringified method
This commit is contained in:
Chris Smowton
2022-03-11 10:34:52 +00:00
committed by GitHub
14 changed files with 343 additions and 12 deletions

View File

@@ -161,6 +161,38 @@ class CompileTimeConstantExpr extends Expr {
)
}
/**
* Gets the stringified value of this expression, where possible.
*
* The stringified version of a compile-time constant expression is the equivalent to
* the result of calling `String.valueOf(expr)` on the expression.
*
* Note that this does not handle the following cases:
*
* - mathematical computations of type `long`, `float`, or `double`.
*/
pragma[nomagic]
string getStringifiedValue() {
result = this.getStringValue()
or
result = this.(Literal).getValue()
or
result = this.getBooleanValue().toString()
or
result = this.getIntValue().toString()
or
// Ternary conditional, with compile-time constant condition.
exists(ConditionalExpr ce, boolean condition |
ce = this and
condition = ce.getCondition().(CompileTimeConstantExpr).getBooleanValue() and
result = ce.getBranchExpr(condition).(CompileTimeConstantExpr).getStringifiedValue()
)
or
exists(Variable v | this = v.getAnAccess() |
result = v.getInitializer().(CompileTimeConstantExpr).getStringifiedValue()
)
}
/**
* Gets the string value of this expression, where possible.
*/
@@ -168,11 +200,11 @@ class CompileTimeConstantExpr extends Expr {
string getStringValue() {
result = this.(StringLiteral).getValue()
or
result = this.(CharacterLiteral).getValue()
or
this.getType() instanceof TypeString and // When the expression type is `String`
result =
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringValue()
// Then the resultant string is the addition of both operands stringified value, regardless of type.
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringifiedValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringifiedValue()
or
// Ternary conditional, with compile-time constant condition.
exists(ConditionalExpr ce, boolean condition |