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

Skip to content

Commit d057530

Browse files
tamasvajkigfoo
authored andcommitted
Add property reference tests
1 parent f3b92e7 commit d057530

4 files changed

Lines changed: 279 additions & 17 deletions

File tree

java/ql/test/kotlin/library-tests/reflection/PrintAst.expected

Lines changed: 214 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
reflection.kt:
22
# 0| [CompilationUnit] reflection
3-
# 3| 1: [Class] Reflection
3+
# 0| 1: [Class] ReflectionKt
4+
# 45| 1: [ExtensionMethod] getLastChar
5+
#-----| 4: (Parameters)
6+
# 44| 0: [Parameter] <this>
7+
# 45| 5: [BlockStmt] { ... }
8+
# 45| 0: [ReturnStmt] return ...
9+
# 45| 0: [MethodAccess] get(...)
10+
# 45| -1: [ThisAccess] this
11+
# 45| 0: [SubExpr] ... - ...
12+
# 45| 0: [MethodAccess] getLength(...)
13+
# 45| -1: [ThisAccess] this
14+
# 45| 1: [IntegerLiteral] 1
15+
# 47| 2: [Method] fn2
16+
# 47| 5: [BlockStmt] { ... }
17+
# 48| 0: [ExprStmt] <Expr>;
18+
# 48| 0: [MethodAccess] println(...)
19+
# 48| -1: [TypeAccess] ConsoleKt
20+
# 48| 0: [MethodAccess] get(...)
21+
# 48| 0: [StringLiteral] abc
22+
# 49| 1: [ExprStmt] <Expr>;
23+
# 49| 0: [MethodAccess] println(...)
24+
# 49| -1: [TypeAccess] ConsoleKt
25+
# 49| 0: [MethodAccess] get(...)
26+
# 3| 2: [Class] Reflection
427
# 3| 1: [Constructor] Reflection
528
# 3| 5: [BlockStmt] { ... }
629
# 3| 0: [SuperConstructorInvocationStmt] super(...)
730
# 3| 1: [ExprStmt] <Expr>;
831
# 3| 0: [MethodAccess] <obinit>(...)
932
# 4| 3: [Method] fn
10-
#-----| 4: (Parameters)
11-
# 4| 0: [Parameter] boo
1233
# 4| 5: [BlockStmt] { ... }
1334
# 5| 0: [LocalVariableDeclStmt] var ...;
1435
# 5| 1: [LocalVariableDeclExpr] ref
@@ -32,15 +53,194 @@ reflection.kt:
3253
# 6| -1: [TypeAccess] ConsoleKt
3354
# 6| 0: [MethodAccess] getName(...)
3455
# 6| -1: [VarAccess] ref
35-
# 9| 4: [Class] Ccc
36-
# 9| 1: [Constructor] Ccc
37-
# 9| 5: [BlockStmt] { ... }
38-
# 9| 0: [SuperConstructorInvocationStmt] super(...)
39-
# 9| 1: [ExprStmt] <Expr>;
40-
# 9| 0: [MethodAccess] <obinit>(...)
41-
# 10| 3: [Method] m
56+
# 8| 2: [LocalVariableDeclStmt] var ...;
57+
# 8| 1: [LocalVariableDeclExpr] x0
58+
# 9| 3: [LocalVariableDeclStmt] var ...;
59+
# 9| 1: [LocalVariableDeclExpr] x1
60+
# 9| 0: [MethodAccess] get(...)
61+
# 9| -1: [VarAccess] x0
62+
# 9| 0: [ClassInstanceExpr] new C(...)
63+
# 9| -3: [TypeAccess] C
64+
# 10| 4: [LocalVariableDeclStmt] var ...;
65+
# 10| 1: [LocalVariableDeclExpr] x2
66+
# 10| 0: [MethodAccess] getName(...)
67+
# 10| -1: [VarAccess] x0
68+
# 11| 5: [LocalVariableDeclStmt] var ...;
69+
# 11| 1: [LocalVariableDeclExpr] x3
70+
# 11| 0: [MethodAccess] getGetter(...)
71+
# 11| -1: [VarAccess] x0
72+
# 12| 6: [LocalVariableDeclStmt] var ...;
73+
# 12| 1: [LocalVariableDeclExpr] x4
74+
# 12| 0: [MemberRefExpr] ...::...
75+
# 12| -4: [AnonymousClass] new Function1<T,V>(...) { ... }
76+
# 12| 1: [Constructor]
77+
#-----| 4: (Parameters)
78+
# 12| 0: [Parameter] <dispatchReceiver>
79+
# 12| 5: [BlockStmt] { ... }
80+
# 12| 0: [SuperConstructorInvocationStmt] super(...)
81+
# 12| 1: [ExprStmt] <Expr>;
82+
# 12| 0: [AssignExpr] ...=...
83+
# 12| 0: [VarAccess] this.<dispatchReceiver>
84+
# 12| -1: [ThisAccess] this
85+
# 12| 1: [VarAccess] <dispatchReceiver>
86+
# 12| 1: [FieldDeclaration] KProperty1<C,Integer> <dispatchReceiver>;
87+
# 12| -1: [TypeAccess] KProperty1<C,Integer>
88+
# 12| 1: [Method] invoke
89+
#-----| 4: (Parameters)
90+
# 12| 0: [Parameter] a0
91+
# 12| 5: [BlockStmt] { ... }
92+
# 12| 0: [ReturnStmt] return ...
93+
# 12| 0: [MethodAccess] get(...)
94+
# 12| -1: [VarAccess] this.<dispatchReceiver>
95+
# 12| -1: [ThisAccess] this
96+
# 12| 0: [VarAccess] a0
97+
# 12| -3: [TypeAccess] Function1<T,V>
98+
# 12| 0: [VarAccess] x0
99+
# 13| 7: [LocalVariableDeclStmt] var ...;
100+
# 13| 1: [LocalVariableDeclExpr] x5
101+
# 15| 8: [LocalVariableDeclStmt] var ...;
102+
# 15| 1: [LocalVariableDeclExpr] y0
103+
# 16| 9: [LocalVariableDeclStmt] var ...;
104+
# 16| 1: [LocalVariableDeclExpr] y1
105+
# 16| 0: [MethodAccess] set(...)
106+
# 16| -1: [VarAccess] y0
107+
# 16| 0: [ClassInstanceExpr] new C(...)
108+
# 16| -3: [TypeAccess] C
109+
# 16| 1: [IntegerLiteral] 5
110+
# 17| 10: [LocalVariableDeclStmt] var ...;
111+
# 17| 1: [LocalVariableDeclExpr] y2
112+
# 17| 0: [MethodAccess] getName(...)
113+
# 17| -1: [VarAccess] y0
114+
# 18| 11: [LocalVariableDeclStmt] var ...;
115+
# 18| 1: [LocalVariableDeclExpr] y3
116+
# 18| 0: [MethodAccess] getSetter(...)
117+
# 18| -1: [VarAccess] y0
118+
# 19| 12: [LocalVariableDeclStmt] var ...;
119+
# 19| 1: [LocalVariableDeclExpr] y4
120+
# 19| 0: [MemberRefExpr] ...::...
121+
# 19| -4: [AnonymousClass] new Function2<T,V,Unit>(...) { ... }
122+
# 19| 1: [Constructor]
123+
#-----| 4: (Parameters)
124+
# 19| 0: [Parameter] <dispatchReceiver>
125+
# 19| 5: [BlockStmt] { ... }
126+
# 19| 0: [SuperConstructorInvocationStmt] super(...)
127+
# 19| 1: [ExprStmt] <Expr>;
128+
# 19| 0: [AssignExpr] ...=...
129+
# 19| 0: [VarAccess] this.<dispatchReceiver>
130+
# 19| -1: [ThisAccess] this
131+
# 19| 1: [VarAccess] <dispatchReceiver>
132+
# 19| 1: [FieldDeclaration] KMutableProperty1<C,Integer> <dispatchReceiver>;
133+
# 19| -1: [TypeAccess] KMutableProperty1<C,Integer>
134+
# 19| 1: [Method] invoke
135+
#-----| 4: (Parameters)
136+
# 19| 0: [Parameter] a0
137+
# 19| 1: [Parameter] a1
138+
# 19| 5: [BlockStmt] { ... }
139+
# 19| 0: [ReturnStmt] return ...
140+
# 19| 0: [MethodAccess] set(...)
141+
# 19| -1: [VarAccess] this.<dispatchReceiver>
142+
# 19| -1: [ThisAccess] this
143+
# 19| 0: [VarAccess] a0
144+
# 19| 1: [VarAccess] a1
145+
# 19| -3: [TypeAccess] Function2<T,V,Unit>
146+
# 19| 0: [VarAccess] y0
147+
# 20| 13: [LocalVariableDeclStmt] var ...;
148+
# 20| 1: [LocalVariableDeclExpr] y5
149+
# 22| 14: [LocalVariableDeclStmt] var ...;
150+
# 22| 1: [LocalVariableDeclExpr] prop
151+
# 22| 0: [CastExpr] (...)...
152+
# 22| 0: [TypeAccess] KProperty2<C,Integer,Integer>
153+
# 22| 1: [MethodAccess] single(...)
154+
# 22| -2: [TypeAccess] KCallable<?>
155+
# 22| -1: [TypeAccess] CollectionsKt
156+
# 22| 0: [MethodAccess] getMembers(...)
157+
# 22| -1: [TypeLiteral] C.class
158+
# 22| 0: [TypeAccess] C
159+
# 22| 1: [LambdaExpr] ...->...
160+
# 22| -4: [AnonymousClass] new Function1<KCallable<?>,Boolean>(...) { ... }
161+
# 22| 1: [Constructor]
162+
# 22| 5: [BlockStmt] { ... }
163+
# 22| 0: [SuperConstructorInvocationStmt] super(...)
164+
# 22| 1: [Method] invoke
165+
#-----| 4: (Parameters)
166+
# 22| 0: [Parameter] it
167+
# 22| 5: [BlockStmt] { ... }
168+
# 22| 0: [ReturnStmt] return ...
169+
# 22| 0: [ValueEQExpr] ... (value equals) ...
170+
# 22| 0: [MethodAccess] getName(...)
171+
# 22| -1: [VarAccess] it
172+
# 22| 1: [StringLiteral] p3
173+
# 22| -3: [TypeAccess] Function1<KCallable<?>,Boolean>
174+
# 23| 15: [LocalVariableDeclStmt] var ...;
175+
# 23| 1: [LocalVariableDeclExpr] z0
176+
# 23| 0: [MethodAccess] get(...)
177+
# 23| -1: [VarAccess] prop
178+
# 23| 0: [ClassInstanceExpr] new C(...)
179+
# 23| -3: [TypeAccess] C
180+
# 23| 1: [IntegerLiteral] 5
181+
# 26| 4: [Class] Ccc
182+
# 26| 1: [Constructor] Ccc
183+
# 26| 5: [BlockStmt] { ... }
184+
# 26| 0: [SuperConstructorInvocationStmt] super(...)
185+
# 26| 1: [ExprStmt] <Expr>;
186+
# 26| 0: [MethodAccess] <obinit>(...)
187+
# 27| 3: [Method] m
188+
#-----| 4: (Parameters)
189+
# 27| 0: [Parameter] i
190+
# 27| 5: [BlockStmt] { ... }
191+
# 27| 0: [ReturnStmt] return ...
192+
# 27| 0: [DoubleLiteral] 5.0
193+
# 30| 5: [Class] C
194+
# 30| 1: [Constructor] C
195+
# 30| 5: [BlockStmt] { ... }
196+
# 30| 0: [SuperConstructorInvocationStmt] super(...)
197+
# 30| 1: [ExprStmt] <Expr>;
198+
# 30| 0: [MethodAccess] <obinit>(...)
199+
# 31| 3: [Method] getP0
200+
# 31| 5: [BlockStmt] { ... }
201+
# 31| 0: [ReturnStmt] return ...
202+
# 31| 0: [VarAccess] this.p0
203+
# 31| -1: [ThisAccess] this
204+
# 31| 3: [FieldDeclaration] int p0;
205+
# 31| -1: [TypeAccess] int
206+
# 31| 0: [IntegerLiteral] 1
207+
# 32| 5: [Method] getP1
208+
# 32| 5: [BlockStmt] { ... }
209+
# 32| 0: [ReturnStmt] return ...
210+
# 32| 0: [VarAccess] this.p1
211+
# 32| -1: [ThisAccess] this
212+
# 32| 5: [Method] setP1
213+
#-----| 4: (Parameters)
214+
# 32| 0: [Parameter] <set-?>
215+
# 32| 5: [BlockStmt] { ... }
216+
# 32| 0: [ExprStmt] <Expr>;
217+
# 32| 0: [AssignExpr] ...=...
218+
# 32| 0: [VarAccess] this.p1
219+
# 32| -1: [ThisAccess] this
220+
# 32| 1: [VarAccess] <set-?>
221+
# 32| 5: [FieldDeclaration] int p1;
222+
# 32| -1: [TypeAccess] int
223+
# 32| 0: [IntegerLiteral] 2
224+
# 34| 8: [Method] getP2
225+
# 34| 5: [BlockStmt] { ... }
226+
# 34| 0: [ReturnStmt] return ...
227+
# 34| 0: [IntegerLiteral] 1
228+
# 35| 9: [Method] setP2
229+
#-----| 4: (Parameters)
230+
# 35| 0: [Parameter] value
231+
# 35| 5: [BlockStmt] { ... }
232+
# 35| 0: [ReturnStmt] return ...
233+
# 35| 0: [VarAccess] INSTANCE
234+
# 38| 10: [ExtensionMethod] getP3
235+
#-----| 4: (Parameters)
236+
# 37| 0: [Parameter] <this>
237+
# 38| 5: [BlockStmt] { ... }
238+
# 38| 0: [ReturnStmt] return ...
239+
# 38| 0: [IntegerLiteral] 1
240+
# 39| 11: [ExtensionMethod] setP3
42241
#-----| 4: (Parameters)
43-
# 10| 0: [Parameter] i
44-
# 10| 5: [BlockStmt] { ... }
45-
# 10| 0: [ReturnStmt] return ...
46-
# 10| 0: [DoubleLiteral] 5.0
242+
# 37| 0: [Parameter] <this>
243+
# 39| 1: [Parameter] value
244+
# 39| 5: [BlockStmt] { ... }
245+
# 39| 0: [ReturnStmt] return ...
246+
# 39| 0: [VarAccess] INSTANCE
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
variableInitializerType
22
| reflection.kt:5:9:5:54 | KFunction<Double> ref | file://<external>/KFunction.class:0:0:0:0 | KFunction<Double> | reflection.kt:5:49:5:54 | new Function2<Ccc,Integer,Double>(...) { ... } | file://<external>/Function2.class:0:0:0:0 | Function2<Ccc,Integer,Double> | true |
33
| reflection.kt:5:9:5:54 | KFunction<Double> ref | file://<external>/KFunction.class:0:0:0:0 | KFunction<Double> | reflection.kt:5:49:5:54 | new Function2<Ccc,Integer,Double>(...) { ... } | file://<external>/FunctionReference.class:0:0:0:0 | FunctionReference | true |
4+
| reflection.kt:11:9:11:53 | Getter<C,Integer> x3 | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/Function1.class:0:0:0:0 | Function1<C,Integer> | true |
5+
| reflection.kt:11:9:11:53 | Getter<C,Integer> x3 | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/KProperty$Getter.class:0:0:0:0 | Getter<Integer> | true |
6+
| reflection.kt:12:9:12:44 | KFunction<Integer> x4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Integer> | reflection.kt:12:38:12:44 | new Function1<T,V>(...) { ... } | file://<external>/Function1.class:0:0:0:0 | Function1<T,V> | true |
7+
| reflection.kt:12:9:12:44 | KFunction<Integer> x4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Integer> | reflection.kt:12:38:12:44 | new Function1<T,V>(...) { ... } | file://<external>/FunctionReference.class:0:0:0:0 | FunctionReference | true |
8+
| reflection.kt:18:9:18:60 | Setter<C,Integer> y3 | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/Function2.class:0:0:0:0 | Function2<C,Integer,Unit> | true |
9+
| reflection.kt:18:9:18:60 | Setter<C,Integer> y3 | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/KMutableProperty$Setter.class:0:0:0:0 | Setter<Integer> | true |
10+
| reflection.kt:19:9:19:50 | KFunction<Unit> y4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Unit> | reflection.kt:19:44:19:50 | new Function2<T,V,Unit>(...) { ... } | file://<external>/Function2.class:0:0:0:0 | Function2<T,V,Unit> | true |
11+
| reflection.kt:19:9:19:50 | KFunction<Unit> y4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Unit> | reflection.kt:19:44:19:50 | new Function2<T,V,Unit>(...) { ... } | file://<external>/FunctionReference.class:0:0:0:0 | FunctionReference | true |
12+
| reflection.kt:22:9:22:91 | KProperty2<C,Integer,Integer> prop | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/Function2.class:0:0:0:0 | Function2<C,Integer,Integer> | true |
13+
| reflection.kt:22:9:22:91 | KProperty2<C,Integer,Integer> prop | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/KProperty.class:0:0:0:0 | KProperty<Integer> | true |
414
invocation
515
| reflection.kt:6:21:6:24 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
16+
| reflection.kt:9:26:9:33 | get(...) | file://<external>/KProperty1.class:0:0:0:0 | get |
17+
| reflection.kt:10:29:10:32 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
18+
| reflection.kt:11:48:11:53 | getGetter(...) | file://<external>/KProperty1.class:0:0:0:0 | getGetter |
19+
| reflection.kt:12:38:12:44 | get(...) | file://<external>/KProperty1.class:0:0:0:0 | get |
20+
| reflection.kt:16:27:16:37 | set(...) | file://<external>/KMutableProperty1.class:0:0:0:0 | set |
21+
| reflection.kt:17:29:17:32 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
22+
| reflection.kt:18:55:18:60 | getSetter(...) | file://<external>/KMutableProperty1.class:0:0:0:0 | getSetter |
23+
| reflection.kt:19:44:19:50 | set(...) | file://<external>/KMutableProperty1.class:0:0:0:0 | set |
24+
| reflection.kt:22:31:22:37 | getMembers(...) | file://<external>/KClass.class:0:0:0:0 | getMembers |
25+
| reflection.kt:22:51:22:54 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
26+
| reflection.kt:23:23:23:33 | get(...) | file://<external>/KProperty2.class:0:0:0:0 | get |
27+
| reflection.kt:48:30:48:39 | get(...) | file://<external>/KProperty1.class:0:0:0:0 | get |
28+
| reflection.kt:49:30:49:34 | get(...) | file://<external>/KProperty0.class:0:0:0:0 | get |
Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
1-
import kotlin.reflect.KFunction2
1+
import kotlin.reflect.*
22

