Commit 380b73d1 authored by Skylot's avatar Skylot

core: sort methods by source line number

parent ef85e29a
...@@ -24,6 +24,7 @@ import jadx.core.utils.exceptions.CodegenException; ...@@ -24,6 +24,7 @@ import jadx.core.utils.exceptions.CodegenException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
...@@ -39,6 +40,13 @@ import com.android.dx.rop.code.AccessFlags; ...@@ -39,6 +40,13 @@ import com.android.dx.rop.code.AccessFlags;
public class ClassGen { public class ClassGen {
private static final Logger LOG = LoggerFactory.getLogger(ClassGen.class); private static final Logger LOG = LoggerFactory.getLogger(ClassGen.class);
public static final Comparator<MethodNode> METHOD_LINE_COMPARATOR = new Comparator<MethodNode>() {
@Override
public int compare(MethodNode a, MethodNode b) {
return Utils.compare(a.getSourceLine(), b.getSourceLine());
}
};
private final ClassNode cls; private final ClassNode cls;
private final ClassGen parentGen; private final ClassGen parentGen;
private final AnnotationGen annotationGen; private final AnnotationGen annotationGen;
...@@ -239,7 +247,8 @@ public class ClassGen { ...@@ -239,7 +247,8 @@ public class ClassGen {
} }
private void addMethods(CodeWriter code) { private void addMethods(CodeWriter code) {
for (MethodNode mth : cls.getMethods()) { List<MethodNode> methods = sortMethodsByLine(cls.getMethods());
for (MethodNode mth : methods) {
if (mth.contains(AFlag.DONT_GENERATE)) { if (mth.contains(AFlag.DONT_GENERATE)) {
continue; continue;
} }
...@@ -255,6 +264,12 @@ public class ClassGen { ...@@ -255,6 +264,12 @@ public class ClassGen {
} }
} }
private static List<MethodNode> sortMethodsByLine(List<MethodNode> methods) {
List<MethodNode> out = new ArrayList<MethodNode>(methods);
Collections.sort(out, METHOD_LINE_COMPARATOR);
return out;
}
private boolean isMethodsPresents() { private boolean isMethodsPresents() {
for (MethodNode mth : cls.getMethods()) { for (MethodNode mth : cls.getMethods()) {
if (!mth.contains(AFlag.DONT_GENERATE)) { if (!mth.contains(AFlag.DONT_GENERATE)) {
......
...@@ -90,4 +90,8 @@ public class Utils { ...@@ -90,4 +90,8 @@ public class Utils {
throwable.printStackTrace(pw); throwable.printStackTrace(pw);
return sw.getBuffer().toString(); return sw.getBuffer().toString();
} }
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
} }
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