Search in sources :

Example 1 with JadxCodeComment

use of jadx.api.data.impl.JadxCodeComment 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 JadxCodeComment

use of jadx.api.data.impl.JadxCodeComment 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 JadxCodeComment

use of jadx.api.data.impl.JadxCodeComment 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 JadxCodeComment

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

the class CommentAction method getCommentRef.

/**
 * Check if possible insert comment at current line.
 *
 * @return blank code comment object (comment string empty)
 */
@Nullable
private ICodeComment getCommentRef(int line) {
    if (line == -1 || this.topCls == null) {
        return null;
    }
    try {
        // TODO: cache and update on class refresh
        CodeLinesInfo linesInfo = new CodeLinesInfo(topCls, true);
        // add comment if node definition at this line
        JavaNode nodeAtLine = linesInfo.getDefAtLine(line);
        if (nodeAtLine != null) {
            // at node definition -> add comment for it
            JadxNodeRef nodeRef = JadxNodeRef.forJavaNode(nodeAtLine);
            return new JadxCodeComment(nodeRef, "");
        }
        Object ann = topCls.getAnnotationAt(new CodePosition(line));
        if (ann == null) {
            // check if line with comment above node definition
            try {
                JavaNode defNode = linesInfo.getJavaNodeBelowLine(line);
                if (defNode != null) {
                    String lineStr = codeArea.getLineText(line).trim();
                    if (lineStr.startsWith("//")) {
                        return new JadxCodeComment(JadxNodeRef.forJavaNode(defNode), "");
                    }
                }
            } catch (Exception e) {
                LOG.error("Failed to check comment line: " + line, e);
            }
            return null;
        }
        // try to add method line comment
        JavaNode node = linesInfo.getJavaNodeByLine(line);
        if (node instanceof JavaMethod) {
            JadxNodeRef nodeRef = JadxNodeRef.forMth((JavaMethod) node);
            if (ann instanceof InsnCodeOffset) {
                int rawOffset = ((InsnCodeOffset) ann).getOffset();
                return new JadxCodeComment(nodeRef, JadxCodeRef.forInsn(rawOffset), "");
            }
        }
    } catch (Exception e) {
        LOG.error("Failed to add comment at line: " + line, e);
    }
    return null;
}
Also used : CodePosition(jadx.api.CodePosition) JavaMethod(jadx.api.JavaMethod) InsnCodeOffset(jadx.api.data.annotations.InsnCodeOffset) JadxCodeComment(jadx.api.data.impl.JadxCodeComment) CodeLinesInfo(jadx.gui.utils.CodeLinesInfo) JadxNodeRef(jadx.api.data.impl.JadxNodeRef) JavaNode(jadx.api.JavaNode) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with JadxCodeComment

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

the class CommentDialog method apply.

private void apply() {
    String newCommentStr = commentArea.getText().trim();
    if (newCommentStr.isEmpty()) {
        if (updateComment) {
            remove();
        } else {
            cancel();
        }
        return;
    }
    ICodeComment newComment = new JadxCodeComment(comment.getNodeRef(), comment.getCodeRef(), newCommentStr);
    if (updateComment) {
        updateCommentsData(codeArea, list -> {
            list.remove(comment);
            list.add(newComment);
        });
    } else {
        updateCommentsData(codeArea, list -> list.add(newComment));
    }
    dispose();
}
Also used : ICodeComment(jadx.api.data.ICodeComment) JadxCodeComment(jadx.api.data.impl.JadxCodeComment)

Aggregations

JadxCodeComment (jadx.api.data.impl.JadxCodeComment)7 ICodeComment (jadx.api.data.ICodeComment)6 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)6 JadxCodeData (jadx.api.data.impl.JadxCodeData)5 IntegrationTest (jadx.tests.api.IntegrationTest)5 Test (org.junit.jupiter.api.Test)5 IJavaCodeRef (jadx.api.data.IJavaCodeRef)4 ClassNode (jadx.core.dex.nodes.ClassNode)2 CodePosition (jadx.api.CodePosition)1 JavaMethod (jadx.api.JavaMethod)1 JavaNode (jadx.api.JavaNode)1 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)1 CodeLinesInfo (jadx.gui.utils.CodeLinesInfo)1 Nullable (org.jetbrains.annotations.Nullable)1