Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat(bytecode): add annotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 28, 2022
1 parent 23d6882 commit 11e0f50
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.archguard.scanner.bytecode

import chapi.domain.core.AnnotationKeyValue
import chapi.domain.core.CodeAnnotation
import chapi.domain.core.CodeDataStruct
import chapi.domain.core.CodeFunction
import org.objectweb.asm.ClassReader
Expand Down Expand Up @@ -38,26 +40,38 @@ class ByteCodeParser {
ds.NodeName = getDataStructureName(classNode.name).toString()

classNode.methods.forEach {
ds.Functions += this.createMethod(it, ds.NodeName, classNode)
ds.Functions += this.createMethod(it)
}

ds.Extend = getDataStructureName(classNode.superName)
ds.Implements = classNode.interfaces?.map {
getDataStructureName(it)
}?.toTypedArray() ?: arrayOf()

classNode.visibleAnnotations?.map {
ds.Annotations = classNode.visibleAnnotations?.map {
createAnnotation(it)
}
}?.toTypedArray() ?: arrayOf()

return ds
}

private fun createAnnotation(annotation: AnnotationNode) {
println(annotation.desc)
private fun createAnnotation(annotation: AnnotationNode): CodeAnnotation {
val name: String = Type.getType(annotation.desc).className
val codeAnnotation = CodeAnnotation(Name = name)

if (annotation.values != null) {
val values: List<Any> = annotation.values
var i = 0
while (i < values.size) {
codeAnnotation.KeyValues += AnnotationKeyValue(values[i].toString(), values[i + 1].toString())
i += 2
}
}

return codeAnnotation
}

private fun createMethod(methodNode: MethodNode, nodeName: String, classNode: ClassNode): CodeFunction {
private fun createMethod(methodNode: MethodNode): CodeFunction {
val codeFunction = CodeFunction(Name = methodNode.name)
return codeFunction
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ internal class ByteCodeParserTest {

val ds = ByteCodeParser().parseClassFile(path)
assertEquals(1, ds.Annotations.size)
assertEquals("org.springframework.boot.autoconfigure.SpringBootApplication", ds.Annotations[0].Name)
}
}

0 comments on commit 11e0f50

Please sign in to comment.