Search in sources :

Example 6 with JMethod

use of jadx.gui.treemodel.JMethod 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 7 with JMethod

use of jadx.gui.treemodel.JMethod in project jadx by skylot.

the class XposedAction method generateMethodSnippet.

private String generateMethodSnippet(JMethod jMth) {
    JavaMethod javaMethod = jMth.getJavaMethod();
    MethodNode mth = javaMethod.getMethodNode();
    String methodName;
    String xposedMethod;
    if (mth.isConstructor()) {
        xposedMethod = "findAndHookConstructor";
        methodName = "";
    } else {
        xposedMethod = "findAndHookMethod";
        methodName = "\"" + mth.getMethodInfo().getName() + "\", ";
    }
    String rawClassName = javaMethod.getDeclaringClass().getRawName();
    String xposedFormatStr = "XposedHelpers.%s(\"%s\", classLoader, %snew XC_MethodHook() {\n" + "    @Override\n" + "    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {\n" + "        super.beforeHookedMethod(param);\n" + "    }\n" + "    @Override\n" + "    protected void afterHookedMethod(MethodHookParam param) throws Throwable {\n" + "        super.afterHookedMethod(param);\n" + "    }\n" + "});";
    List<ArgType> mthArgs = mth.getArgTypes();
    if (mthArgs.isEmpty()) {
        return String.format(xposedFormatStr, xposedMethod, rawClassName, methodName);
    }
    String params = mthArgs.stream().map(type -> type + ".class, ").collect(Collectors.joining());
    return String.format(xposedFormatStr, xposedMethod, rawClassName, methodName + params);
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) KeyStroke(javax.swing.KeyStroke) ArgType(jadx.core.dex.instructions.args.ArgType) JMethod(jadx.gui.treemodel.JMethod) MethodNode(jadx.core.dex.nodes.MethodNode) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) KeyEvent(java.awt.event.KeyEvent) JOptionPane(javax.swing.JOptionPane) JavaClass(jadx.api.JavaClass) NLS(jadx.gui.utils.NLS) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) KeyStroke.getKeyStroke(javax.swing.KeyStroke.getKeyStroke) AbstractAction(javax.swing.AbstractAction) UiUtils(jadx.gui.utils.UiUtils) JNode(jadx.gui.treemodel.JNode) JClass(jadx.gui.treemodel.JClass) JavaMethod(jadx.api.JavaMethod) MethodNode(jadx.core.dex.nodes.MethodNode) JavaMethod(jadx.api.JavaMethod)

Aggregations

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