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 simple constant to TypeValue
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 29, 2022
1 parent eecafb3 commit 19e13e4
Showing 1 changed file with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,22 @@ class ByteCodeParser {

var isStart = false
val position = CodePosition()
val simpleConstantsPool: Stack<String> = Stack()
var simpleConstantsPool: Stack<String> = Stack()

methodNode.instructions.forEach {
println(it.javaClass.simpleName + " ")

when (it) {
is LdcInsnNode -> {
val item = it.cst.toString()
simpleConstantsPool.push(item)
when(it.cst) {
is Type -> {
println("Type -> cst: ${it.cst}")
val cst = it.cst
println(Type.getType(cst.toString()).className)
}
else -> {
simpleConstantsPool.push(item)
}
}
}
is LineNumberNode -> {
if (!isStart) {
Expand All @@ -242,29 +249,40 @@ class ByteCodeParser {
is MethodInsnNode -> {
val isInitMethod = it.name == CodeConstants.INIT_NAME || it.name == CodeConstants.CLINIT_NAME
val qualifiedName = refineMethodOwner(it.name, it.owner, node).orEmpty()
val isJavaOrKotlin = qualifiedName.startsWith("java.lang") || qualifiedName.startsWith("kotlin.jvm")
val isJavaOrKotlin = qualifiedName.startsWith("java.lang")

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

if (!(isInitMethod || isJavaOrKotlin)) {
println(it.owner)
// val pools = Type.getType(it.desc).argumentTypes.map {
// simpleConstantsPool.pop()
// }.toList()
println(simpleConstantsPool.elements.joinToString(", "))
val pools = Type.getType(it.desc).argumentTypes.map {
simpleConstantsPool.pop()
}.toList()

calls += CodeCall(
Package = names.first,
NodeName = names.second,
FunctionName = it.name,
Parameters = getArgsFromDesc(it.desc),
Parameters = getArgsFromDesc(it.desc, pools),
// todo: add return type for code call
// Type = Type.getType(it.desc).returnType.className
)
}
}
is LabelNode -> {}
is InsnNode -> {}
is JumpInsnNode -> {}
is VarInsnNode -> {
// println("VarInsnNode: ${it.opcode}")
}
is TypeInsnNode -> {
// println("TypeInsnNode: ${it.desc}")
}
is FieldInsnNode -> {
// println("FieldInsnNode: ${it.name}")
}
else -> {
// println(it.javaClass.simpleName + " ")
}
}
}
Expand Down Expand Up @@ -306,13 +324,18 @@ class ByteCodeParser {
}.toTypedArray()
}

private fun getArgsFromDesc(desc: String): Array<CodeProperty> {
return Type.getType(desc).argumentTypes.map {
private fun getArgsFromDesc(desc: String, pools: List<String?>): Array<CodeProperty> {
return Type.getType(desc).argumentTypes.mapIndexed { index, it ->
importCollector.addImport(it.className)

CodeProperty(
TypeType = it.className,
TypeValue = it.internalName
TypeValue = (if (pools[index] != null) {
pools[index]
} else {
it.internalName
}).toString()

)
}.toTypedArray()
}
Expand Down

0 comments on commit 19e13e4

Please sign in to comment.