Skip to content

Commit 9844ae7

Browse files
authored
Merge pull request #9219 from igfoo/igfoo/livelits
Improve LiveLiterals
2 parents 8beef45 + 5498f41 commit 9844ae7

File tree

1 file changed

+22
-7
lines changed
  • java/ql/lib/semmle/code/java/frameworks/android

1 file changed

+22
-7
lines changed

java/ql/lib/semmle/code/java/frameworks/android/Compose.qll

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ class LiveLiteral extends MethodAccess {
1414
not this.getEnclosingCallable() instanceof LiveLiteralMethod
1515
}
1616

17-
/** Gets the constant value that backs this live literal. */
17+
/**
18+
* Live literal classes consist of the following:
19+
* - A private field holding the constant value that backs this live literal.
20+
* - A private getter to access the constant value.
21+
* - A public getter that either calls the private getter and returns its result or,
22+
* if live literals are activated, returns the value of a dynamic state object that is initialized with
23+
* the constant value.
24+
*
25+
* This predicate gets the constant value held by the private field.
26+
*/
1827
CompileTimeConstantExpr getValue() {
19-
result =
20-
any(ReturnStmt r | this.getMethod().calls*(r.getEnclosingCallable()))
21-
.getResult()
22-
.(VarAccess)
23-
.getVariable()
24-
.getInitializer()
28+
exists(MethodAccess getterCall, VarAccess va |
29+
methodReturns(this.getMethod(), getterCall) and
30+
methodReturns(getterCall.getMethod(), va) and
31+
result = va.getVariable().getInitializer()
32+
)
2533
}
2634

2735
override string toString() { result = this.getValue().toString() }
@@ -31,3 +39,10 @@ class LiveLiteral extends MethodAccess {
3139
class LiveLiteralMethod extends Method {
3240
LiveLiteralMethod() { this.getDeclaringType().getName().matches("LiveLiterals$%") }
3341
}
42+
43+
private predicate methodReturns(Method m, Expr res) {
44+
exists(ReturnStmt r |
45+
r.getResult() = res and
46+
r.getEnclosingCallable() = m
47+
)
48+
}

0 commit comments

Comments
 (0)