Search in sources :

Example 1 with InsnCodeOffset

use of jadx.api.data.annotations.InsnCodeOffset 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 2 with InsnCodeOffset

use of jadx.api.data.annotations.InsnCodeOffset in project jadx by skylot.

the class JsonCodeGen method fillMthCode.

private List<JsonCodeLine> fillMthCode(MethodNode mth, MethodGen mthGen) {
    if (mth.isNoCode()) {
        return Collections.emptyList();
    }
    ICodeWriter cw = mth.root().makeCodeWriter();
    try {
        mthGen.addInstructions(cw);
    } catch (Exception e) {
        throw new JadxRuntimeException("Method generation error", e);
    }
    ICodeInfo code = cw.finish();
    String codeStr = code.getCodeStr();
    if (codeStr.isEmpty()) {
        return Collections.emptyList();
    }
    String[] lines = codeStr.split(ICodeWriter.NL);
    Map<Integer, Integer> lineMapping = code.getLineMapping();
    Map<CodePosition, Object> annotations = code.getAnnotations();
    long mthCodeOffset = mth.getMethodCodeOffset() + 16;
    int linesCount = lines.length;
    List<JsonCodeLine> codeLines = new ArrayList<>(linesCount);
    for (int i = 0; i < linesCount; i++) {
        String codeLine = lines[i];
        int line = i + 2;
        JsonCodeLine jsonCodeLine = new JsonCodeLine();
        jsonCodeLine.setCode(codeLine);
        jsonCodeLine.setSourceLine(lineMapping.get(line));
        Object obj = annotations.get(new CodePosition(line));
        if (obj instanceof InsnCodeOffset) {
            long offset = ((InsnCodeOffset) obj).getOffset();
            jsonCodeLine.setOffset("0x" + Long.toHexString(mthCodeOffset + offset * 2));
        }
        codeLines.add(jsonCodeLine);
    }
    return codeLines;
}
Also used : ArrayList(java.util.ArrayList) InsnCodeOffset(jadx.api.data.annotations.InsnCodeOffset) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) CodePosition(jadx.api.CodePosition) ICodeInfo(jadx.api.ICodeInfo) JsonCodeLine(jadx.core.codegen.json.cls.JsonCodeLine) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) ICodeWriter(jadx.api.ICodeWriter)

Example 3 with InsnCodeOffset

use of jadx.api.data.annotations.InsnCodeOffset in project jadx by skylot.

the class IntegrationTest method printCodeWithOffsets.

private void printCodeWithOffsets(ICodeInfo code) {
    String codeStr = code.getCodeStr();
    Map<CodePosition, Object> annotations = code.getAnnotations();
    String[] lines = codeStr.split(ICodeWriter.NL);
    for (int i = 0; i < lines.length; i++) {
        String line = lines[i];
        int curLine = i + 1;
        Object ann = annotations.get(new CodePosition(curLine, 0));
        String offsetStr = "";
        if (ann instanceof InsnCodeOffset) {
            int offset = ((InsnCodeOffset) ann).getOffset();
            offsetStr = "/* " + leftPad(String.valueOf(offset), 5) + " */";
        }
        System.out.println(rightPad(offsetStr, 12) + line);
    }
}
Also used : CodePosition(jadx.api.CodePosition) InsnCodeOffset(jadx.api.data.annotations.InsnCodeOffset) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

CodePosition (jadx.api.CodePosition)3 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)3 ICodeInfo (jadx.api.ICodeInfo)1 ICodeWriter (jadx.api.ICodeWriter)1 JavaMethod (jadx.api.JavaMethod)1 JavaNode (jadx.api.JavaNode)1 JadxCodeComment (jadx.api.data.impl.JadxCodeComment)1 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)1 JsonCodeLine (jadx.core.codegen.json.cls.JsonCodeLine)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 CodeLinesInfo (jadx.gui.utils.CodeLinesInfo)1 ArrayList (java.util.ArrayList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Nullable (org.jetbrains.annotations.Nullable)1