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 constuctor check
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 28, 2022
1 parent c55b649 commit af8f13a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Expand Up @@ -66,7 +66,12 @@ class ByteCodeParser {
return classNode
}

private fun createModifiers(classAccess: Int, allowField: Int, isInterface: Boolean, fieldExcluded: Int): Array<String> {
private fun createModifiers(
classAccess: Int,
allowField: Int,
isInterface: Boolean,
fieldExcluded: Int
): Array<String> {
var flags = classAccess
var excluded = fieldExcluded
flags = flags and allowField
Expand All @@ -88,7 +93,7 @@ class ByteCodeParser {

val isInterface = CodeConstants.ACC_INTERFACE == classNode.access

ds.Type = if(isInterface) DataStructType.INTERFACE else DataStructType.CLASS;
ds.Type = if (isInterface) DataStructType.INTERFACE else DataStructType.CLASS;

// todo: add modifiers to Chapi
createModifiers(classNode.access, FIELD_ALLOWED, isInterface, FIELD_EXCLUDED)
Expand Down Expand Up @@ -128,6 +133,10 @@ class ByteCodeParser {
private fun createMethod(methodNode: MethodNode): CodeFunction {
val codeFunction = CodeFunction(Name = methodNode.name)

if (methodNode.name == CodeConstants.INIT_NAME) {
codeFunction.IsConstructor = true
}

val isInterface: Boolean = CodeConstants.ACC_INTERFACE == methodNode.access
codeFunction.Modifiers = createModifiers(methodNode.access, METHOD_ALLOWED, isInterface, METHOD_EXCLUDED)
codeFunction.ReturnType = getReturnTypeFromDesc(methodNode.desc).orEmpty()
Expand All @@ -136,6 +145,10 @@ class ByteCodeParser {
codeFunction.Parameters = getParamsFromDesc(methodNode.desc, methodNode.parameters)
}

codeFunction.Annotations = methodNode.visibleAnnotations?.map {
createAnnotation(it)
}?.toTypedArray() ?: arrayOf()

return codeFunction
}

Expand Down
Expand Up @@ -14,6 +14,7 @@ internal class ByteCodeParserTest {
assertEquals("org.archguard.demo.HelloWorld", ds.NodeName)
assertEquals(2, ds.Functions.size)
assertEquals("<init>", ds.Functions[0].Name)
assert(ds.Functions[0].IsConstructor)
assertEquals("main", ds.Functions[1].Name)
assertEquals("void", ds.Functions[1].ReturnType)
}
Expand Down Expand Up @@ -69,7 +70,7 @@ internal class ByteCodeParserTest {
val resource = this.javaClass.classLoader.getResource("annotation/DemoApplication.class")
val path = Paths.get(resource.toURI()).toFile()

val ds = ByteCodeParser().parseClassFile(path)
g val ds = ByteCodeParser().parseClassFile(path)
assertEquals(2, ds.Functions.size)
assertEquals("args", ds.Functions[1].Parameters[0].TypeValue)
assertEquals("java.lang.String[]", ds.Functions[1].Parameters[0].TypeType)
Expand Down

0 comments on commit af8f13a

Please sign in to comment.