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 get classes support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 28, 2022
1 parent 87d55a5 commit 82fb58a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ class ByteCodeParser {

private fun createClass(classNode: ClassNode): CodeDataStruct {
val ds = CodeDataStruct()
ds.NodeName = getClassName(classNode.name).toString()
ds.NodeName = getDataStructureName(classNode.name).toString()

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

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

return ds
}

Expand All @@ -49,7 +54,7 @@ class ByteCodeParser {
}


private fun getClassName(internalName: String): String? {
private fun getDataStructureName(internalName: String): String {
return Type.getObjectType(internalName).className
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.archguard.scanner.bytecode

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.nio.file.Paths
import kotlin.test.assertEquals

internal class ByteCodeParserTest {
@Test
fun parseHelloWorldJava() {
fun java_hello_world() {
val resource = this.javaClass.classLoader.getResource("classes/HelloWorld.class")
val path = Paths.get(resource.toURI()).toFile()

Expand All @@ -18,7 +19,7 @@ internal class ByteCodeParserTest {
}

@Test
fun parseHelloWorld() {
fun scala_hello_world() {
val resource = this.javaClass.classLoader.getResource("scala/Hello.class")
val path = Paths.get(resource.toURI()).toFile()

Expand All @@ -27,4 +28,14 @@ internal class ByteCodeParserTest {
assertEquals(1, ds.Functions.size)
assertEquals("main", ds.Functions[0].Name)
}

@Test
fun should_get_parent() {
val resource = this.javaClass.classLoader.getResource("inheritance/Child.class")
val path = Paths.get(resource.toURI()).toFile()

val ds = ByteCodeParser().parseClassFile(path)
assertEquals("com.example.demo.Parent", ds.Extend)
assertEquals("com.example.demo.Interface", ds.Implements[0])
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 82fb58a

Please sign in to comment.