From a85d382e89a3969f9989b9d2f0c1e6ece228861c Mon Sep 17 00:00:00 2001 From: Skylot Date: Tue, 24 Dec 2013 22:38:34 +0400 Subject: [PATCH] core: reformat TryCatchBlock class --- .../jadx/core/dex/nodes/InsnContainer.java | 2 +- .../jadx/core/dex/trycatch/TryCatchBlock.java | 65 ++++++++++--------- .../jadx/tests/internal/TestTryCatch.java | 32 +++++++++ 3 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/internal/TestTryCatch.java diff --git a/jadx-core/src/main/java/jadx/core/dex/nodes/InsnContainer.java b/jadx-core/src/main/java/jadx/core/dex/nodes/InsnContainer.java index 24b53a61e..f0b1af416 100644 --- a/jadx-core/src/main/java/jadx/core/dex/nodes/InsnContainer.java +++ b/jadx-core/src/main/java/jadx/core/dex/nodes/InsnContainer.java @@ -8,7 +8,7 @@ public class InsnContainer extends AttrNode implements IBlock { private List insns; - public void setInstructions(List insns) { + public InsnContainer(List insns) { this.insns = insns; } diff --git a/jadx-core/src/main/java/jadx/core/dex/trycatch/TryCatchBlock.java b/jadx-core/src/main/java/jadx/core/dex/trycatch/TryCatchBlock.java index e2b7b13d8..f2821287c 100644 --- a/jadx-core/src/main/java/jadx/core/dex/trycatch/TryCatchBlock.java +++ b/jadx-core/src/main/java/jadx/core/dex/trycatch/TryCatchBlock.java @@ -15,6 +15,8 @@ import jadx.core.utils.Utils; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; public class TryCatchBlock { @@ -27,7 +29,7 @@ public class TryCatchBlock { private final CatchAttr attr; public TryCatchBlock() { - handlers = new ArrayList(2); + handlers = new LinkedList(); insns = new ArrayList(); attr = new CatchAttr(this); } @@ -45,9 +47,10 @@ public class TryCatchBlock { } public void removeHandler(MethodNode mth, ExceptionHandler handler) { - for (int i = 0; i < handlers.size(); i++) { - if (handlers.get(i) == handler) { - handlers.remove(i); + for (Iterator it = handlers.iterator(); it.hasNext(); ) { + ExceptionHandler h = it.next(); + if (h == handler) { + it.remove(); break; } } @@ -70,16 +73,16 @@ public class TryCatchBlock { } } } - return; + } else { + // self destruction + for (InsnNode insn : insns) { + insn.getAttributes().remove(attr); + } + insns.clear(); + for (BlockNode block : mth.getBasicBlocks()) { + block.getAttributes().remove(attr); + } } - - // self destruction - for (InsnNode insn : insns) - insn.getAttributes().remove(attr); - - insns.clear(); - for (BlockNode block : mth.getBasicBlocks()) - block.getAttributes().remove(attr); } public void addInsn(InsnNode insn) { @@ -113,34 +116,34 @@ public class TryCatchBlock { } public void setFinalBlockFromInsns(MethodNode mth, List insns) { - InsnContainer cont = new InsnContainer(); List finalBlockInsns = new ArrayList(insns); - cont.setInstructions(finalBlockInsns); - setFinalBlock(cont); + setFinalBlock(new InsnContainer(finalBlockInsns)); InstructionRemover.unbindInsnList(finalBlockInsns); // remove these instructions from other handlers for (ExceptionHandler h : getHandlers()) { - for (BlockNode ehb : h.getBlocks()) + for (BlockNode ehb : h.getBlocks()) { ehb.getInstructions().removeAll(finalBlockInsns); + } } // remove from blocks with this catch for (BlockNode b : mth.getBasicBlocks()) { IAttribute ca = b.getAttributes().get(AttributeType.CATCH_BLOCK); - if (attr == ca) + if (attr == ca) { b.getInstructions().removeAll(finalBlockInsns); + } } } public void merge(MethodNode mth, TryCatchBlock tryBlock) { - for (InsnNode insn : tryBlock.getInsns()) + for (InsnNode insn : tryBlock.getInsns()) { this.addInsn(insn); - + } this.handlers.addAll(tryBlock.getHandlers()); - for (ExceptionHandler eh : handlers) + for (ExceptionHandler eh : handlers) { eh.setTryBlock(this); - + } // clear tryBlock.handlers.clear(); tryBlock.removeWholeBlock(mth); @@ -148,25 +151,23 @@ public class TryCatchBlock { @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((handlers == null) ? 0 : handlers.hashCode()); - return result; + return handlers.hashCode(); } @Override public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } TryCatchBlock other = (TryCatchBlock) obj; - if (!handlers.equals(other.handlers)) return false; - return true; + return handlers.equals(other.handlers); } @Override public String toString() { return "Catch:{ " + Utils.listToString(handlers) + " }"; } - } diff --git a/jadx-core/src/test/java/jadx/tests/internal/TestTryCatch.java b/jadx-core/src/test/java/jadx/tests/internal/TestTryCatch.java new file mode 100644 index 000000000..5d6e9297f --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/internal/TestTryCatch.java @@ -0,0 +1,32 @@ +package jadx.tests.internal; + +import jadx.api.InternalJadxTest; +import jadx.core.dex.nodes.ClassNode; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + +public class TestTryCatch extends InternalJadxTest { + + public static class TestCls { + private void f() { + try { + Thread.sleep(50); + } catch (InterruptedException e) { + // ignore + } + } + } + + @Test + public void test() { + ClassNode cls = getClassNode(TestCls.class); + String code = cls.getCode().toString(); + + assertThat(code, containsString("try {")); + assertThat(code, containsString("Thread.sleep(50);")); + assertThat(code, containsString("} catch (InterruptedException e) {")); + } +}