Skip to content

Commit 48b9049

Browse files
committed
Retrieve annotation data
1 parent 20b28ff commit 48b9049

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

plugin/main/src/kotlinx/benchmark/gradle/AndroidTasks.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ fun Project.unpackAndProcessAar(target: KotlinJvmAndroidCompilation) {
3535
annotationProcessor.processClassFile(file)
3636
}
3737
}
38+
39+
val annotations = annotationProcessor.getClassAnnotations()
40+
// Use the annotations data
41+
annotations.forEach { (className, classAnnotations) ->
42+
println("Annotations for class: $className")
43+
classAnnotations.forEach { (annotationName, annotationData) ->
44+
println(" - Annotation: $annotationName")
45+
annotationData.parameters.forEach { (paramName, paramValue) ->
46+
println(" - $paramName: $paramValue")
47+
}
48+
}
49+
}
50+
3851
} else {
3952
println("classes.jar not found in AAR file")
4053
}

plugin/main/src/kotlinx/benchmark/gradle/AnnotationProcessor.kt

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,78 @@ import org.jetbrains.org.objectweb.asm.*
44
import org.jetbrains.org.objectweb.asm.tree.*
55
import java.io.File
66

7+
data class AnnotationData(
8+
val parameters: Map<String, Any?>
9+
)
10+
711
class AnnotationProcessor {
812

13+
private val classAnnotations = mutableMapOf<String, MutableMap<String, AnnotationData>>()
14+
915
fun processClassFile(classFile: File) {
1016
val classReader = ClassReader(classFile.readBytes())
1117
val classNode = ClassNode()
1218
classReader.accept(classNode, 0)
1319

20+
val annotations = mutableMapOf<String, AnnotationData>()
21+
1422
classNode.visibleAnnotations?.forEach { annotationNode ->
15-
println("Class annotation: ${annotationNode.desc}")
1623
if (annotationNode.desc != "Lkotlin/Metadata;") {
17-
printAnnotationValues(annotationNode)
18-
} else {
19-
println("Ignoring kotlin.Metadata annotation for readability.")
24+
val annotationData = parseAnnotation(annotationNode)
25+
annotations[annotationNode.desc] = annotationData
2026
}
2127
}
2228

2329
classNode.methods?.forEach { methodNode ->
2430
methodNode.visibleAnnotations?.forEach { annotationNode ->
25-
println("Method annotation in ${methodNode.name}: ${annotationNode.desc}")
2631
if (annotationNode.desc != "Lkotlin/Metadata;") {
27-
printAnnotationValues(annotationNode)
28-
} else {
29-
println("Ignoring kotlin.Metadata annotation for readability.")
32+
val annotationData = parseAnnotation(annotationNode)
33+
annotations[annotationNode.desc] = annotationData
3034
}
3135
}
3236
}
3337

3438
classNode.fields?.forEach { fieldNode ->
3539
fieldNode.visibleAnnotations?.forEach { annotationNode ->
36-
println("Field annotation in ${fieldNode.name}: ${annotationNode.desc}")
3740
if (annotationNode.desc != "Lkotlin/Metadata;") {
38-
printAnnotationValues(annotationNode)
39-
} else {
40-
println("Ignoring kotlin.Metadata annotation for readability.")
41+
val annotationData = parseAnnotation(annotationNode)
42+
annotations[annotationNode.desc] = annotationData
4143
}
4244
}
4345
}
46+
47+
classAnnotations[classNode.name] = annotations
4448
}
4549

46-
private fun printAnnotationValues(annotationNode: AnnotationNode) {
50+
private fun parseAnnotation(annotationNode: AnnotationNode): AnnotationData {
51+
val parameters = mutableMapOf<String, Any?>()
4752
annotationNode.values?.let { values ->
4853
for (i in values.indices step 2) {
49-
val name = values[i]
54+
val name = values[i] as String
5055
val value = values[i + 1]
51-
println("Annotation parameter: $name = ${formatAnnotationValue(value)}")
56+
parameters[name] = formatAnnotationValue(value)
5257
}
5358
}
59+
return AnnotationData(parameters)
5460
}
5561

56-
private fun formatAnnotationValue(value: Any?): String {
62+
private fun formatAnnotationValue(value: Any?): Any? {
5763
return when (value) {
58-
is List<*> -> value.joinToString(prefix = "[", postfix = "]") { formatAnnotationValue(it) }
59-
is Array<*> -> value.joinToString(prefix = "[", postfix = "]") { formatAnnotationValue(it) }
64+
is List<*> -> value.map { formatAnnotationValue(it) }
65+
is Array<*> -> value.map { formatAnnotationValue(it) }
6066
is TypePath -> value.toString()
6167
is AnnotationNode -> formatAnnotationNode(value)
6268
is Type -> value.className
63-
is String -> "\"${value.replace("\"", "\\\"")}\""
64-
is ByteArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
65-
is CharArray -> value.joinToString(prefix = "[", postfix = "]") { "\"${it}\"" }
66-
is ShortArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
67-
is IntArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
68-
is LongArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
69-
is FloatArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
70-
is DoubleArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
71-
is BooleanArray -> value.joinToString(prefix = "[", postfix = "]") { it.toString() }
72-
else -> value.toString()
69+
is String -> value.replace("\"", "\\\"")
70+
is ByteArray -> value.toList()
71+
is CharArray -> value.map { it.toString() }
72+
is ShortArray -> value.toList()
73+
is IntArray -> value.toList()
74+
is LongArray -> value.toList()
75+
is FloatArray -> value.toList()
76+
is DoubleArray -> value.toList()
77+
is BooleanArray -> value.toList()
78+
else -> value
7379
}
7480
}
7581

@@ -88,4 +94,8 @@ class AnnotationProcessor {
8894
sb.append(")")
8995
return sb.toString()
9096
}
97+
98+
fun getClassAnnotations(): Map<String, Map<String, AnnotationData>> {
99+
return classAnnotations
100+
}
91101
}

0 commit comments

Comments
 (0)