Search in sources :

Example 6 with JavaMethod

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

the class RenameDialog method buildRename.

@NotNull
private JadxCodeRename buildRename(JNode node, String newName, Set<ICodeRename> renames) {
    if (node instanceof JMethod) {
        JavaMethod javaMethod = ((JMethod) node).getJavaMethod();
        List<JavaMethod> relatedMethods = javaMethod.getOverrideRelatedMethods();
        if (!relatedMethods.isEmpty()) {
            for (JavaMethod relatedMethod : relatedMethods) {
                renames.remove(new JadxCodeRename(JadxNodeRef.forMth(relatedMethod), ""));
            }
        }
        return new JadxCodeRename(JadxNodeRef.forMth(javaMethod), newName);
    }
    if (node instanceof JField) {
        return new JadxCodeRename(JadxNodeRef.forFld(((JField) node).getJavaField()), newName);
    }
    if (node instanceof JClass) {
        return new JadxCodeRename(JadxNodeRef.forCls(((JClass) node).getCls()), newName);
    }
    if (node instanceof JPackage) {
        return new JadxCodeRename(JadxNodeRef.forPkg(((JPackage) node).getFullName()), newName);
    }
    if (node instanceof JVariable) {
        JavaVariable javaVar = ((JVariable) node).getJavaVarNode();
        return new JadxCodeRename(JadxNodeRef.forMth(javaVar.getMth()), JadxCodeRef.forVar(javaVar), newName);
    }
    throw new JadxRuntimeException("Failed to build rename node for: " + node);
}
Also used : JVariable(jadx.gui.treemodel.JVariable) JField(jadx.gui.treemodel.JField) JavaVariable(jadx.api.JavaVariable) JClass(jadx.gui.treemodel.JClass) JPackage(jadx.gui.treemodel.JPackage) JavaMethod(jadx.api.JavaMethod) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) JMethod(jadx.gui.treemodel.JMethod) JadxCodeRename(jadx.api.data.impl.JadxCodeRename) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with JavaMethod

use of jadx.api.JavaMethod 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 8 with JavaMethod

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

the class TextSearchIndex method indexNames.

public void indexNames(JavaClass cls) {
    clsNamesIndex.put(cls.getFullName(), nodeCache.makeFrom(cls));
    for (JavaMethod mth : cls.getMethods()) {
        JNode mthNode = nodeCache.makeFrom(mth);
        mthSignaturesIndex.put(mthNode.makeDescString(), mthNode);
    }
    for (JavaField fld : cls.getFields()) {
        JNode fldNode = nodeCache.makeFrom(fld);
        fldSignaturesIndex.put(fldNode.makeDescString(), fldNode);
    }
    for (JavaClass innerCls : cls.getInnerClasses()) {
        indexNames(innerCls);
    }
}
Also used : JavaField(jadx.api.JavaField) JavaClass(jadx.api.JavaClass) JavaMethod(jadx.api.JavaMethod) JNode(jadx.gui.treemodel.JNode)

Example 9 with JavaMethod

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

the class FridaAction method generateMethodSnippet.

