Search in sources :

Example 11 with CodePosition

use of jadx.api.CodePosition in project jadx by skylot.

the class AnnotatedCodeWriter method add.

@Override
public ICodeWriter add(ICodeWriter cw) {
    if ((!(cw instanceof AnnotatedCodeWriter))) {
        buf.append(cw.getCodeStr());
        return this;
    }
    AnnotatedCodeWriter code = ((AnnotatedCodeWriter) cw);
    line--;
    int startLine = line;
    int startPos = getLength();
    for (Map.Entry<CodePosition, Object> entry : code.annotations.entrySet()) {
        CodePosition codePos = entry.getKey();
        int newLine = startLine + codePos.getLine();
        int newPos = startPos + codePos.getPos();
        attachAnnotation(entry.getValue(), new CodePosition(newLine, codePos.getOffset(), newPos));
    }
    for (Map.Entry<Integer, Integer> entry : code.lineMap.entrySet()) {
        attachSourceLine(line + entry.getKey(), entry.getValue());
    }
    line += code.line;
    offset = code.offset;
    buf.append(code.buf);
    return this;
}
Also used : CodePosition(jadx.api.CodePosition) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with CodePosition

use of jadx.api.CodePosition 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)

Example 13 with CodePosition

use of jadx.api.CodePosition in project jadx by skylot.

the class CaretPositionFix method save.

/**
 * Save caret position by anchor to token under caret
 */
public void save() {
    try {
        linesCount = codeArea.getLineCount();
        int pos = codeArea.getCaretPosition();
        line = codeArea.getLineOfOffset(pos);
        lineOffset = pos - codeArea.getLineStartOffset(line);
        tokenInfo = getTokenInfoByOffset(codeArea.getTokenListForLine(line), pos);
        JClass cls = codeArea.getJClass();
        if (cls != null) {
            JavaClass topParentClass = cls.getJavaNode().getTopParentClass();
            Object ann = topParentClass.getAnnotationAt(new CodePosition(line));
            if (ann instanceof ICodeRawOffset) {
                codeRawOffset = ((ICodeRawOffset) ann).getOffset();
                CodeLinesInfo codeLinesInfo = new CodeLinesInfo(topParentClass);
                JavaNode javaNodeAtLine = codeLinesInfo.getJavaNodeByLine(line);
                if (javaNodeAtLine != null) {
                    javaNodeLine = javaNodeAtLine.getDecompiledLine();
                }
            }
        }
        LOG.debug("Saved position data: line={}, lineOffset={}, token={}, codeRawOffset={}, javaNodeLine={}", line, lineOffset, tokenInfo, codeRawOffset, javaNodeLine);
    } catch (Exception e) {
        LOG.error("Failed to save caret position before refresh", e);
        line = -1;
    }
}
Also used : CodePosition(jadx.api.CodePosition) JavaClass(jadx.api.JavaClass) JClass(jadx.gui.treemodel.JClass) ICodeRawOffset(jadx.api.data.annotations.ICodeRawOffset) JavaNode(jadx.api.JavaNode)

Example 14 with CodePosition

use of jadx.api.CodePosition in project jadx by skylot.

the class CodeArea method buildCodePosFromOffset.

private CodePosition buildCodePosFromOffset(int offset) throws BadLocationException {
    int start = getWordStart(offset);
    if (start == -1) {
        start = offset;
    }
    int line = getLineOfOffset(start);
    int lineOffset = start - getLineStartOffset(line);
    return new CodePosition(line + 1, lineOffset + 1, start);
}
Also used : CodePosition(jadx.api.CodePosition) Point(java.awt.Point)

Example 15 with CodePosition

use of jadx.api.CodePosition in project jadx by skylot.

the class CodeArea method getDefPosForNodeAtOffset.

/**
 * Search node by offset in {@code jCls} code and return its definition position
 * (useful for jumps from usage)
 */
@Nullable
public JumpPosition getDefPosForNodeAtOffset(int offset) {
    if (offset == -1) {
        return null;
    }
    JavaNode foundNode = getJavaNodeAtOffset(offset);
    if (foundNode == null) {
        return null;
    }
    CodePosition pos = getDecompiler().getDefinitionPosition(foundNode);
    if (pos == null) {
        return null;
    }
    JNode jNode = convertJavaNode(foundNode);
    return new JumpPosition(jNode.getRootClass(), pos);
}
Also used : CodePosition(jadx.api.CodePosition) JumpPosition(jadx.gui.utils.JumpPosition) JNode(jadx.gui.treemodel.JNode) JavaNode(jadx.api.JavaNode) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CodePosition (jadx.api.CodePosition)15 JavaNode (jadx.api.JavaNode)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)3 TreeMap (java.util.TreeMap)3 JavaClass (jadx.api.JavaClass)2 JNode (jadx.gui.treemodel.JNode)2 CodeLinesInfo (jadx.gui.utils.CodeLinesInfo)2 Nullable (org.jetbrains.annotations.Nullable)2 ICodeInfo (jadx.api.ICodeInfo)1 ICodeWriter (jadx.api.ICodeWriter)1 JavaMethod (jadx.api.JavaMethod)1 ICodeRawOffset (jadx.api.data.annotations.ICodeRawOffset)1 JadxCodeComment (jadx.api.data.impl.JadxCodeComment)1 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)1 JsonCodeLine (jadx.core.codegen.json.cls.JsonCodeLine)1 ILineAttributeNode (jadx.core.dex.attributes.ILineAttributeNode)1 LineAttrNode (jadx.core.dex.attributes.nodes.LineAttrNode)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1