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

Skip to content

Commit 3e1d4a7

Browse files
tamasvajksmowton
authored andcommitted
Add more annotation tests, add KClass to Class substitution
1 parent f465dd5 commit 3e1d4a7

5 files changed

Lines changed: 76 additions & 18 deletions

File tree

java/kotlin-extractor/src/main/kotlin/utils/ClassNames.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.declarations.*
1414
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
1515
import org.jetbrains.kotlin.ir.util.parentClassOrNull
1616
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
17+
import org.jetbrains.kotlin.name.ClassId
1718

1819
// Adapted from Kotlin's interpreter/Utils.kt function 'internalName'
1920
// Translates class names into their JLS section 13.1 binary name,
@@ -119,4 +120,10 @@ fun getContainingClassOrSelf(decl: IrDeclaration): IrClass? {
119120
}
120121

121122
fun getJavaEquivalentClassId(c: IrClass) =
122-
c.fqNameWhenAvailable?.toUnsafe()?.let { JavaToKotlinClassMap.mapKotlinToJava(it) }
123+
c.fqNameWhenAvailable?.toUnsafe()?.let {
124+
if (it.asString() == "kotlin.reflect.KClass") {
125+
ClassId.fromString("java.lang.Class")
126+
} else {
127+
JavaToKotlinClassMap.mapKotlinToJava(it)
128+
}
129+
}

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Annot1j.java:
2222
def.kt:
2323
# 0| [CompilationUnit] def
2424
# 0| 1: [Class] DefKt
25-
# 23| 1: [Method] fn
25+
# 25| 1: [Method] fn
2626
#-----| 4: (Parameters)
27-
# 23| 0: [Parameter] a
28-
# 23| 5: [BlockStmt] { ... }
29-
# 24| 0: [ExprStmt] <Expr>;
30-
# 24| 0: [MethodAccess] println(...)
31-
# 24| -1: [TypeAccess] ConsoleKt
32-
# 24| 0: [MethodAccess] a(...)
33-
# 24| -1: [VarAccess] a
27+
# 25| 0: [Parameter] a
28+
# 25| 5: [BlockStmt] { ... }
29+
# 26| 0: [ExprStmt] <Expr>;
30+
# 26| 0: [MethodAccess] println(...)
31+
# 26| -1: [TypeAccess] ConsoleKt
32+
# 26| 0: [MethodAccess] a(...)
33+
# 26| -1: [VarAccess] a
3434
# 3| 2: [Interface] Annot0k
3535
# 3| 1: [Method] a
3636
# 5| 3: [Interface] Annot1k
@@ -72,10 +72,21 @@ def.kt:
7272
# 0| 1: [VarAccess] A
7373
# 0| 1: [Annotation] Annot0k
7474
# 0| 1: [IntegerLiteral] 1
75-
# 21| 1: [Constructor] Z
75+
# 21| 3: [Annotation] Annot0j
76+
# 0| 1: [IntegerLiteral] 1
77+
# 22| 4: [Annotation] Annot1j
78+
# 0| 1: [ErrorExpr] <error expr>
79+
# 0| 1: [ErrorExpr] <error expr>
80+
# 0| 1: [ErrorExpr] <error expr>
81+
# 0| 1: [VarAccess] B
82+
# 0| 1: [ArrayInit] {...}
83+
# 0| 1: [VarAccess] C
84+
# 0| 1: [VarAccess] A
85+
# 0| 1: [ErrorExpr] <error expr>
86+
# 23| 1: [Constructor] Z
7687
# 19| 5: [BlockStmt] { ... }
7788
# 19| 0: [SuperConstructorInvocationStmt] super(...)
78-
# 21| 1: [BlockStmt] { ... }
89+
# 23| 1: [BlockStmt] { ... }
7990
use.java:
8091
# 0| [CompilationUnit] use
8192
# 1| 1: [Class] use
@@ -97,7 +108,7 @@ use.java:
97108
# 6| 5: [BlockStmt] { ... }
98109
# 7| 0: [ReturnStmt] return ...
99110
# 7| 0: [NullLiteral] null
100-
# 12| 4: [Class] Z
111+
# 14| 4: [Class] Z
101112
#-----| -3: (Annotations)
102113
# 10| 1: [Annotation] Annot0j
103114
# 10| 1: [IntegerLiteral] 1
@@ -115,3 +126,19 @@ use.java:
115126
# 11| -1: [TypeAccess] Y
116127
# 11| 6: [Annotation] Annot0j
117128
# 11| 1: [IntegerLiteral] 2
129+
# 12| 3: [Annotation] Annot0k
130+
# 12| 1: [IntegerLiteral] 1
131+
# 13| 4: [Annotation] Annot1k
132+
# 13| 1: [IntegerLiteral] 1
133+
# 13| 2: [StringLiteral] "ac"
134+
# 13| 3: [TypeLiteral] X.class
135+
# 13| 0: [TypeAccess] X
136+
# 13| 4: [VarAccess] Y.B
137+
# 13| -1: [TypeAccess] Y
138+
# 13| 5: [ArrayInit] {...}
139+
# 13| 3: [VarAccess] Y.C
140+
# 13| -1: [TypeAccess] Y
141+
# 13| 4: [VarAccess] Y.A
142+
# 13| -1: [TypeAccess] Y
143+
# 13| 6: [Annotation] Annot0k
144+
# 13| 1: [IntegerLiteral] 2

java/ql/test/kotlin/library-tests/annotation_classes/classes.expected

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ annotationDeclarations
1515
| def.kt:5:1:12:1 | Annot1k | def.kt:11:5:11:31 | f |
1616
annotations
1717
| def.kt:0:0:0:0 | Annot0k | def.kt:20:1:20:40 | Annot1k | def.kt:3:1:3:60 | Annot0k |
18-
| def.kt:19:1:19:17 | Annot0k | def.kt:19:1:21:10 | Z | def.kt:3:1:3:60 | Annot0k |
19-
| def.kt:20:1:20:40 | Annot1k | def.kt:19:1:21:10 | Z | def.kt:5:1:12:1 | Annot1k |
20-
| use.java:10:5:10:21 | Annot0j | use.java:12:18:12:18 | Z | Annot0j.java:1:19:1:25 | Annot0j |
21-
| use.java:11:5:11:90 | Annot1j | use.java:12:18:12:18 | Z | Annot1j.java:1:19:1:25 | Annot1j |
18+
| def.kt:19:1:19:17 | Annot0k | def.kt:19:1:23:10 | Z | def.kt:3:1:3:60 | Annot0k |
19+
| def.kt:20:1:20:40 | Annot1k | def.kt:19:1:23:10 | Z | def.kt:5:1:12:1 | Annot1k |
20+
| def.kt:21:1:21:17 | Annot0j | def.kt:19:1:23:10 | Z | Annot0j.java:1:19:1:25 | Annot0j |
21+
| def.kt:22:1:22:40 | Annot1j | def.kt:19:1:23:10 | Z | Annot1j.java:1:19:1:25 | Annot1j |
22+
| use.java:10:5:10:21 | Annot0j | use.java:14:18:14:18 | Z | Annot0j.java:1:19:1:25 | Annot0j |
23+
| use.java:11:5:11:90 | Annot1j | use.java:14:18:14:18 | Z | Annot1j.java:1:19:1:25 | Annot1j |
2224
| use.java:11:73:11:89 | Annot0j | use.java:11:5:11:90 | Annot1j | Annot0j.java:1:19:1:25 | Annot0j |
25+
| use.java:12:5:12:19 | Annot0k | use.java:14:18:14:18 | Z | def.kt:3:1:3:60 | Annot0k |
26+
| use.java:13:5:13:88 | Annot1k | use.java:14:18:14:18 | Z | def.kt:5:1:12:1 | Annot1k |
27+
| use.java:13:73:13:87 | Annot0k | use.java:13:5:13:88 | Annot1k | def.kt:3:1:3:60 | Annot0k |
2328
annotationValues
2429
| def.kt:0:0:0:0 | Annot0k | def.kt:0:0:0:0 | 1 |
2530
| def.kt:19:1:19:17 | Annot0k | def.kt:0:0:0:0 | 1 |
@@ -29,6 +34,13 @@ annotationValues
2934
| def.kt:20:1:20:40 | Annot1k | def.kt:0:0:0:0 | X.class |
3035
| def.kt:20:1:20:40 | Annot1k | def.kt:0:0:0:0 | ab |
3136
| def.kt:20:1:20:40 | Annot1k | def.kt:0:0:0:0 | {...} |
37+
| def.kt:21:1:21:17 | Annot0j | def.kt:0:0:0:0 | 1 |
38+
| def.kt:22:1:22:40 | Annot1j | def.kt:0:0:0:0 | <error expr> |
39+
| def.kt:22:1:22:40 | Annot1j | def.kt:0:0:0:0 | <error expr> |
40+
| def.kt:22:1:22:40 | Annot1j | def.kt:0:0:0:0 | <error expr> |
41+
| def.kt:22:1:22:40 | Annot1j | def.kt:0:0:0:0 | <error expr> |
42+
| def.kt:22:1:22:40 | Annot1j | def.kt:0:0:0:0 | B |
43+
| def.kt:22:1:22:40 | Annot1j | def.kt:0:0:0:0 | {...} |
3244
| use.java:10:5:10:21 | Annot0j | use.java:10:20:10:20 | 1 |
3345
| use.java:11:5:11:90 | Annot1j | use.java:11:18:11:18 | 1 |
3446
| use.java:11:5:11:90 | Annot1j | use.java:11:25:11:28 | "ac" |
@@ -37,6 +49,14 @@ annotationValues
3749
| use.java:11:5:11:90 | Annot1j | use.java:11:57:11:66 | {...} |
3850
| use.java:11:5:11:90 | Annot1j | use.java:11:73:11:89 | Annot0j |
3951
| use.java:11:73:11:89 | Annot0j | use.java:11:88:11:88 | 2 |
52+
| use.java:12:5:12:19 | Annot0k | use.java:12:18:12:18 | 1 |
53+
| use.java:13:5:13:88 | Annot1k | use.java:13:18:13:18 | 1 |
54+
| use.java:13:5:13:88 | Annot1k | use.java:13:25:13:28 | "ac" |
55+
| use.java:13:5:13:88 | Annot1k | use.java:13:35:13:41 | X.class |
56+
| use.java:13:5:13:88 | Annot1k | use.java:13:48:13:50 | Y.B |
57+
| use.java:13:5:13:88 | Annot1k | use.java:13:57:13:66 | {...} |
58+
| use.java:13:5:13:88 | Annot1k | use.java:13:73:13:87 | Annot0k |
59+
| use.java:13:73:13:87 | Annot0k | use.java:13:86:13:86 | 2 |
4060
#select
4161
| Annot0j.java:1:19:1:25 | Annot0j | Interface |
4262
| Annot1j.java:1:19:1:25 | Annot1j | Interface |
@@ -45,6 +65,6 @@ annotationValues
4565
| def.kt:5:1:12:1 | Annot1k | Interface |
4666
| def.kt:14:1:14:10 | X | Class |
4767
| def.kt:15:1:17:1 | Y | Class |
48-
| def.kt:19:1:21:10 | Z | Class |
68+
| def.kt:19:1:23:10 | Z | Class |
4969
| use.java:1:14:1:16 | use | Class |
50-
| use.java:12:18:12:18 | Z | Class |
70+
| use.java:14:18:14:18 | Z | Class |

java/ql/test/kotlin/library-tests/annotation_classes/def.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ enum class Y {
1818

1919
@Annot0k(abc = 1)
2020
@Annot1k(d = Y.B, e = arrayOf(Y.C, Y.A))
21+
@Annot0j(abc = 1)
22+
@Annot1j(d = Y.B, e = arrayOf(Y.C, Y.A))
2123
class Z {}
2224

2325
fun fn(a: Annot0k) {

java/ql/test/kotlin/library-tests/annotation_classes/use.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public Class<? extends java.lang.annotation.Annotation> annotationType() {
99

1010
@Annot0j(abc = 1)
1111
@Annot1j(a = 1, b = "ac", c = X.class, d = Y.B, e = {Y.C, Y.A}, f = @Annot0j(abc = 2))
12+
@Annot0k(a = 1)
13+
@Annot1k(a = 1, b = "ac", c = X.class, d = Y.B, e = {Y.C, Y.A}, f = @Annot0k(a = 2))
1214
public class Z { }
1315
}

0 commit comments

Comments
 (0)