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

Skip to content

Commit 06133e7

Browse files
committed
Handle generic annotations
1 parent 14a47f6 commit 06133e7

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ open class KotlinFileExtractor(
470470
idx: Int,
471471
contextLabel: String? = null
472472
): Label<out DbExpr> {
473-
val t = useType(constructorCall.type)
473+
// Erase the type here because the JVM lowering erases the annotation type, and so the Java extractor will see it in erased form.
474+
val t = useType(erase(constructorCall.type))
474475
val annotationContextLabel = contextLabel ?: "{${t.javaResult.id}}"
475476
val id = tw.getLabelFor<DbDeclannotation>("@\"annotation;{$parent};$annotationContextLabel\"")
476477
tw.writeExprs_declannotation(id, t.javaResult.id, parent, idx)
@@ -485,8 +486,9 @@ open class KotlinFileExtractor(
485486
.filterIsInstance<IrProperty>()
486487
.first { it.name == param.name }
487488
val v = constructorCall.getValueArgument(i) ?: param.defaultValue?.expression
488-
val getterId = useFunction<DbMethod>(prop.getter!!)
489-
val exprId = extractAnnotationValueExpression(v, id, i, "{${getterId}}")
489+
val getter = prop.getter!!
490+
val getterId = useFunction<DbMethod>(getter)
491+
val exprId = extractAnnotationValueExpression(v, id, i, "{${getterId}}", getter.returnType)
490492
if (exprId != null) {
491493
tw.writeAnnotValue(id, getterId, exprId)
492494
}
@@ -498,7 +500,8 @@ open class KotlinFileExtractor(
498500
v: IrExpression?,
499501
parent: Label<out DbExprparent>,
500502
idx: Int,
501-
contextLabel: String): Label<out DbExpr>? {
503+
contextLabel: String,
504+
contextType: IrType?): Label<out DbExpr>? {
502505

503506
fun exprId() = tw.getLabelFor<DbExpr>("@\"annotationExpr;{$parent};$idx\"")
504507

@@ -519,7 +522,10 @@ open class KotlinFileExtractor(
519522
}
520523
is IrVararg -> {
521524
tw.getLabelFor<DbArrayinit>("@\"annotationarray;{${parent}};$contextLabel\"").also { arrayId ->
522-
val type = useType(kClassToJavaClass(v.type))
525+
// Use the context type (i.e., the type the annotation expects, not the actual type of the array)
526+
// because the Java extractor fills in array types using the same technique. These should only
527+
// differ for generic annotations.
528+
val type = useType(kClassToJavaClass(contextType!!))
523529
tw.writeExprs_arrayinit(arrayId, type.javaResult.id, parent, idx)
524530
tw.writeExprsKotlinType(arrayId, type.kotlinResult.id)
525531
tw.writeHasLocation(arrayId, tw.getLocation(v))
@@ -533,7 +539,7 @@ open class KotlinFileExtractor(
533539
null
534540
}
535541
}
536-
extractAnnotationValueExpression(argExpr, arrayId, index, "child;$index")
542+
extractAnnotationValueExpression(argExpr, arrayId, index, "child;$index", null)
537543
} }
538544
}
539545
}

java/ql/integration-tests/all-platforms/kotlin/annotation-id-consistency/PrintAst.expected

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ test.kt:
3131
# 5| 5: [Method] t
3232
# 7| 3: [Interface] Ann3
3333
# 7| 1: [Method] a
34-
# 9| 4: [Class] Annotated
34+
# 9| 4: [GenericType,Interface,ParameterizedType] GenericAnnotation
35+
#-----| -2: (Generic Parameters)
36+
# 9| 0: [TypeVariable] T
37+
# 9| 1: [Method] x
38+
# 9| 2: [Method] y
39+
# 11| 6: [Class] Annotated
3540
#-----| -3: (Annotations)
36-
# 9| 2: [Annotation] Ann1
41+
# 11| 2: [Annotation] Ann1
3742
# 0| 1: [IntegerLiteral] 1
3843
# 0| 1: [Annotation] Ann2
3944
# 0| 1: [StringLiteral] "Hello"
@@ -53,7 +58,15 @@ test.kt:
5358
# 0| 0: [TypeAccess] String
5459
# 0| 1: [TypeLiteral] int.class
5560
# 0| 0: [TypeAccess] int
56-
# 10| 1: [Constructor] Annotated
57-
# 9| 5: [BlockStmt] { ... }
58-
# 9| 0: [SuperConstructorInvocationStmt] super(...)
59-
# 10| 1: [BlockStmt] { ... }
61+
# 12| 3: [Annotation] GenericAnnotation<>
62+
# 13| 1: [Constructor] Annotated
63+
# 11| 5: [BlockStmt] { ... }
64+
# 11| 0: [SuperConstructorInvocationStmt] super(...)
65+
# 13| 1: [BlockStmt] { ... }
66+
# 0| [TypeLiteral] String.class
67+
# 0| 0: [TypeAccess] String
68+
# 0| [ArrayInit] {...}
69+
# 0| 1: [TypeLiteral] String.class
70+
# 0| 0: [TypeAccess] String
71+
# 0| 1: [TypeLiteral] String.class
72+
# 0| 0: [TypeAccess] String

java/ql/integration-tests/all-platforms/kotlin/annotation-id-consistency/test.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ annotation class Ann2(val z: String, val w: KClass<*>, val v: IntArray, val u: A
66

77
annotation class Ann3(val a: Int) { }
88

9+
annotation class GenericAnnotation<T : Any>(val x: KClass<T>, val y: Array<KClass<T>>) { }
10+
911
@Ann1(1, Ann2("Hello", String::class, intArrayOf(1, 2, 3), arrayOf(Ann3(1), Ann3(2)), arrayOf(String::class, Int::class)))
12+
@GenericAnnotation<String>(String::class, arrayOf(String::class, String::class))
1013
class Annotated { }

0 commit comments

Comments
 (0)