Search in sources :

Example 1 with JadxCodeData

use of jadx.api.data.impl.JadxCodeData in project jadx by skylot.

the class TestCodeCommentsMultiline method test.

@Test
public void test() {
    printOffsets();
    String baseClsId = TestCls.class.getName();
    JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test(Z)I");
    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));
    getArgs().setCodeData(codeData);
    assertThat(getClassNode(TestCls.class)).code().containsOne("// multi").containsOne("// line").containsOne("// comment");
}
Also used : ICodeComment(jadx.api.data.ICodeComment) JadxCodeComment(jadx.api.data.impl.JadxCodeComment) IJavaCodeRef(jadx.api.data.IJavaCodeRef) JadxNodeRef(jadx.api.data.impl.JadxNodeRef) JadxCodeData(jadx.api.data.impl.JadxCodeData) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Example 2 with JadxCodeData

use of jadx.api.data.impl.JadxCodeData in project jadx by skylot.

the class TestCodeComments method test.

@Test
public void test() {
    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 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");
    IJavaCodeRef insnRef = JadxCodeRef.forInsn(isJavaInput() ? 13 : 11);
    ICodeComment insnComment = new JadxCodeComment(mthRef, insnRef, "insn comment");
    JadxCodeData codeData = new JadxCodeData();
    getArgs().setCodeData(codeData);
    codeData.setComments(Arrays.asList(clsComment, innerClsComment, fldComment, mthComment, insnComment));
    ClassNode cls = getClassNode(TestCls.class);
    assertThat(cls).decompile().checkCodeOffsets().code().containsOne("// class comment").containsOne("// inner class comment").containsOne("// field comment").containsOne("// method comment").containsOne("System.out.println(\"comment\"); // insn comment");
    String code = cls.getCode().getCodeStr();
    assertThat(cls).reloadCode(this).isEqualTo(code);
    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").doesNotContain("class comment").containsOne(" comment");
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ICodeComment(jadx.api.data.ICodeComment) JadxCodeComment(jadx.api.data.impl.JadxCodeComment) IJavaCodeRef(jadx.api.data.IJavaCodeRef) JadxNodeRef(jadx.api.data.impl.JadxNodeRef) JadxCodeData(jadx.api.data.impl.JadxCodeData) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Example 3 with JadxCodeData

use of jadx.api.data.impl.JadxCodeData in project jadx by skylot.

the class TestCodeCommentsOverride method test.

@Test
public void test() {
    String baseClsId = TestCls.class.getName();
    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");
    ICodeComment mthComment = new JadxCodeComment(mthRef, "mth comment");
    JadxCodeData codeData = new JadxCodeData();
    codeData.setComments(Arrays.asList(iMthComment, mthComment));
    getArgs().setCodeData(codeData);
    ClassNode cls = getClassNode(TestCls.class);
    assertThat(cls).decompile().checkCodeOffsets().code().containsOne("@Override").containsOne("// " + iMthComment.getComment()).containsOne("// " + mthComment.getComment());
    assertThat(cls).reloadCode(this).containsOne("@Override").containsOne("// " + iMthComment.getComment()).containsOne("// " + mthComment.getComment());
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ICodeComment(jadx.api.data.ICodeComment) JadxCodeComment(jadx.api.data.impl.JadxCodeComment) JadxNodeRef(jadx.api.data.impl.JadxNodeRef) JadxCodeData(jadx.api.data.impl.JadxCodeData) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Example 4 with JadxCodeData

use of jadx.api.data.impl.JadxCodeData in project jadx by skylot.

the class CommentDialog method updateCommentsData.

private static void updateCommentsData(CodeArea codeArea, Consumer<List<ICodeComment>> updater) {
    try {
        JadxProject project = codeArea.getProject();
        JadxCodeData codeData = project.getCodeData();
        if (codeData == null) {
            codeData = new JadxCodeData();
        }
        List<ICodeComment> list = new ArrayList<>(codeData.getComments());
        updater.accept(list);
        Collections.sort(list);
        codeData.setComments(list);
        project.setCodeData(codeData);
        codeArea.getMainWindow().getWrapper().getDecompiler().reloadCodeData();
    } catch (Exception e) {
        LOG.error("Comment action failed", e);
    }
    try {
        // refresh code
        codeArea.refreshClass();
    } catch (Exception e) {
        LOG.error("Failed to reload code", e);
    }
}
Also used : ICodeComment(jadx.api.data.ICodeComment) ArrayList(java.util.ArrayList) JadxProject(jadx.gui.settings.JadxProject) JadxCodeData(jadx.api.data.impl.JadxCodeData)

Example 5 with JadxCodeData

use of jadx.api.data.impl.JadxCodeData in project jadx by skylot.

the class CommentDialog method searchForExistComment.

private static ICodeComment searchForExistComment(CodeArea codeArea, ICodeComment blankComment) {
    try {
        JadxProject project = codeArea.getProject();
        JadxCodeData codeData = project.getCodeData();
        if (codeData == null || codeData.getComments().isEmpty()) {
            return null;
        }
        for (ICodeComment comment : codeData.getComments()) {
            if (Objects.equals(comment.getNodeRef(), blankComment.getNodeRef()) && Objects.equals(comment.getCodeRef(), blankComment.getCodeRef())) {
                return comment;
            }
        }
    } catch (Exception e) {
        LOG.error("Error searching for exists comment", e);
    }
    return null;
}
Also used : ICodeComment(jadx.api.data.ICodeComment) JadxProject(jadx.gui.settings.JadxProject) JadxCodeData(jadx.api.data.impl.JadxCodeData)

Aggregations

JadxCodeData (jadx.api.data.impl.JadxCodeData)9 ICodeComment (jadx.api.data.ICodeComment)7 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)6 IntegrationTest (jadx.tests.api.IntegrationTest)6 Test (org.junit.jupiter.api.Test)6 IJavaCodeRef (jadx.api.data.IJavaCodeRef)5 JadxCodeComment (jadx.api.data.impl.JadxCodeComment)5 ClassNode (jadx.core.dex.nodes.ClassNode)3 JadxProject (jadx.gui.settings.JadxProject)3 ArrayList (java.util.ArrayList)3 ICodeRename (jadx.api.data.ICodeRename)2 JadxCodeRef (jadx.api.data.impl.JadxCodeRef)1 JadxCodeRename (jadx.api.data.impl.JadxCodeRename)1 HashSet (java.util.HashSet)1