Commit ca448fc4 authored by Skylot's avatar Skylot

core: fix variable definitions for 'try' blocks

parent 7a51c0d0
...@@ -5,7 +5,7 @@ import jadx.core.dex.nodes.IRegion; ...@@ -5,7 +5,7 @@ import jadx.core.dex.nodes.IRegion;
public abstract class AbstractRegion extends AttrNode implements IRegion { public abstract class AbstractRegion extends AttrNode implements IRegion {
private final IRegion parent; private IRegion parent;
public AbstractRegion(IRegion parent) { public AbstractRegion(IRegion parent) {
this.parent = parent; this.parent = parent;
...@@ -16,4 +16,7 @@ public abstract class AbstractRegion extends AttrNode implements IRegion { ...@@ -16,4 +16,7 @@ public abstract class AbstractRegion extends AttrNode implements IRegion {
return parent; return parent;
} }
public void setParent(IRegion parent) {
this.parent = parent;
}
} }
...@@ -5,6 +5,7 @@ import jadx.core.dex.nodes.BlockNode; ...@@ -5,6 +5,7 @@ import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IContainer; import jadx.core.dex.nodes.IContainer;
import jadx.core.dex.nodes.IRegion; import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.regions.AbstractRegion;
import jadx.core.dex.regions.Region; import jadx.core.dex.regions.Region;
import jadx.core.dex.trycatch.CatchAttr; import jadx.core.dex.trycatch.CatchAttr;
import jadx.core.dex.trycatch.ExceptionHandler; import jadx.core.dex.trycatch.ExceptionHandler;
...@@ -144,6 +145,14 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor { ...@@ -144,6 +145,14 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
region.getSubBlocks().removeAll(newRegion.getSubBlocks()); region.getSubBlocks().removeAll(newRegion.getSubBlocks());
newRegion.getAttributes().add(tb.getCatchAttr()); newRegion.getAttributes().add(tb.getCatchAttr());
// fix parents
for (IContainer cont : newRegion.getSubBlocks()) {
if (cont instanceof AbstractRegion) {
AbstractRegion aReg = (AbstractRegion) cont;
aReg.setParent(newRegion);
}
}
} }
} }
......
...@@ -60,7 +60,7 @@ public class ProcessVariables extends AbstractVisitor { ...@@ -60,7 +60,7 @@ public class ProcessVariables extends AbstractVisitor {
@Override @Override
public String toString() { public String toString() {
return arg + " " + assigns + " " + usage; return arg + ", a:" + assigns + ", u:" + usage;
} }
} }
......
...@@ -6,10 +6,16 @@ import jadx.core.dex.visitors.DepthTraverser; ...@@ -6,10 +6,16 @@ import jadx.core.dex.visitors.DepthTraverser;
import jadx.core.dex.visitors.IDexTreeVisitor; import jadx.core.dex.visitors.IDexTreeVisitor;
import jadx.core.utils.exceptions.DecodeException; import jadx.core.utils.exceptions.DecodeException;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
public class TestVariablesDefinitions extends InternalJadxTest { public class TestVariablesDefinitions extends InternalJadxTest {
public static class TestCls { public static class TestCls {
...@@ -20,23 +26,24 @@ public class TestVariablesDefinitions extends InternalJadxTest { ...@@ -20,23 +26,24 @@ public class TestVariablesDefinitions extends InternalJadxTest {
public void run() { public void run() {
try { try {
cls.load(); cls.load();
for (IDexTreeVisitor visitor : passes) { Iterator<IDexTreeVisitor> iterator = passes.iterator();
DepthTraverser.visit(visitor, cls); while (iterator.hasNext()) {
DepthTraverser.visit(iterator.next(), cls);
} }
} catch (DecodeException e) { } catch (DecodeException e) {
LOG.error("Decode exception: " + cls, e); LOG.error("Decode exception: " + cls, e);
} finally {
cls.unload();
} }
} }
} }
//@Test @Test
public void test() { public void test() {
ClassNode cls = getClassNode(TestCls.class); ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString(); String code = cls.getCode().toString();
System.out.println(code); System.out.println(code);
// 'iterator' variable must be declared inside 'try' block
assertThat(code, containsString(makeIndent(3) + "Iterator<IDexTreeVisitor> iterator = "));
assertThat(code, not(containsString("iterator;")));
} }
} }
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