From 5dbd4d1d0cbba36edff41d9af79ca380e50c3d3d Mon Sep 17 00:00:00 2001 From: v7878 <46866705+vova7878@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:01:34 +0700 Subject: [PATCH] CodeBuilder: Add try_catch and try_catch_all method variants without handler parameter --- .../com/v7878/dex/bytecode/CodeBuilder.java | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/v7878/dex/bytecode/CodeBuilder.java b/src/main/java/com/v7878/dex/bytecode/CodeBuilder.java index 3a9a1f1..40ce443 100644 --- a/src/main/java/com/v7878/dex/bytecode/CodeBuilder.java +++ b/src/main/java/com/v7878/dex/bytecode/CodeBuilder.java @@ -322,17 +322,37 @@ private void addTryBlock(Object start, Object end, TypeId exceptionType, Object try_blocks.add(new BuilderTryBlock(try_blocks.size(), start, end, exceptionType, handler)); } - public CodeBuilder try_catch(String start, String end, TypeId exceptionType, String handler) { + private CodeBuilder try_catch_internal(Object start, Object end, TypeId exceptionType, Object handler) { Objects.requireNonNull(exceptionType); addTryBlock(start, end, exceptionType, handler); return this; } - public CodeBuilder try_catch_all(String start, String end, String handler) { + public CodeBuilder try_catch(String start, String end, TypeId exceptionType, String handler) { + return try_catch_internal(start, end, exceptionType, handler); + } + + public CodeBuilder try_catch(String start, String end, TypeId exceptionType) { + InternalLabel handler = new InternalLabel(); + putLabel(handler); + return try_catch_internal(start, end, exceptionType, handler); + } + + public CodeBuilder try_catch_all_internal(Object start, Object end, Object handler) { addTryBlock(start, end, null, handler); return this; } + public CodeBuilder try_catch_all(String start, String end, String handler) { + return try_catch_all_internal(start, end, handler); + } + + public CodeBuilder try_catch_all(String start, String end) { + InternalLabel handler = new InternalLabel(); + putLabel(handler); + return try_catch_all_internal(start, end, handler); + } + public CodeBuilder if_(boolean value, Consumer true_branch, Consumer false_branch) { if (value) { @@ -967,34 +987,34 @@ public CodeBuilder throw_(int ex_reg) { return f11x(Opcode.THROW, ex_reg, false); } - private CodeBuilder goto_(Object label) { + private CodeBuilder goto_internal(Object label) { int start_unit = current_unit; return f10t(Opcode.GOTO, () -> getLabelBranchOffset(label, start_unit)); } public CodeBuilder goto_(String label) { - return goto_((Object) label); + return goto_internal(label); } - private CodeBuilder goto_16(Object label) { + private CodeBuilder goto_16_internal(Object label) { int start_unit = current_unit; return f20t(Opcode.GOTO_16, () -> getLabelBranchOffset(label, start_unit)); } public CodeBuilder goto_16(String label) { - return goto_16((Object) label); + return goto_16_internal(label); } - private CodeBuilder goto_32(Object label) { + private CodeBuilder goto_32_internal(Object label) { int start_unit = current_unit; return f30t(Opcode.GOTO_32, () -> getLabelBranchOffset(label, start_unit, true)); } public CodeBuilder goto_32(String label) { - return goto_32((Object) label); + return goto_32_internal(label); } - private CodeBuilder packed_switch(int reg_to_test, int first_key, Object... labels) { + private CodeBuilder packed_switch_internal(int reg_to_test, int first_key, Object... labels) { Objects.requireNonNull(labels); int start_unit = current_unit; InternalLabel payload = new InternalLabel(); @@ -1012,10 +1032,10 @@ private CodeBuilder packed_switch(int reg_to_test, int first_key, Object... labe } public CodeBuilder packed_switch(int reg_to_test, int first_key, String... labels) { - return packed_switch(reg_to_test, first_key, (Object[]) labels); + return packed_switch_internal(reg_to_test, first_key, (Object[]) labels); } - private CodeBuilder sparse_switch(int reg_to_test, int[] keys, Object... labels) { + private CodeBuilder sparse_switch_internal(int reg_to_test, int[] keys, Object... labels) { if (keys.length != labels.length) { throw new IllegalArgumentException("sparse_switch: keys.length != labels.length"); } @@ -1035,7 +1055,7 @@ private CodeBuilder sparse_switch(int reg_to_test, int[] keys, Object... labels) } public CodeBuilder sparse_switch(int reg_to_test, int[] keys, String... labels) { - return sparse_switch(reg_to_test, keys, (Object[]) labels); + return sparse_switch_internal(reg_to_test, keys, (Object[]) labels); } public enum Cmp { @@ -1076,7 +1096,7 @@ public enum Test { } } - private CodeBuilder if_test(Test test, int first_reg_to_test, int second_reg_to_test, Object label) { + private CodeBuilder if_test_internal(Test test, int first_reg_to_test, int second_reg_to_test, Object label) { int start_unit = current_unit; return f22t(test.test, first_reg_to_test, false, second_reg_to_test, false, @@ -1084,7 +1104,7 @@ private CodeBuilder if_test(Test test, int first_reg_to_test, int second_reg_to_ } public CodeBuilder if_test(Test test, int first_reg_to_test, int second_reg_to_test, String label) { - return if_test(test, first_reg_to_test, second_reg_to_test, (Object) label); + return if_test_internal(test, first_reg_to_test, second_reg_to_test, label); } public CodeBuilder if_test_else(Test test, int first_reg_to_test, int second_reg_to_test, @@ -1092,9 +1112,9 @@ public CodeBuilder if_test_else(Test test, int first_reg_to_test, int second_reg InternalLabel true_ = new InternalLabel(); InternalLabel end = new InternalLabel(); - if_test(test, first_reg_to_test, second_reg_to_test, true_); + if_test_internal(test, first_reg_to_test, second_reg_to_test, true_); false_branch.accept(this); - goto_32(end); + goto_32_internal(end); putLabel(true_); true_branch.accept(this); putLabel(end); @@ -1102,14 +1122,14 @@ public CodeBuilder if_test_else(Test test, int first_reg_to_test, int second_reg return this; } - private CodeBuilder if_testz(Test test, int reg_to_test, Object label) { + private CodeBuilder if_testz_internal(Test test, int reg_to_test, Object label) { int start_unit = current_unit; return f21t(test.testz, reg_to_test, false, () -> getLabelBranchOffset(label, start_unit)); } public CodeBuilder if_testz(Test test, int reg_to_test, String label) { - return if_testz(test, reg_to_test, (Object) label); + return if_testz_internal(test, reg_to_test, label); } public CodeBuilder if_testz_else(Test test, int reg_to_test, Consumer true_branch, @@ -1117,9 +1137,9 @@ public CodeBuilder if_testz_else(Test test, int reg_to_test, Consumer