Commit a85d382e authored by Skylot's avatar Skylot

core: reformat TryCatchBlock class

parent 4caa58f5
...@@ -8,7 +8,7 @@ public class InsnContainer extends AttrNode implements IBlock { ...@@ -8,7 +8,7 @@ public class InsnContainer extends AttrNode implements IBlock {
private List<InsnNode> insns; private List<InsnNode> insns;
public void setInstructions(List<InsnNode> insns) { public InsnContainer(List<InsnNode> insns) {
this.insns = insns; this.insns = insns;
} }
......
...@@ -15,6 +15,8 @@ import jadx.core.utils.Utils; ...@@ -15,6 +15,8 @@ import jadx.core.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
public class TryCatchBlock { public class TryCatchBlock {
...@@ -27,7 +29,7 @@ public class TryCatchBlock { ...@@ -27,7 +29,7 @@ public class TryCatchBlock {
private final CatchAttr attr; private final CatchAttr attr;
public TryCatchBlock() { public TryCatchBlock() {
handlers = new ArrayList<ExceptionHandler>(2); handlers = new LinkedList<ExceptionHandler>();
insns = new ArrayList<InsnNode>(); insns = new ArrayList<InsnNode>();
attr = new CatchAttr(this); attr = new CatchAttr(this);
} }
...@@ -45,9 +47,10 @@ public class TryCatchBlock { ...@@ -45,9 +47,10 @@ public class TryCatchBlock {
} }
public void removeHandler(MethodNode mth, ExceptionHandler handler) { public void removeHandler(MethodNode mth, ExceptionHandler handler) {
for (int i = 0; i < handlers.size(); i++) { for (Iterator<ExceptionHandler> it = handlers.iterator(); it.hasNext(); ) {
if (handlers.get(i) == handler) { ExceptionHandler h = it.next();
handlers.remove(i); if (h == handler) {
it.remove();
break; break;
} }
} }
...@@ -70,17 +73,17 @@ public class TryCatchBlock { ...@@ -70,17 +73,17 @@ public class TryCatchBlock {
} }
} }
} }
return; } else {
}
// self destruction // self destruction
for (InsnNode insn : insns) for (InsnNode insn : insns) {
insn.getAttributes().remove(attr); insn.getAttributes().remove(attr);
}
insns.clear(); insns.clear();
for (BlockNode block : mth.getBasicBlocks()) for (BlockNode block : mth.getBasicBlocks()) {
block.getAttributes().remove(attr); block.getAttributes().remove(attr);
} }
}
}
public void addInsn(InsnNode insn) { public void addInsn(InsnNode insn) {
insns.add(insn); insns.add(insn);
...@@ -113,34 +116,34 @@ public class TryCatchBlock { ...@@ -113,34 +116,34 @@ public class TryCatchBlock {
} }
public void setFinalBlockFromInsns(MethodNode mth, List<InsnNode> insns) { public void setFinalBlockFromInsns(MethodNode mth, List<InsnNode> insns) {
InsnContainer cont = new InsnContainer();
List<InsnNode> finalBlockInsns = new ArrayList<InsnNode>(insns); List<InsnNode> finalBlockInsns = new ArrayList<InsnNode>(insns);
cont.setInstructions(finalBlockInsns); setFinalBlock(new InsnContainer(finalBlockInsns));
setFinalBlock(cont);
InstructionRemover.unbindInsnList(finalBlockInsns); InstructionRemover.unbindInsnList(finalBlockInsns);
// remove these instructions from other handlers // remove these instructions from other handlers
for (ExceptionHandler h : getHandlers()) { for (ExceptionHandler h : getHandlers()) {
for (BlockNode ehb : h.getBlocks()) for (BlockNode ehb : h.getBlocks()) {
ehb.getInstructions().removeAll(finalBlockInsns); ehb.getInstructions().removeAll(finalBlockInsns);
} }
}
// remove from blocks with this catch // remove from blocks with this catch
for (BlockNode b : mth.getBasicBlocks()) { for (BlockNode b : mth.getBasicBlocks()) {
IAttribute ca = b.getAttributes().get(AttributeType.CATCH_BLOCK); IAttribute ca = b.getAttributes().get(AttributeType.CATCH_BLOCK);
if (attr == ca) if (attr == ca) {
b.getInstructions().removeAll(finalBlockInsns); b.getInstructions().removeAll(finalBlockInsns);
} }
} }
}
public void merge(MethodNode mth, TryCatchBlock tryBlock) { public void merge(MethodNode mth, TryCatchBlock tryBlock) {
for (InsnNode insn : tryBlock.getInsns()) for (InsnNode insn : tryBlock.getInsns()) {
this.addInsn(insn); this.addInsn(insn);
}
this.handlers.addAll(tryBlock.getHandlers()); this.handlers.addAll(tryBlock.getHandlers());
for (ExceptionHandler eh : handlers) for (ExceptionHandler eh : handlers) {
eh.setTryBlock(this); eh.setTryBlock(this);
}
// clear // clear
tryBlock.handlers.clear(); tryBlock.handlers.clear();
tryBlock.removeWholeBlock(mth); tryBlock.removeWholeBlock(mth);
...@@ -148,25 +151,23 @@ public class TryCatchBlock { ...@@ -148,25 +151,23 @@ public class TryCatchBlock {
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; return handlers.hashCode();
int result = 1;
result = prime * result + ((handlers == null) ? 0 : handlers.hashCode());
return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) {
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TryCatchBlock other = (TryCatchBlock) obj;
if (!handlers.equals(other.handlers)) return false;
return true; return true;
} }
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TryCatchBlock other = (TryCatchBlock) obj;
return handlers.equals(other.handlers);
}
@Override @Override
public String toString() { public String toString() {
return "Catch:{ " + Utils.listToString(handlers) + " }"; return "Catch:{ " + Utils.listToString(handlers) + " }";
} }
} }
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) {"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment