Search in sources :

Example 1 with CodeLinesInfo

use of jadx.gui.utils.CodeLinesInfo in project jadx by skylot.

the class IndexService method indexCls.

/**
 * Warning! Not ready for parallel execution. Use only in a single thread.
 */
public boolean indexCls(JavaClass cls) {
    try {
        TextSearchIndex index = cache.getTextIndex();
        if (index == null) {
            return false;
        }
        // get code from cache to avoid decompilation here
        String code = getCodeFromCache(cls);
        if (code == null) {
            return cls.isNoCode();
        }
        List<StringRef> lines = splitLines(code);
        CodeLinesInfo linesInfo = new CodeLinesInfo(cls);
        index.indexCode(cls, linesInfo, lines);
        index.indexNames(cls);
        indexSet.add(cls);
        return true;
    } catch (Exception e) {
        LOG.error("Index error in class: {}", cls.getFullName(), e);
        return false;
    }
}
Also used : TextSearchIndex(jadx.gui.utils.search.TextSearchIndex) StringRef(jadx.gui.utils.search.StringRef) CodeLinesInfo(jadx.gui.utils.CodeLinesInfo)

Example 2 with CodeLinesInfo

use of jadx.gui.utils.CodeLinesInfo 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 3 with CodeLinesInfo

use of jadx.gui.utils.CodeLinesInfo 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 4 with CodeLinesInfo

use of jadx.gui.utils.CodeLinesInfo in project jadx by skylot.

the class IndexJob method runJob.

protected void runJob() {
    JNodeCache nodeCache = cache.getNodeCache();
    final TextSearchIndex index = new TextSearchIndex(nodeCache);
    final CodeUsageInfo usageInfo = new CodeUsageInfo(nodeCache);
    cache.setTextIndex(index);
    cache.setUsageInfo(usageInfo);
    for (final JavaClass cls : wrapper.getClasses()) {
        addTask(new Runnable() {

            @Override
            public void run() {
                try {
                    index.indexNames(cls);
                    CodeLinesInfo linesInfo = new CodeLinesInfo(cls);
                    List<StringRef> lines = splitLines(cls);
                    usageInfo.processClass(cls, linesInfo, lines);
                    if (Utils.isFreeMemoryAvailable()) {
                        index.indexCode(cls, linesInfo, lines);
                    } else {
                        index.classCodeIndexSkipped(cls);
                    }
                } catch (Exception e) {
                    LOG.error("Index error in class: {}", cls.getFullName(), e);
                }
            }
        });
    }
}
Also used : CodeUsageInfo(jadx.gui.utils.CodeUsageInfo) JavaClass(jadx.api.JavaClass) List(java.util.List) JNodeCache(jadx.gui.utils.JNodeCache) TextSearchIndex(jadx.gui.utils.search.TextSearchIndex) CodeLinesInfo(jadx.gui.utils.CodeLinesInfo)

Aggregations

CodeLinesInfo (jadx.gui.utils.CodeLinesInfo)4 CodePosition (jadx.api.CodePosition)2 JavaClass (jadx.api.JavaClass)2 JavaNode (jadx.api.JavaNode)2 StringRef (jadx.gui.utils.search.StringRef)2 TextSearchIndex (jadx.gui.utils.search.TextSearchIndex)2 JavaMethod (jadx.api.JavaMethod)1 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)1 JadxCodeComment (jadx.api.data.impl.JadxCodeComment)1 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)1 CodeNode (jadx.gui.treemodel.CodeNode)1 JNode (jadx.gui.treemodel.JNode)1 CodeUsageInfo (jadx.gui.utils.CodeUsageInfo)1 JNodeCache (jadx.gui.utils.JNodeCache)1 List (java.util.List)1 Nullable (org.jetbrains.annotations.Nullable)1