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

Commit

Permalink
feat(bytecode): change annotation to end with import collector
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 29, 2022
1 parent 22cae37 commit 3a86b78
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import chapi.domain.core.*
import org.archguard.scanner.bytecode.module.CodeModule
import org.archguard.scanner.bytecode.module.ModuleUtil.getModule
import org.objectweb.asm.ClassReader
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.*
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -146,7 +144,7 @@ class ByteCodeParser {

val className = Type.getType(field.desc).className

importCollector.processClassName(className)
importCollector.addImport(className)

return CodeField(
TypeType = className,
Expand All @@ -163,9 +161,10 @@ class ByteCodeParser {
return null
}

val codeAnnotation = CodeAnnotation(Name = className)
val clzName = importCollector.splitPackageAndClassName(className).second
val codeAnnotation = CodeAnnotation(Name = clzName)

importCollector.processClassName(className)
importCollector.addImport(className)

if (annotation.values != null) {
val values: List<Any> = annotation.values
Expand Down Expand Up @@ -206,10 +205,6 @@ class ByteCodeParser {
return codeFunction
}

class SimpleMethodVisitor() : MethodVisitor(Opcodes.ASM9) {

}

private fun createFunctionCalls(methodNode: MethodNode, node: ClassNode): Array<CodeCall> {
var calls: Array<CodeCall> = arrayOf()

Expand All @@ -233,19 +228,7 @@ class ByteCodeParser {
val qualifiedName = refineMethodOwner(it.name, it.owner, node).orEmpty()

val names = importCollector.splitPackageAndClassName(qualifiedName)
importCollector.processClassName(qualifiedName)

// if (qualifiedName == "org.springframework.web.client.RestTemplate") {
// println(it.opcode)
// when(it.opcode) {
// CodeConstants.opc_invokevirtual -> {
//
// }
// CodeConstants.opc_invokespecial -> {
//
// }
// }
// }
importCollector.addImport(qualifiedName)

calls += CodeCall(
Package = names.first,
Expand Down Expand Up @@ -282,15 +265,15 @@ class ByteCodeParser {
private fun classNameFromType(superName: String, isImport: Boolean): String {
val className = Type.getObjectType(superName).className
if (isImport) {
importCollector.processClassName(className)
importCollector.addImport(className)
}

return className
}

private fun getParamsFromDesc(desc: String, parameters: MutableList<ParameterNode>): Array<CodeProperty> {
return Type.getType(desc).argumentTypes.mapIndexed { index, it ->
importCollector.processClassName(it.className)
importCollector.addImport(it.className)

CodeProperty(
TypeType = it.className,
Expand All @@ -301,7 +284,7 @@ class ByteCodeParser {

private fun getArgsFromDesc(desc: String): Array<CodeProperty> {
return Type.getType(desc).argumentTypes.map {
importCollector.processClassName(it.className)
importCollector.addImport(it.className)

CodeProperty(
TypeType = it.className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ class ImportCollector {
private val KOTLIN_LANG_PACKAGE = "kotlin."

private var mapSimpleNames: MutableMap<String, String> = mutableMapOf()
private val setNotImportedNames: Set<String> = HashSet()

// set of field names in this class and all its predecessors.
private val setFieldNames: Set<String> = HashSet()
private val setInnerClassNames: Set<String> = HashSet()
private val currentPackageSlash: String? = null
private val currentPackagePoint: String? = null

fun ImportCollector() {

}

fun splitPackageAndClassName(fullName: String): Pair<String, String> {
val lastDot = fullName.lastIndexOf('.')
Expand All @@ -29,22 +18,16 @@ class ImportCollector {
return Pair(packageName, className)
}

private fun processNestedName(fullName: String): String {
mapSimpleNames[fullName] = fullName
return ""
}

fun packImports(): MutableMap<String, String> {
return mapSimpleNames
}

fun processClassName(className: String) {
fun addImport(className: String) {
if (className.startsWith(JAVA_LANG_PACKAGE) || className.startsWith(KOTLIN_LANG_PACKAGE)) {
return
}

val names = splitPackageAndClassName(className)

this.mapSimpleNames[className] = names.second
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class ByteCodeParserTest {

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

@Test
Expand Down Expand Up @@ -122,6 +122,15 @@ internal class ByteCodeParserTest {
assertEquals(1, ds.Annotations.size)
}

@Test
fun should_ident_for_restful_api() {
val resource = this.javaClass.classLoader.getResource("controller/CodeTreeController.class")
val path = Paths.get(resource.toURI()).toFile()
val ds = ByteCodeParser().parseClassFile(path)

assertEquals(2, ds.Annotations.size)
}

@Test
fun should_ident_kotlin() {
val resource = this.javaClass.classLoader.getResource("kotlin/QualityGateClientImpl.class")
Expand Down
Binary file not shown.

0 comments on commit 3a86b78

Please sign in to comment.