Skip to content

Commit f7f6e51

Browse files
DaniilStepanovlehvolk
authored andcommitted
Fix of try/catch empty handlers
1 parent 2307fe9 commit f7f6e51

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/MethodNodeBuilder.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class MethodNodeBuilder(
7878
fun build(): MethodNode {
7979
initializeFrame(method)
8080
buildInstructionList()
81+
insertNopInstructions()
8182
val mn = MethodNode()
8283
mn.name = method.name
8384
mn.desc = method.description
@@ -790,4 +791,23 @@ class MethodNodeBuilder(
790791
error("Could not load method constant $value")
791792
}
792793

794+
//We have to insert NOP instructions in empty basic blocks to handle situations with empty handlers of try/catch
795+
private fun insertNopInstructions() {
796+
val firstLabelIndex = currentInsnList.indexOfFirst { it is LabelNode }
797+
val nodesBetweenLabels = mutableListOf<AbstractInsnNode>()
798+
var i = firstLabelIndex + 1
799+
while (i < currentInsnList.size()) {
800+
when (val curInst = currentInsnList[i]) {
801+
is LabelNode -> {
802+
if (nodesBetweenLabels.all { it is LineNumberNode }) {
803+
currentInsnList.insertBefore(curInst, InsnNode(Opcodes.NOP))
804+
++i
805+
}
806+
nodesBetweenLabels.clear()
807+
}
808+
else -> nodesBetweenLabels.add(curInst)
809+
}
810+
++i
811+
}
812+
}
793813
}

0 commit comments

Comments
 (0)