Skip to content

Commit 20b28ff

Browse files
committed
Retrieve annotation data
1 parent 4bcd09e commit 20b28ff

File tree

4 files changed

+95
-32
lines changed

4 files changed

+95
-32
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kotlinx.benchmark.gradle
33
import kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi
44
import org.gradle.api.*
55
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
6+
import java.util.*
67

78

89
@KotlinxBenchmarkPluginInternalApi
@@ -11,10 +12,10 @@ fun Project.processAndroidCompilation(target: KotlinJvmAndroidCompilation) {
1112
println("processAndroidCompilation: ${target.name}")
1213
val compilation = target.target.compilations.names.let(::println)
1314

14-
tasks.register("process${target.name.capitalize()}Compilation", DefaultTask::class.java) {
15+
tasks.register("process${target.name.capitalize(Locale.getDefault())}Compilation", DefaultTask::class.java) {
1516
it.group = "benchmark"
1617
it.description = "Processes the Android compilation '${target.name}' for benchmarks"
17-
it.dependsOn("bundle${target.name.capitalize()}Aar")
18+
it.dependsOn("bundle${target.name.capitalize(Locale.getDefault())}Aar")
1819
it.doLast {
1920
unpackAndProcessAar(target)
2021
}

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package kotlinx.benchmark.gradle
33
import kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi
44
import org.gradle.api.*
55
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
6-
import org.jetbrains.org.objectweb.asm.*
7-
import org.jetbrains.org.objectweb.asm.tree.*
86
import java.io.File
97

108
@KotlinxBenchmarkPluginInternalApi
@@ -43,32 +41,4 @@ fun Project.unpackAndProcessAar(target: KotlinJvmAndroidCompilation) {
4341
} else {
4442
println("AAR file not found")
4543
}
46-
}
47-
48-
class AnnotationProcessor {
49-
50-
fun processClassFile(classFile: File) {
51-
val classReader = ClassReader(classFile.readBytes())
52-
val classNode = ClassNode()
53-
classReader.accept(classNode, 0)
54-
55-
// Retrieve annotations from the class
56-
classNode.visibleAnnotations?.forEach { annotationNode ->
57-
println("Class annotation: ${annotationNode.desc}")
58-
}
59-
60-
// Retrieve annotations from the methods
61-
classNode.methods?.forEach { methodNode ->
62-
methodNode.visibleAnnotations?.forEach { annotationNode ->
63-
println("Method annotation in ${methodNode.name}: ${annotationNode.desc}")
64-
}
65-
}
66-
67-
// Retrieve annotations from the fields
68-
classNode.fields?.forEach { fieldNode ->
69-
fieldNode.visibleAnnotations?.forEach { annotationNode ->
70-
println("Field annotation in ${fieldNode.name}: ${annotationNode.desc}")
71-
}
72-
}
73-
}
7444
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package kotlinx.benchmark.gradle
2+
3+
import org.jetbrains.org.objectweb.asm.*
4+
import org.jetbrains.org.objectweb.asm.tree.*
5+
import java.io.File
6+
7+
class AnnotationProcessor {
8+
9+
fun processClassFile(classFile: File) {
10+
val classReader = ClassReader(classFile.readBytes())
11+
val classNode = ClassNode()
12+
classReader.accept(classNode, 0)
13+
14+
classNode.visibleAnnotations?.forEach { annotationNode ->
15+
println("Class annotation: ${annotationNode.desc}")
16+
if (annotationNode.desc != "Lkotlin/Metadata;") {
17+
printAnnotationValues(annotationNode)
18+
} else {
19+
println("Ignoring kotlin.Metadata annotation for readability.")
20+
}
21+
}
22+
23+
classNode.methods?.forEach { methodNode ->
24+
methodNode.visibleAnnotations?.forEach { annotationNode ->
25+
println("Method annotation in ${methodNode.name}: ${annotationNode.desc}")
26+
if (annotationNode.desc != "Lkotlin/Metadata;") {
27+
printAnnotationValues(annotationNode)
28+
} else {
29+
println("Ignoring kotlin.Metadata annotation for readability.")
30+
}
31+
}
32+
}
33+
34+
classNode.fields?.forEach { fieldNode ->
35+
fieldNode.visibleAnnotations?.forEach { annotationNode ->
36+
println("Field annotation in ${fieldNode.name}: ${annotationNode.desc}")
37+
if (annotationNode.desc != "Lkotlin/Metadata;") {
38+
printAnnotationValues(annotationNode)
39+
} else {
40+
println("Ignoring kotlin.Metadata annotation for readability.")
41+
}
42+
}
43+
}
44+
}
45+
46+
private fun printAnnotationValues(annotationNode: AnnotationNode) {
47+
annotationNode.values?.let { values ->
48+
for (i in values.indices step 2) {
49+
val name = values[i]
50+
val value = values[i + 1]
51+
println("Annotation parameter: $name = ${formatAnnotationValue(value)}")
52+
}
53+
}
54+
}
55+
56+
private fun formatAnnotationValue(value: Any?): String {
57+
return when (value) {
58+
is List<*> -> value.joinToString(prefix = "[", postfix = "]") { formatAnnotationValue(it) }
59+
is Array<*> -> value.joinToString(prefix = "[", postfix = "]") { formatAnnotationValue(it) }
60+
is TypePath -> value.toString()
61+
is AnnotationNode -> formatAnnotationNode(value)
62+
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()
73+
}
74+
}
75+
76+
private fun formatAnnotationNode(annotationNode: AnnotationNode): String {
77+
val sb = StringBuilder("@${annotationNode.desc}(")
78+
annotationNode.values?.let { values ->
79+
for (i in values.indices step 2) {
80+
val name = values[i]
81+
val value = values[i + 1]
82+
sb.append("$name = ${formatAnnotationValue(value)}, ")
83+
}
84+
}
85+
if (sb.endsWith(", ")) {
86+
sb.setLength(sb.length - 2)
87+
}
88+
sb.append(")")
89+
return sb.toString()
90+
}
91+
}

runtime/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins {
88

99
repositories {
1010
mavenCentral()
11+
google()
1112
}
1213

1314
android {

0 commit comments

Comments
 (0)