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

Skip to content

Commit 42803a1

Browse files
tamasvajkigfoo
authored andcommitted
WIP: add test for reflective calls
1 parent c4c2545 commit 42803a1

6 files changed

Lines changed: 105 additions & 1 deletion

File tree

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.descriptors.java.JavaVisibilities
1717
import org.jetbrains.kotlin.ir.IrElement
1818
import org.jetbrains.kotlin.ir.IrStatement
1919
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
20+
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
2021
import org.jetbrains.kotlin.ir.declarations.*
2122
import org.jetbrains.kotlin.ir.expressions.*
2223
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
@@ -1018,7 +1019,7 @@ open class KotlinFileExtractor(
10181019
typeArguments: List<IrType> = listOf(),
10191020
extractClassTypeArguments: Boolean = false) {
10201021

1021-
val callTarget = syntacticCallTarget.target
1022+
val callTarget = syntacticCallTarget.target.realOverrideTarget
10221023
val id = tw.getFreshIdLabel<DbMethodaccess>()
10231024
val type = useType(callsite.type)
10241025
val locId = tw.getLocation(callsite)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
reflection.kt:
2+
# 0| [CompilationUnit] reflection
3+
# 3| 1: [Class] Reflection
4+
# 3| 1: [Constructor] Reflection
5+
# 3| 5: [BlockStmt] { ... }
6+
# 3| 0: [SuperConstructorInvocationStmt] super(...)
7+
# 3| 1: [ExprStmt] <Expr>;
8+
# 3| 0: [MethodAccess] <obinit>(...)
9+
# 4| 3: [Method] fn
10+
#-----| 4: (Parameters)
11+
# 4| 0: [Parameter] boo
12+
# 4| 5: [BlockStmt] { ... }
13+
# 5| 0: [LocalVariableDeclStmt] var ...;
14+
# 5| 1: [LocalVariableDeclExpr] ref
15+
# 5| 0: [MemberRefExpr] ...::...
16+
# 5| -4: [AnonymousClass] new Function2<Ccc,Integer,Double>(...) { ... }
17+
# 5| 1: [Constructor]
18+
# 5| 5: [BlockStmt] { ... }
19+
# 5| 0: [SuperConstructorInvocationStmt] super(...)
20+
# 5| 1: [Method] invoke
21+
#-----| 4: (Parameters)
22+
# 5| 0: [Parameter] a0
23+
# 5| 1: [Parameter] a1
24+
# 5| 5: [BlockStmt] { ... }
25+
# 5| 0: [ReturnStmt] return ...
26+
# 5| 0: [MethodAccess] m(...)
27+
# 5| -1: [VarAccess] a0
28+
# 5| 0: [VarAccess] a1
29+
# 5| -3: [TypeAccess] Function2<Ccc,Integer,Double>
30+
# 6| 1: [ExprStmt] <Expr>;
31+
# 6| 0: [MethodAccess] println(...)
32+
# 6| -1: [TypeAccess] ConsoleKt
33+
# 6| 0: [MethodAccess] getName(...)
34+
# 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
42+
#-----| 4: (Parameters)
43+
# 10| 0: [Parameter] i
44+
# 10| 5: [BlockStmt] { ... }
45+
# 10| 0: [ReturnStmt] return ...
46+
# 10| 0: [DoubleLiteral] 5.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variableInitializerType
2+
| 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> | false |
3+
| 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>/Object.class:0:0:0:0 | Object | false |
4+
invocation
5+
| reflection.kt:6:21:6:24 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import kotlin.reflect.KFunction2
2+
3+
class Reflection {
4+
fun fn(boo: Boolean) {
5+
val ref: KFunction2<Ccc, Int, Double> = Ccc::m
6+
println(ref.name)
7+
}
8+
9+
class Ccc {
10+
fun m(i:Int):Double = 5.0
11+
}
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java
2+
3+
// Stop external filepaths from appearing in the results
4+
class ClassOrInterfaceLocation extends ClassOrInterface {
5+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
6+
exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
7+
if exists(this.getFile().getRelativePath())
8+
then path = fullPath
9+
else path = fullPath.regexpReplaceAll(".*/", "<external>/")
10+
)
11+
}
12+
}
13+
14+
class CallableLocation extends Callable {
15+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
16+
exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
17+
if exists(this.getFile().getRelativePath())
18+
then path = fullPath
19+
else path = fullPath.regexpReplaceAll(".*/", "<external>/")
20+
)
21+
}
22+
}
23+
24+
query predicate variableInitializerType(
25+
LocalVariableDecl decl, RefType t1, RefType t2, RefType t3, boolean implements
26+
) {
27+
decl.getType() = t1 and
28+
decl.getInitializer().getType() = t2 and
29+
t2.extendsOrImplements(t3) and
30+
(
31+
implements = true and t2.extendsOrImplements+(t1)
32+
or
33+
implements = false and not t2.extendsOrImplements+(t1)
34+
)
35+
}
36+
37+
query predicate invocation(Call c, Callable callee) {
38+
c.getCallee() = callee and callee.getDeclaringType().getPackage().getName() = "kotlin.reflect"
39+
}

0 commit comments

Comments
 (0)