private String generateMethodSnippet(JMethod jMth) {
    JavaMethod javaMethod = jMth.getJavaMethod();
    MethodInfo methodInfo = javaMethod.getMethodNode().getMethodInfo();
    String methodName = methodInfo.getName();
    if (methodInfo.isConstructor()) {
        methodName = "$init";
    }
    String rawClassName = javaMethod.getDeclaringClass().getRawName();
    String shortClassName = javaMethod.getDeclaringClass().getName();
    String functionUntilImplementation;
    if (isOverloaded(javaMethod.getMethodNode())) {
        List<ArgType> methodArgs = methodInfo.getArgumentsTypes();
        String overloadStr = methodArgs.stream().map(this::parseArgType).collect(Collectors.joining(", "));
        functionUntilImplementation = String.format("%s.%s.overload(%s).implementation", shortClassName, methodName, overloadStr);
    } else {
        functionUntilImplementation = String.format("%s.%s.implementation", shortClassName, methodName);
    }
    String functionParametersString = Objects.requireNonNull(javaMethod.getTopParentClass().getCodeInfo()).getAnnotations().entrySet().stream().filter(e -> e.getKey().getLine() == jMth.getLine() && e.getValue() instanceof VarDeclareRef).sorted(Comparator.comparingInt(e -> e.getKey().getPos())).map(e -> ((VarDeclareRef) e.getValue()).getName()).collect(Collectors.joining(", "));
    String functionParameterAndBody = String.format("%s = function(%s){\n" + "    console.log('%s is called');\n" + "    let ret = this.%s(%s);\n" + "    console.log('%s ret value is ' + ret);\n" + "    return ret;\n" + "};", functionUntilImplementation, functionParametersString, methodName, methodName, functionParametersString, methodName);
    String finalFridaCode;
    if (isInitial.getOrDefault(rawClassName, true)) {
        String classSnippet = generateClassSnippet(jMth.getJParent());
        finalFridaCode = classSnippet + "\n" + functionParameterAndBody;
    } else {
        finalFridaCode = functionParameterAndBody;
    }
    return finalFridaCode;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) ArgType(jadx.core.dex.instructions.args.ArgType) java.util(java.util) MethodNode(jadx.core.dex.nodes.MethodNode) LoggerFactory(org.slf4j.LoggerFactory) NLS(jadx.gui.utils.NLS) JField(jadx.gui.treemodel.JField) TypeGen(jadx.core.codegen.TypeGen) KeyStroke.getKeyStroke(javax.swing.KeyStroke.getKeyStroke) ClassNode(jadx.core.dex.nodes.ClassNode) UiUtils(jadx.gui.utils.UiUtils) JNode(jadx.gui.treemodel.JNode) JClass(jadx.gui.treemodel.JClass) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef) JMethod(jadx.gui.treemodel.JMethod) Logger(org.slf4j.Logger) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) KeyEvent(java.awt.event.KeyEvent) JavaClass(jadx.api.JavaClass) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) MethodInfo(jadx.core.dex.info.MethodInfo) Nullable(org.jetbrains.annotations.Nullable) JavaMethod(jadx.api.JavaMethod) JavaField(jadx.api.JavaField) javax.swing(javax.swing) JavaMethod(jadx.api.JavaMethod) MethodInfo(jadx.core.dex.info.MethodInfo) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef)

Example 10 with JavaMethod

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

the class CodeLinesInfo method addClass.

private void addClass(JavaClass cls, boolean includeFields) {
    map.put(cls.getDecompiledLine(), cls);
    for (JavaClass innerCls : cls.getInnerClasses()) {
        map.put(innerCls.getDecompiledLine(), innerCls);
        addClass(innerCls, includeFields);
    }
    for (JavaMethod mth : cls.getMethods()) {
        map.put(mth.getDecompiledLine(), mth);
    }
    if (includeFields) {
        for (JavaField field : cls.getFields()) {
            map.put(field.getDecompiledLine(), field);
        }
    }
}
Also used : JavaField(jadx.api.JavaField) JavaClass(jadx.api.JavaClass) JavaMethod(jadx.api.JavaMethod)

Aggregations

JavaMethod (jadx.api.JavaMethod)11 JavaClass (jadx.api.JavaClass)9 JavaField (jadx.api.JavaField)6 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)4 JMethod (jadx.gui.treemodel.JMethod)4 Nullable (org.jetbrains.annotations.Nullable)4 JClass (jadx.gui.treemodel.JClass)3 JField (jadx.gui.treemodel.JField)3 JNode (jadx.gui.treemodel.JNode)3 JavaVariable (jadx.api.JavaVariable)2 ArgType (jadx.core.dex.instructions.args.ArgType)2 MethodNode (jadx.core.dex.nodes.MethodNode)2 JVariable (jadx.gui.treemodel.JVariable)2 NLS (jadx.gui.utils.NLS)2 UiUtils (jadx.gui.utils.UiUtils)2 ActionEvent (java.awt.event.ActionEvent)2 KeyEvent (java.awt.event.KeyEvent)2 Collectors (java.util.stream.Collectors)2 KeyStroke.getKeyStroke (javax.swing.KeyStroke.getKeyStroke)2 Logger (org.slf4j.Logger)2