This commit is contained in:
@@ -23,7 +23,7 @@ public class JadxCodeInfoAssertions extends AbstractObjectAssert<JadxCodeInfoAss
|
||||
|
||||
public JadxCodeInfoAssertions checkCodeOffsets() {
|
||||
long dupOffsetCount = actual.getAnnotations().values().stream()
|
||||
.filter(o -> o instanceof ICodeRawOffset)
|
||||
.filter(ICodeRawOffset.class::isInstance)
|
||||
.collect(Collectors.groupingBy(o -> ((ICodeRawOffset) o).getOffset(), Collectors.toList()))
|
||||
.values().stream()
|
||||
.filter(list -> list.size() > 1)
|
||||
|
||||
@@ -6,9 +6,11 @@ import java.util.Collections;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.api.data.ICodeComment;
|
||||
import jadx.api.data.IJavaCodeRef;
|
||||
import jadx.api.data.IJavaNodeRef.RefType;
|
||||
import jadx.api.data.impl.JadxCodeComment;
|
||||
import jadx.api.data.impl.JadxCodeData;
|
||||
import jadx.api.data.impl.JadxCodeRef;
|
||||
import jadx.api.data.impl.JadxNodeRef;
|
||||
import jadx.core.dex.nodes.ClassNode;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
@@ -17,6 +19,7 @@ import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
|
||||
|
||||
public class TestCodeComments extends IntegrationTest {
|
||||
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
public static class TestCls {
|
||||
private int intField = 5;
|
||||
|
||||
@@ -32,15 +35,14 @@ public class TestCodeComments extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int insnOffset = isJavaInput() ? 13 : 11;
|
||||
|
||||
String baseClsId = TestCls.class.getName();
|
||||
ICodeComment clsComment = new JadxCodeComment(JadxNodeRef.forCls(baseClsId), "class comment");
|
||||
ICodeComment innerClsComment = new JadxCodeComment(JadxNodeRef.forCls(baseClsId + ".A"), "inner class comment");
|
||||
ICodeComment innerClsComment = new JadxCodeComment(JadxNodeRef.forCls(baseClsId + "$A"), "inner class comment");
|
||||
ICodeComment fldComment = new JadxCodeComment(new JadxNodeRef(RefType.FIELD, baseClsId, "intField:I"), "field comment");
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test()I");
|
||||
ICodeComment mthComment = new JadxCodeComment(mthRef, "method comment");
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, "insn comment", insnOffset);
|
||||
IJavaCodeRef insnRef = JadxCodeRef.forInsn(isJavaInput() ? 13 : 11);
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, insnRef, "insn comment");
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
getArgs().setCodeData(codeData);
|
||||
@@ -62,8 +64,9 @@ public class TestCodeComments extends IntegrationTest {
|
||||
.reloadCode(this)
|
||||
.isEqualTo(code);
|
||||
|
||||
ICodeComment updInsnComment = new JadxCodeComment(mthRef, "updated insn comment", insnOffset);
|
||||
ICodeComment updInsnComment = new JadxCodeComment(mthRef, insnRef, "updated insn comment");
|
||||
codeData.setComments(Collections.singletonList(updInsnComment));
|
||||
jadxDecompiler.reloadCodeData();
|
||||
assertThat(cls)
|
||||
.reloadCode(this)
|
||||
.containsOne("System.out.println(\"comment\"); // updated insn comment")
|
||||
|
||||
@@ -5,9 +5,11 @@ import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.api.data.ICodeComment;
|
||||
import jadx.api.data.IJavaCodeRef;
|
||||
import jadx.api.data.IJavaNodeRef.RefType;
|
||||
import jadx.api.data.impl.JadxCodeComment;
|
||||
import jadx.api.data.impl.JadxCodeData;
|
||||
import jadx.api.data.impl.JadxCodeRef;
|
||||
import jadx.api.data.impl.JadxNodeRef;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
@@ -31,8 +33,10 @@ public class TestCodeComments2 extends IntegrationTest {
|
||||
|
||||
String baseClsId = TestCls.class.getName();
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test(Z)I");
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, "return comment", isJavaInput() ? 13 : 10);
|
||||
ICodeComment insnComment2 = new JadxCodeComment(mthRef, "another return comment", isJavaInput() ? 15 : 11);
|
||||
IJavaCodeRef insnRef = JadxCodeRef.forInsn(isJavaInput() ? 13 : 10);
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, insnRef, "return comment");
|
||||
IJavaCodeRef insnRef2 = JadxCodeRef.forInsn(isJavaInput() ? 15 : 11);
|
||||
ICodeComment insnComment2 = new JadxCodeComment(mthRef, insnRef2, "another return comment");
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
codeData.setComments(Arrays.asList(insnComment, insnComment2));
|
||||
|
||||
@@ -6,9 +6,11 @@ import java.util.Random;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.api.data.ICodeComment;
|
||||
import jadx.api.data.IJavaCodeRef;
|
||||
import jadx.api.data.IJavaNodeRef.RefType;
|
||||
import jadx.api.data.impl.JadxCodeComment;
|
||||
import jadx.api.data.impl.JadxCodeData;
|
||||
import jadx.api.data.impl.JadxCodeRef;
|
||||
import jadx.api.data.impl.JadxNodeRef;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
@@ -16,6 +18,7 @@ import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
|
||||
|
||||
public class TestCodeComments2a extends IntegrationTest {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static class TestCls {
|
||||
private int f;
|
||||
|
||||
@@ -34,8 +37,10 @@ public class TestCodeComments2a extends IntegrationTest {
|
||||
|
||||
String baseClsId = TestCls.class.getName();
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test(Z)I");
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, "return comment", isJavaInput() ? 22 : 18);
|
||||
ICodeComment insnComment2 = new JadxCodeComment(mthRef, "another return comment", isJavaInput() ? 27 : 19);
|
||||
IJavaCodeRef insnRef = JadxCodeRef.forInsn(isJavaInput() ? 22 : 18);
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, insnRef, "return comment");
|
||||
IJavaCodeRef insnRef2 = JadxCodeRef.forInsn(isJavaInput() ? 27 : 19);
|
||||
ICodeComment insnComment2 = new JadxCodeComment(mthRef, insnRef2, "another return comment");
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
codeData.setComments(Arrays.asList(insnComment, insnComment2));
|
||||
|
||||
+4
-1
@@ -5,9 +5,11 @@ import java.util.Collections;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.api.data.ICodeComment;
|
||||
import jadx.api.data.IJavaCodeRef;
|
||||
import jadx.api.data.IJavaNodeRef.RefType;
|
||||
import jadx.api.data.impl.JadxCodeComment;
|
||||
import jadx.api.data.impl.JadxCodeData;
|
||||
import jadx.api.data.impl.JadxCodeRef;
|
||||
import jadx.api.data.impl.JadxNodeRef;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
@@ -31,7 +33,8 @@ public class TestCodeCommentsMultiline extends IntegrationTest {
|
||||
|
||||
String baseClsId = TestCls.class.getName();
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test(Z)I");
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, "multi\nline\ncomment", isJavaInput() ? 15 : 11);
|
||||
IJavaCodeRef insnRef = JadxCodeRef.forInsn(isJavaInput() ? 15 : 11);
|
||||
ICodeComment insnComment = new JadxCodeComment(mthRef, insnRef, "multi\nline\ncomment");
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
codeData.setComments(Collections.singletonList(insnComment));
|
||||
|
||||
@@ -32,10 +32,10 @@ public class TestCodeCommentsOverride extends IntegrationTest {
|
||||
@Test
|
||||
public void test() {
|
||||
String baseClsId = TestCls.class.getName();
|
||||
JadxNodeRef iMthRef = new JadxNodeRef(RefType.METHOD, baseClsId + ".I", "mth()V");
|
||||
JadxNodeRef iMthRef = new JadxNodeRef(RefType.METHOD, baseClsId + "$I", "mth()V");
|
||||
ICodeComment iMthComment = new JadxCodeComment(iMthRef, "interface mth comment");
|
||||
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId + ".A", "mth()V");
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId + "$A", "mth()V");
|
||||
ICodeComment mthComment = new JadxCodeComment(mthRef, "mth comment");
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package jadx.tests.integration.rename;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.api.data.CodeRefType;
|
||||
import jadx.api.data.ICodeRename;
|
||||
import jadx.api.data.IJavaCodeRef;
|
||||
import jadx.api.data.IJavaNodeRef.RefType;
|
||||
import jadx.api.data.impl.JadxCodeData;
|
||||
import jadx.api.data.impl.JadxCodeRef;
|
||||
import jadx.api.data.impl.JadxCodeRename;
|
||||
import jadx.api.data.impl.JadxNodeRef;
|
||||
import jadx.core.dex.nodes.ClassNode;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
|
||||
|
||||
public class TestUserRenames extends IntegrationTest {
|
||||
|
||||
@SuppressWarnings({ "FieldCanBeLocal", "FieldMayBeFinal" })
|
||||
public static class TestCls {
|
||||
private int intField = 5;
|
||||
|
||||
public static class A {
|
||||
}
|
||||
|
||||
public int test(int x) {
|
||||
int y = x + "test".length();
|
||||
System.out.println(y);
|
||||
int z = y + 1;
|
||||
System.out.println(z);
|
||||
return z;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
getArgs().setDeobfuscationOn(false);
|
||||
|
||||
List<ICodeRename> renames = new ArrayList<>();
|
||||
String baseClsId = TestCls.class.getName();
|
||||
renames.add(new JadxCodeRename(JadxNodeRef.forPkg("jadx.tests"), "renamedPkgTests"));
|
||||
renames.add(new JadxCodeRename(JadxNodeRef.forPkg("jadx.tests.integration.rename"), "renamedPkgRename"));
|
||||
renames.add(new JadxCodeRename(JadxNodeRef.forCls(baseClsId), "RenamedTestCls"));
|
||||
renames.add(new JadxCodeRename(JadxNodeRef.forCls(baseClsId + "$A"), "RenamedInnerCls"));
|
||||
renames.add(new JadxCodeRename(new JadxNodeRef(RefType.FIELD, baseClsId, "intField:I"), "renamedField"));
|
||||
JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test(I)I");
|
||||
renames.add(new JadxCodeRename(mthRef, "renamedTestMth"));
|
||||
renames.add(new JadxCodeRename(mthRef, new JadxCodeRef(CodeRefType.MTH_ARG, 0), "renamedX"));
|
||||
JadxCodeRef varDeclareRef = isJavaInput() ? JadxCodeRef.forVar(0, 1) : JadxCodeRef.forVar(0, 0);
|
||||
renames.add(new JadxCodeRename(mthRef, varDeclareRef, "renamedY"));
|
||||
IJavaCodeRef varUseRef = isJavaInput() ? JadxCodeRef.forVar(0, 4) : JadxCodeRef.forVar(1, 0);
|
||||
renames.add(new JadxCodeRename(mthRef, varUseRef, "renamedZ"));
|
||||
|
||||
JadxCodeData codeData = new JadxCodeData();
|
||||
codeData.setRenames(renames);
|
||||
getArgs().setCodeData(codeData);
|
||||
|
||||
ClassNode cls = getClassNode(TestCls.class);
|
||||
assertThat(cls)
|
||||
.decompile()
|
||||
.checkCodeOffsets()
|
||||
.code()
|
||||
.containsOne("package jadx.renamedPkgTests.integration.renamedPkgRename;")
|
||||
.containsOne("public class RenamedTestCls {")
|
||||
.containsOne("private int renamedField")
|
||||
.containsOne("public static class RenamedInnerCls {")
|
||||
.containsOne("public int renamedTestMth(int renamedX) {")
|
||||
.containsOne("int renamedY = renamedX + \"test\".length();")
|
||||
.containsOne("int renamedZ = renamedY + 1;")
|
||||
.containsOne("return renamedZ;");
|
||||
|
||||
String code = cls.getCode().getCodeStr();
|
||||
assertThat(cls)
|
||||
.reloadCode(this)
|
||||
.isEqualTo(code);
|
||||
|
||||
ICodeRename updVarRename = new JadxCodeRename(mthRef, varUseRef, "anotherZ");
|
||||
codeData.setRenames(Collections.singletonList(updVarRename));
|
||||
jadxDecompiler.reloadCodeData();
|
||||
assertThat(cls)
|
||||
.reloadCode(this)
|
||||
.containsOne("int anotherZ = y + 1;")
|
||||
.doesNotContain("int z")
|
||||
.doesNotContain("int renamedZ");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user