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

Skip to content

Commit cd5555a

Browse files
tamasvajkigfoo
authored andcommitted
Extract companion objects from interfaces
1 parent 53f40a3 commit cd5555a

8 files changed

Lines changed: 33 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ open class KotlinFileExtractor(
386386
tw.writeHasLocation(instance.id, innerLocId)
387387
addModifiers(instance.id, "public", "static", "final")
388388
@Suppress("UNCHECKED_CAST")
389-
tw.writeClass_companion_object(parentId as Label<DbClass>, instance.id, innerId as Label<DbClass>)
389+
tw.writeType_companion_object(parentId, instance.id, innerId as Label<DbClass>)
390390
}
391391
}
392392

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ class_object(
348348
unique int instance: @field ref
349349
);
350350

351-
class_companion_object(
352-
unique int id: @class ref,
351+
type_companion_object(
352+
unique int id: @classorinterface ref,
353353
unique int instance: @field ref,
354354
unique int companion_object: @class ref
355355
);

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,6 @@ class Class extends ClassOrInterface, @class {
695695
)
696696
}
697697

698-
/** Get the companion object of this class, if any. */
699-
ClassCompanionObject getCompanionObject() {
700-
class_companion_object(this, _, result)
701-
}
702-
703698
override string getAPrimaryQlClass() { result = "Class" }
704699
}
705700

@@ -718,12 +713,12 @@ class ClassObject extends Class {
718713
/** A Kotlin `companion object`. */
719714
class ClassCompanionObject extends Class {
720715
ClassCompanionObject() {
721-
class_companion_object(_, _, this)
716+
type_companion_object(_, _, this)
722717
}
723718

724719
/** Gets the instance variable that implements this `companion object`. */
725720
Field getInstance() {
726-
class_companion_object(_, result, this)
721+
type_companion_object(_, result, this)
727722
}
728723
}
729724

@@ -964,6 +959,9 @@ class ClassOrInterface extends RefType, @classorinterface {
964959

965960
/** Holds if this class or interface is explicitly or implicitly a sealed class (Java 17 feature). */
966961
predicate isSealed() { exists(this.getAPermittedSubtype()) }
962+
963+
/** Get the companion object of this class or interface, if any. */
964+
ClassCompanionObject getCompanionObject() { type_companion_object(this, _, result) }
967965
}
968966

969967
private string getAPublicObjectMethodSignature() {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
| companion_objects.kt:3:5:5:5 | MyClassCompanion | companion_objects.kt:9:5:9:11 | MyClassCompanion |
1+
| companion_objects.kt:3:5:5:5 | MyClassCompanion | companion_objects.kt:23:5:23:11 | MyClassCompanion |
2+
| companion_objects.kt:10:5:12:5 | MyInterfaceCompanion | companion_objects.kt:25:5:25:15 | MyInterfaceCompanion |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
| companion_objects.kt:1:1:6:1 | MyClass | companion_objects.kt:3:5:5:5 | MyClassCompanion | companion_objects.kt:3:5:5:5 | MyClassCompanion | final,public,static |
2+
| companion_objects.kt:8:1:13:1 | MyInterface | companion_objects.kt:10:5:12:5 | MyInterfaceCompanion | companion_objects.kt:10:5:12:5 | MyInterfaceCompanion | final,public,static |

java/ql/test/kotlin/library-tests/companion_objects/companion_objects.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,24 @@ class MyClass {
55
}
66
}
77

8+
interface MyInterface {
9+
fun funInInterface()
10+
companion object MyInterfaceCompanion {
11+
fun funInCompanion() {}
12+
}
13+
}
14+
15+
class Imp : MyInterface {
16+
override fun funInInterface() {
17+
TODO("Not yet implemented")
18+
}
19+
20+
}
21+
822
fun user() {
923
MyClass.funInCompanion()
1024
MyClass().funInClass()
25+
MyInterface.funInCompanion()
26+
Imp().funInInterface()
1127
}
1228

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import java
22

3-
from Class c, ClassCompanionObject cco, Field f
3+
from ClassOrInterface c, ClassCompanionObject cco, Field f
44
where c.fromSource()
55
and cco = c.getCompanionObject()
66
and f = cco.getInstance()
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
| companion_objects.kt:9:5:9:11 | MyClassCompanion | VarAccess | companion_objects.kt:4:9:4:31 | funInCompanion |
2-
| companion_objects.kt:10:5:10:13 | new MyClass(...) | ClassInstanceExpr | companion_objects.kt:2:5:2:23 | funInClass |
1+
| companion_objects.kt:17:9:17:35 | StandardKt | TypeAccess | file:///!unknown-binary-location/kotlin/StandardKt.class:0:0:0:0 | TODO |
2+
| companion_objects.kt:23:5:23:11 | MyClassCompanion | VarAccess | companion_objects.kt:4:9:4:31 | funInCompanion |
3+
| companion_objects.kt:24:5:24:13 | new MyClass(...) | ClassInstanceExpr | companion_objects.kt:2:5:2:23 | funInClass |
4+
| companion_objects.kt:25:5:25:15 | MyInterfaceCompanion | VarAccess | companion_objects.kt:11:9:11:31 | funInCompanion |
5+
| companion_objects.kt:26:5:26:9 | new Imp(...) | ClassInstanceExpr | companion_objects.kt:16:14:18:5 | funInInterface |

0 commit comments

Comments
 (0)