@@ -4,72 +4,78 @@ import org.jetbrains.org.objectweb.asm.*
4
4
import org.jetbrains.org.objectweb.asm.tree.*
5
5
import java.io.File
6
6
7
+ data class AnnotationData (
8
+ val parameters : Map <String , Any ?>
9
+ )
10
+
7
11
class AnnotationProcessor {
8
12
13
+ private val classAnnotations = mutableMapOf<String , MutableMap <String , AnnotationData >>()
14
+
9
15
fun processClassFile (classFile : File ) {
10
16
val classReader = ClassReader (classFile.readBytes())
11
17
val classNode = ClassNode ()
12
18
classReader.accept(classNode, 0 )
13
19
20
+ val annotations = mutableMapOf<String , AnnotationData >()
21
+
14
22
classNode.visibleAnnotations?.forEach { annotationNode ->
15
- println (" Class annotation: ${annotationNode.desc} " )
16
23
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
20
26
}
21
27
}
22
28
23
29
classNode.methods?.forEach { methodNode ->
24
30
methodNode.visibleAnnotations?.forEach { annotationNode ->
25
- println (" Method annotation in ${methodNode.name} : ${annotationNode.desc} " )
26
31
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
30
34
}
31
35
}
32
36
}
33
37
34
38
classNode.fields?.forEach { fieldNode ->
35
39
fieldNode.visibleAnnotations?.forEach { annotationNode ->
36
- println (" Field annotation in ${fieldNode.name} : ${annotationNode.desc} " )
37
40
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
41
43
}
42
44
}
43
45
}
46
+
47
+ classAnnotations[classNode.name] = annotations
44
48
}
45
49
46
- private fun printAnnotationValues (annotationNode : AnnotationNode ) {
50
+ private fun parseAnnotation (annotationNode : AnnotationNode ): AnnotationData {
51
+ val parameters = mutableMapOf<String , Any ?>()
47
52
annotationNode.values?.let { values ->
48
53
for (i in values.indices step 2 ) {
49
- val name = values[i]
54
+ val name = values[i] as String
50
55
val value = values[i + 1 ]
51
- println ( " Annotation parameter: $ name = ${ formatAnnotationValue(value)} " )
56
+ parameters[ name] = formatAnnotationValue(value)
52
57
}
53
58
}
59
+ return AnnotationData (parameters)
54
60
}
55
61
56
- private fun formatAnnotationValue (value : Any? ): String {
62
+ private fun formatAnnotationValue (value : Any? ): Any? {
57
63
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) }
60
66
is TypePath -> value.toString()
61
67
is AnnotationNode -> formatAnnotationNode(value)
62
68
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
73
79
}
74
80
}
75
81
@@ -88,4 +94,8 @@ class AnnotationProcessor {
88
94
sb.append(" )" )
89
95
return sb.toString()
90
96
}
97
+
98
+ fun getClassAnnotations (): Map <String , Map <String , AnnotationData >> {
99
+ return classAnnotations
100
+ }
91
101
}
0 commit comments