Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3920b64

Browse files
atorralbaigfoo
authored andcommitted
Add support for live literals
1 parent 1f812f8 commit 3920b64

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
* Provides classes for working with Java expressions.
33
*/
44

5-
import Member
6-
import Type
7-
import Variable
8-
import Statement
5+
import java
6+
import semmle.code.java.frameworks.android.Compose
97

108
/** A common super-class that represents all kinds of expressions. */
119
class Expr extends ExprParent, @expr {
@@ -161,6 +159,8 @@ class CompileTimeConstantExpr extends Expr {
161159
v.isFinal() and
162160
v.getInitializer().isCompileTimeConstant()
163161
)
162+
or
163+
this instanceof LiveLiteral
164164
)
165165
}
166166

@@ -185,6 +185,8 @@ class CompileTimeConstantExpr extends Expr {
185185
exists(Variable v | this = v.getAnAccess() |
186186
result = v.getInitializer().(CompileTimeConstantExpr).getStringValue()
187187
)
188+
or
189+
result = this.(LiveLiteral).getValue().getStringValue()
188190
}
189191

190192
/**
@@ -297,6 +299,8 @@ class CompileTimeConstantExpr extends Expr {
297299
exists(Variable v | this = v.getAnAccess() |
298300
result = v.getInitializer().(CompileTimeConstantExpr).getBooleanValue()
299301
)
302+
or
303+
result = this.(LiveLiteral).getValue().getBooleanValue()
300304
}
301305

302306
/**
@@ -377,6 +381,8 @@ class CompileTimeConstantExpr extends Expr {
377381
result = v.getInitializer().(CompileTimeConstantExpr).getIntValue()
378382
)
379383
)
384+
or
385+
result = this.(LiveLiteral).getValue().getIntValue()
380386
}
381387
}
382388

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Provides classes and predicates for working with components generated by the Android's Jetpack Compose compiler.
3+
*/
4+
5+
import java
6+
7+
/**
8+
* A call to a live literal method.
9+
* This always returns a constant expression and can be considered as such.
10+
*/
11+
class LiveLiteral extends MethodAccess {
12+
CompileTimeConstantExpr value;
13+
14+
LiveLiteral() {
15+
this.getMethod() instanceof LiveLiteralMethod and
16+
not this.getEnclosingCallable() instanceof LiveLiteralMethod
17+
}
18+
19+
/** Gets the constant value that backs this live literal. */
20+
CompileTimeConstantExpr getValue() {
21+
result =
22+
any(ReturnStmt r | this.getMethod().calls*(r.getEnclosingCallable()))
23+
.getResult()
24+
.(VarAccess)
25+
.getVariable()
26+
.getInitializer()
27+
}
28+
29+
override string toString() { result = this.getValue().toString() }
30+
}
31+
32+
/** A live literal method. */
33+
class LiveLiteralMethod extends Method {
34+
LiveLiteralMethod() { this.getDeclaringType().getName().matches("LiveLiterals$%") }
35+
}

0 commit comments

Comments
 (0)