Skip to content

Commit fdda59b

Browse files
committed
refactor retrieve annotation data
1 parent 48b9049 commit fdda59b

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,31 @@ fun Project.unpackAndProcessAar(target: KotlinJvmAndroidCompilation) {
3737
}
3838

3939
val annotations = annotationProcessor.getClassAnnotations()
40-
// Use the annotations data
4140
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")
41+
println("Annotation for class: $className")
42+
43+
classAnnotations.classAnnotations.forEach { (annotationDesc, annotationData) ->
44+
println("Class annotation: $annotationDesc")
45+
annotationData.parameters.forEach { (name, value) ->
46+
println(" - $name: $value")
47+
}
48+
}
49+
50+
classAnnotations.methodAnnotations.forEach { (methodName, methodAnnotationMap) ->
51+
methodAnnotationMap.forEach { (annotationDesc, annotationData) ->
52+
println("Method annotation in $methodName: $annotationDesc")
53+
annotationData.parameters.forEach { (name, value) ->
54+
println(" - $name: $value")
55+
}
56+
}
57+
}
58+
59+
classAnnotations.fieldAnnotations.forEach { (fieldName, fieldAnnotationMap) ->
60+
fieldAnnotationMap.forEach { (annotationDesc, annotationData) ->
61+
println("Field annotation in $fieldName: $annotationDesc")
62+
annotationData.parameters.forEach { (name, value) ->
63+
println(" - $name: $value")
64+
}
4765
}
4866
}
4967
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,37 @@ data class AnnotationData(
88
val parameters: Map<String, Any?>
99
)
1010

11+
data class ClassAnnotations(
12+
val classAnnotations: Map<String, AnnotationData>,
13+
val methodAnnotations: Map<String, Map<String, AnnotationData>>,
14+
val fieldAnnotations: Map<String, Map<String, AnnotationData>>
15+
)
16+
1117
class AnnotationProcessor {
1218

13-
private val classAnnotations = mutableMapOf<String, MutableMap<String, AnnotationData>>()
19+
private val classAnnotationsMap = mutableMapOf<String, ClassAnnotations>()
1420

1521
fun processClassFile(classFile: File) {
1622
val classReader = ClassReader(classFile.readBytes())
1723
val classNode = ClassNode()
1824
classReader.accept(classNode, 0)
1925

20-
val annotations = mutableMapOf<String, AnnotationData>()
26+
val classAnnotations = mutableMapOf<String, AnnotationData>()
27+
val methodAnnotations = mutableMapOf<String, MutableMap<String, AnnotationData>>()
28+
val fieldAnnotations = mutableMapOf<String, MutableMap<String, AnnotationData>>()
2129

2230
classNode.visibleAnnotations?.forEach { annotationNode ->
2331
if (annotationNode.desc != "Lkotlin/Metadata;") {
2432
val annotationData = parseAnnotation(annotationNode)
25-
annotations[annotationNode.desc] = annotationData
33+
classAnnotations[annotationNode.desc] = annotationData
2634
}
2735
}
2836

2937
classNode.methods?.forEach { methodNode ->
3038
methodNode.visibleAnnotations?.forEach { annotationNode ->
3139
if (annotationNode.desc != "Lkotlin/Metadata;") {
3240
val annotationData = parseAnnotation(annotationNode)
33-
annotations[annotationNode.desc] = annotationData
41+
methodAnnotations.getOrPut(methodNode.name) { mutableMapOf() }[annotationNode.desc] = annotationData
3442
}
3543
}
3644
}
@@ -39,12 +47,12 @@ class AnnotationProcessor {
3947
fieldNode.visibleAnnotations?.forEach { annotationNode ->
4048
if (annotationNode.desc != "Lkotlin/Metadata;") {
4149
val annotationData = parseAnnotation(annotationNode)
42-
annotations[annotationNode.desc] = annotationData
50+
fieldAnnotations.getOrPut(fieldNode.name) { mutableMapOf() }[annotationNode.desc] = annotationData
4351
}
4452
}
4553
}
4654

47-
classAnnotations[classNode.name] = annotations
55+
classAnnotationsMap[classNode.name] = ClassAnnotations(classAnnotations, methodAnnotations, fieldAnnotations)
4856
}
4957

5058
private fun parseAnnotation(annotationNode: AnnotationNode): AnnotationData {
@@ -95,7 +103,7 @@ class AnnotationProcessor {
95103
return sb.toString()
96104
}
97105

98-
fun getClassAnnotations(): Map<String, Map<String, AnnotationData>> {
99-
return classAnnotations
106+
fun getClassAnnotations(): Map<String, ClassAnnotations> {
107+
return classAnnotationsMap
100108
}
101109
}

0 commit comments

Comments
 (0)