Skip to content

Commit 054aab2

Browse files
Kotlin: test full value class extraction
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 63553e3 commit 054aab2

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
classes
2+
| test.kt:3:1:5:1 | Base | abstract, public |
3+
| test.kt:7:1:9:1 | PairValue | final, public |
4+
supertypes
5+
| test.kt:7:1:9:1 | PairValue | test.kt:3:1:5:1 | Base |
6+
properties
7+
| test.kt:4:14:4:27 | value | int | public | test.kt:4:14:4:27 | getValue | <none> |
8+
| test.kt:7:32:7:45 | value | int | public | test.kt:7:32:7:45 | getValue | value |
9+
| test.kt:7:48:7:64 | label | String | public | test.kt:7:48:7:64 | getLabel | label |
10+
constructors
11+
| test.kt:3:1:5:1 | Base | Base() |
12+
| test.kt:7:22:7:65 | PairValue | PairValue(int,java.lang.String) |
13+
| test.kt:8:5:8:68 | PairValue | PairValue(long) |
14+
constructorCalls
15+
| test.kt:7:1:9:1 | super(...) | test.kt:3:1:5:1 | Base |
16+
| test.kt:8:32:8:68 | this(...) | test.kt:7:22:7:65 | PairValue |
17+
| test.kt:11:40:11:55 | new PairValue(...) | test.kt:8:5:8:68 | PairValue |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// codeql-extractor-kotlin-options: -XXLanguage:+FullValueClasses
2+
3+
abstract value class Base {
4+
abstract val value: Int
5+
}
6+
7+
value class PairValue(override val value: Int, val label: String) : Base() {
8+
constructor(value: Long) : this(value.toInt(), value.toString())
9+
}
10+
11+
fun makePairValue(value: Long): Base = PairValue(value)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java
2+
3+
string backingField(Property p) {
4+
if exists(p.getBackingField()) then result = p.getBackingField().toString() else result = "<none>"
5+
}
6+
7+
query predicate classes(Class c, string classModifiers) {
8+
c.fromSource() and
9+
not c.isCompilerGenerated() and
10+
c.getLocation().getStartLine() > 0 and
11+
classModifiers = concat(string m | c.hasModifier(m) | m, ", ")
12+
}
13+
14+
query predicate supertypes(Class c, Class supertype) {
15+
c.fromSource() and
16+
supertype.fromSource() and
17+
extendsReftype(c, supertype)
18+
}
19+
20+
query predicate properties(
21+
Property p, string propertyType, string propertyModifiers, Method getter, string field
22+
) {
23+
p.fromSource() and
24+
propertyType = p.getGetter().getReturnType().toString() and
25+
propertyModifiers = concat(string m | p.hasModifier(m) | m, ", ") and
26+
getter = p.getGetter() and
27+
field = backingField(p)
28+
}
29+
30+
query predicate constructors(Constructor c, string signature) {
31+
c.fromSource() and
32+
signature = c.getSignature()
33+
}
34+
35+
query predicate constructorCalls(ConstructorCall call, Constructor target) {
36+
call.getEnclosingCallable().fromSource() and
37+
target = call.getConstructor() and
38+
target.getSourceDeclaration().fromSource()
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.ql

0 commit comments

Comments
 (0)