Search in sources :

Example 6 with CodePosition

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

the class UsageDialog method processUsageClass.

private void processUsageClass(JavaNode usageNode) {
    JavaClass cls = usageNode.getTopParentClass();
    String code = cls.getCodeInfo().getCodeStr();
    CodeLinesInfo linesInfo = new CodeLinesInfo(cls);
    List<? extends JavaNode> targetNodes = getMethodWithOverride();
    for (JavaNode javaNode : targetNodes) {
        List<CodePosition> usage = cls.getUsageFor(javaNode);
        for (CodePosition pos : usage) {
            if (javaNode.getTopParentClass().equals(cls) && pos.getPos() == javaNode.getDefPos()) {
                // skip declaration
                continue;
            }
            StringRef line = getLineStrAt(code, pos.getPos());
            if (line.startsWith("import ")) {
                continue;
            }
            JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(pos.getLine());
            JNode useAtNode = javaNodeByLine == null ? node : getNodeCache().makeFrom(javaNodeByLine);
            usageList.add(new CodeNode(useAtNode, line, pos.getLine(), pos.getPos()));
        }
    }
}
Also used : CodePosition(jadx.api.CodePosition) JavaClass(jadx.api.JavaClass) CodeNode(jadx.gui.treemodel.CodeNode) CodeLinesInfo(jadx.gui.utils.CodeLinesInfo) StringRef(jadx.gui.utils.search.StringRef) JNode(jadx.gui.treemodel.JNode) JavaNode(jadx.api.JavaNode)

Example 7 with CodePosition

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

the class CodeWriter method add.

CodeWriter add(CodeWriter code) {
    line--;
    for (Map.Entry<CodePosition, Object> entry : code.annotations.entrySet()) {
        CodePosition pos = entry.getKey();
        attachAnnotation(entry.getValue(), new CodePosition(line + pos.getLine(), pos.getOffset()));
    }
    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) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 8 with CodePosition

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

the class CodeWriter method attachDefinition.

public void attachDefinition(LineAttrNode obj) {
    attachAnnotation(obj);
    attachAnnotation(new DefinitionWrapper(obj), new CodePosition(line, offset));
}
Also used : CodePosition(jadx.api.CodePosition)

Example 9 with CodePosition

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

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

the class AnnotatedCodeWriter method processDefinitionAnnotations.

private void processDefinitionAnnotations() {
    if (!annotations.isEmpty()) {
        annotations.entrySet().removeIf(entry -> {
            Object v = entry.getValue();
            if (v instanceof DefinitionWrapper) {
                ILineAttributeNode l = ((DefinitionWrapper) v).getNode();
                CodePosition codePos = entry.getKey();
                l.setDecompiledLine(codePos.getLine());
                l.setDefPosition(codePos.getPos());
                return true;
            }
            return false;
        });
    }
}
Also used : ILineAttributeNode(jadx.core.dex.attributes.ILineAttributeNode) CodePosition(jadx.api.CodePosition)

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