33
class Reflection {
4-
fun fn(boo: Boolean) {
4+
fun fn() {
55
val ref: KFunction2<Ccc, Int, Double> = Ccc::m
66
println(ref.name)
7+
8+
val x0: KProperty1<C, Int> = C::p0
9+
val x1: Int = x0.get(C())
10+
val x2: String = x0.name
11+
val x3: KProperty1.Getter<C, Int> = x0.getter
12+
val x4: KFunction1<C, Int> = x0::get
13+
val x5: KProperty0<Int> = C()::p0
14+
15+
val y0: KMutableProperty1<C, Int> = C::p1
16+
val y1: Unit = y0.set(C(), 5)
17+
val y2: String = y0.name
18+
val y3: KMutableProperty1.Setter<C, Int> = y0.setter
19+
val y4: KFunction2<C, Int, Unit> = y0::set
20+
val y5: KMutableProperty0<Int> = C()::p1
21+
22+
val prop = (C::class).members.single { it.name == "p3" } as KProperty2<C, Int, Int>
23+
val z0 = prop.get(C(), 5)
724
}
825

926
class Ccc {
1027
fun m(i:Int):Double = 5.0
1128
}
12-
}
29+
30+
class C {
31+
val p0: Int = 1
32+
var p1: Int = 2
33+
var p2: Int
34+
get() = 1
35+
set(value) = Unit
36+
37+
var Int.p3: Int // this can't be referenced through a property reference
38+
get() = 1
39+
set(value) = Unit
40+
}
41+
}
42+
43+
44+
val String.lastChar: Char
45+
get() = this[length - 1]
46+
47+
fun fn2() {
48+
println(String::lastChar.get("abc"))
49+
println("abcd"::lastChar.get())
50+
}

java/ql/test/kotlin/library-tests/reflection/reflection.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ query predicate variableInitializerType(
2626
LocalVariableDecl decl, RefType t1, RefType t2, RefType t3, boolean compatible
2727
) {
2828
decl.getType() = t1 and
29+
t1.getPackage().getName() = "kotlin.reflect" and
2930
decl.getInitializer().getType() = t2 and
3031
t2.extendsOrImplements(t3) and
3132
(

0 commit comments

Comments
 (0)