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

Skip to content

Commit fcb3341

Browse files
atorralbaigfoo
authored andcommitted
Create ExtensionMethodAccess class
1 parent 270beec commit fcb3341

8 files changed

Lines changed: 83 additions & 22 deletions

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,23 @@ private module ControlFlowGraphImpl {
550550
or
551551
index = 0 and result = this.(RValue).getQualifier() and not result instanceof TypeAccess
552552
or
553-
exists(Call e | e = this |
553+
exists(Call e | e = this and not e instanceof ExtensionMethodAccess |
554554
index = -1 and result = e.getQualifier() and not result instanceof TypeAccess
555555
or
556556
result = e.getArgument(index)
557557
)
558558
or
559+
exists(ExtensionMethodAccess e | e = this |
560+
// the actual qualifier of the expression method access
561+
index = -1 and result.(Expr).isNthChildOf(this, index)
562+
or
563+
// the extension receiver
564+
index = 0 and result = e.getQualifier()
565+
or
566+
// the arguments
567+
result = e.getArgument(index)
568+
)
569+
or
559570
exists(StringTemplateExpr e | e = this | result = e.getComponent(index))
560571
or
561572
index = 0 and result = this.(ClassExpr).getExpr()

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,22 @@ class MethodAccess extends Expr, Call, @methodaccess {
19571957
override string getAPrimaryQlClass() { result = "MethodAccess" }
19581958
}
19591959

1960+
/**
1961+
* An invocation of a Kotlin `ExtensionMethod`.
1962+
*
1963+
* The syntactical qualifier of an extension method is its receiver (arg 0),
1964+
* whereas the actual arguments begin at index 1.
1965+
*/
1966+
class ExtensionMethodAccess extends MethodAccess {
1967+
ExtensionMethodAccess() { this.getMethod() instanceof ExtensionMethod }
1968+
1969+
override Expr getQualifier() { result.isNthChildOf(this, 0) }
1970+
1971+
override Expr getAnArgument() { result.getIndex() >= 1 and result.getParent() = this }
1972+
1973+
override Expr getArgument(int index) { result = super.getArgument(index + 1) }
1974+
}
1975+
19601976
/** A type access is a (possibly qualified) reference to a type. */
19611977
class TypeAccess extends Expr, Annotatable, @typeaccess {
19621978
/** Gets the qualifier of this type access, if any. */
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11

22
class SomeClass {
3-
fun someClassMethod() {}
3+
fun someClassMethod(p1: String) {}
44
}
55
class AnotherClass {
6-
fun anotherClassMethod() {}
6+
fun anotherClassMethod(p1: String) {}
77
}
88

9-
fun SomeClass.someFun() {}
10-
fun AnotherClass.anotherFun() {}
9+
fun SomeClass.someFun(p1: String) {}
10+
fun AnotherClass.anotherFun(p1: String) {}
1111

12-
fun SomeClass.bothFun() {}
13-
fun AnotherClass.bothFun() {}
12+
fun SomeClass.bothFun(p1: String) {}
13+
fun AnotherClass.bothFun(p1: String) {}
1414

15-
fun SomeClass.bothFunDiffTypes(): Int { return 5 }
16-
fun AnotherClass.bothFunDiffTypes(): String { return "Foo" }
15+
fun SomeClass.bothFunDiffTypes(p1: Int): Int { return 5 }
16+
fun AnotherClass.bothFunDiffTypes(p1: String): String { return "Foo" }
17+
18+
fun String.bar(p1: String): String { return "Bar" }
19+
20+
fun foo() {
21+
SomeClass().someClassMethod("foo")
22+
SomeClass().someFun("foo")
23+
SomeClass().bothFun("foo")
24+
SomeClass().bothFunDiffTypes(1)
25+
AnotherClass().anotherClassMethod("foo")
26+
AnotherClass().anotherFun("foo")
27+
AnotherClass().bothFun("foo")
28+
AnotherClass().bothFunDiffTypes("foo")
29+
"someString".bar("foo")
30+
fun String.baz(p1: String): String { return "Baz" }
31+
"someString".baz("bazParam")
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
| extensions.kt:21:17:21:38 | someClassMethod(...) | extensions.kt:21:5:21:15 | new SomeClass(...) | extensions.kt:21:34:21:36 | foo |
2+
| extensions.kt:22:17:22:30 | someFun(...) | extensions.kt:22:5:22:15 | new SomeClass(...) | extensions.kt:22:26:22:28 | foo |
3+
| extensions.kt:23:17:23:30 | bothFun(...) | extensions.kt:23:5:23:15 | new SomeClass(...) | extensions.kt:23:26:23:28 | foo |
4+
| extensions.kt:24:17:24:35 | bothFunDiffTypes(...) | extensions.kt:24:5:24:15 | new SomeClass(...) | extensions.kt:24:34:24:34 | 1 |
5+
| extensions.kt:25:20:25:44 | anotherClassMethod(...) | extensions.kt:25:5:25:18 | new AnotherClass(...) | extensions.kt:25:40:25:42 | foo |
6+
| extensions.kt:26:20:26:36 | anotherFun(...) | extensions.kt:26:5:26:18 | new AnotherClass(...) | extensions.kt:26:32:26:34 | foo |
7+
| extensions.kt:27:20:27:33 | bothFun(...) | extensions.kt:27:5:27:18 | new AnotherClass(...) | extensions.kt:27:29:27:31 | foo |
8+
| extensions.kt:28:20:28:42 | bothFunDiffTypes(...) | extensions.kt:28:5:28:18 | new AnotherClass(...) | extensions.kt:28:38:28:40 | foo |
9+
| extensions.kt:29:18:29:27 | bar(...) | extensions.kt:29:6:29:15 | someString | extensions.kt:29:23:29:25 | foo |
10+
| extensions.kt:31:18:31:32 | baz(...) | extensions.kt:31:6:31:15 | someString | extensions.kt:31:23:31:30 | bazParam |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import java
2+
3+
// For ExtensionMethodAccess:
4+
// * qualifier is the child with index 0 instead of -1
5+
// * arguments are children starting from index 1 instead of 0
6+
from MethodAccess ma
7+
select ma, ma.getQualifier(), ma.getAnArgument()
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
| extensions.kt:3:5:3:28 | someClassMethod | file://:0:0:0:0 | void |
2-
| extensions.kt:6:5:6:31 | anotherClassMethod | file://:0:0:0:0 | void |
3-
| extensions.kt:9:1:9:26 | someFun | file://:0:0:0:0 | void |
4-
| extensions.kt:10:1:10:32 | anotherFun | file://:0:0:0:0 | void |
5-
| extensions.kt:12:1:12:26 | bothFun | file://:0:0:0:0 | void |
6-
| extensions.kt:13:1:13:29 | bothFun | file://:0:0:0:0 | void |
7-
| extensions.kt:15:1:15:50 | bothFunDiffTypes | file://:0:0:0:0 | int |
8-
| extensions.kt:16:1:16:60 | bothFunDiffTypes | file://<external>/String.class:0:0:0:0 | String |
1+
| extensions.kt:3:5:3:38 | someClassMethod | file://:0:0:0:0 | void |
2+
| extensions.kt:6:5:6:41 | anotherClassMethod | file://:0:0:0:0 | void |
3+
| extensions.kt:9:1:9:36 | someFun | file://:0:0:0:0 | void |
4+
| extensions.kt:10:1:10:42 | anotherFun | file://:0:0:0:0 | void |
5+
| extensions.kt:12:1:12:36 | bothFun | file://:0:0:0:0 | void |
6+
| extensions.kt:13:1:13:39 | bothFun | file://:0:0:0:0 | void |
7+
| extensions.kt:15:1:15:57 | bothFunDiffTypes | file://:0:0:0:0 | int |
8+
| extensions.kt:16:1:16:70 | bothFunDiffTypes | file://<external>/String.class:0:0:0:0 | String |
9+
| extensions.kt:18:1:18:51 | bar | file://<external>/String.class:0:0:0:0 | String |
10+
| extensions.kt:20:1:32:1 | foo | file://:0:0:0:0 | void |
11+
| extensions.kt:30:5:30:55 | baz | file://<external>/String.class:0:0:0:0 | String |
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
| test.kt:2:17:2:31 | startsWith(...) | test.kt:2:17:2:31 | StringsKt | test.kt:2:13:2:15 | url |
2-
| test.kt:2:17:2:31 | startsWith(...) | test.kt:2:17:2:31 | StringsKt | test.kt:2:29:2:29 | 1 |
3-
| test.kt:3:17:3:33 | removePrefix(...) | test.kt:3:17:3:33 | StringsKt | test.kt:3:13:3:15 | url |
4-
| test.kt:3:17:3:33 | removePrefix(...) | test.kt:3:17:3:33 | StringsKt | test.kt:3:31:3:31 | 2 |
1+
| test.kt:2:17:2:31 | startsWith(...) | test.kt:2:13:2:15 | url | test.kt:2:29:2:29 | 1 |
2+
| test.kt:3:17:3:33 | removePrefix(...) | test.kt:3:13:3:15 | url | test.kt:3:31:3:31 | 2 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
| test.kt:4:17:4:30 | Class<> | TypeAccess | forName |
2-
| test.kt:7:18:7:44 | StringsKt | TypeAccess | format |
2+
| test.kt:7:11:7:16 | Companion | VarAccess | format |

0 commit comments

Comments
 (0)