Search in sources :

Example 1 with VarDeclareRef

use of jadx.api.data.annotations.VarDeclareRef 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 2 with VarDeclareRef

use of jadx.api.data.annotations.VarDeclareRef in project jadx by skylot.

the class TestVariablesDeclAnnotation method checkArgNamesInMethod.

private static void checkArgNamesInMethod(ClassNode cls, String mthName, String expectedVars) {
    MethodNode testMth = cls.searchMethodByShortName(mthName);
    assertThat(testMth).isNotNull();
    int mthLine = testMth.getDecompiledLine();
    List<String> argNames = cls.getCode().getAnnotations().entrySet().stream().filter(e -> e.getKey().getLine() == mthLine && e.getValue() instanceof VarDeclareRef).sorted(Comparator.comparingInt(e -> e.getKey().getPos())).map(e -> ((VarDeclareRef) e.getValue()).getName()).collect(Collectors.toList());
    assertThat(argNames).doesNotContainNull();
    assertThat(argNames.toString()).isEqualTo(expectedVars);
}
Also used : Test(org.junit.jupiter.api.Test) List(java.util.List) MethodNode(jadx.core.dex.nodes.MethodNode) ClassNode(jadx.core.dex.nodes.ClassNode) IntegrationTest(jadx.tests.api.IntegrationTest) JadxAssertions.assertThat(jadx.tests.api.utils.assertj.JadxAssertions.assertThat) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef) Comparator(java.util.Comparator) Collectors(java.util.stream.Collectors) MethodNode(jadx.core.dex.nodes.MethodNode) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef)

Aggregations

VarDeclareRef (jadx.api.data.annotations.VarDeclareRef)2 ClassNode (jadx.core.dex.nodes.ClassNode)2 MethodNode (jadx.core.dex.nodes.MethodNode)2 Collectors (java.util.stream.Collectors)2 JavaClass (jadx.api.JavaClass)1 JavaField (jadx.api.JavaField)1 JavaMethod (jadx.api.JavaMethod)1 TypeGen (jadx.core.codegen.TypeGen)1 MethodInfo (jadx.core.dex.info.MethodInfo)1 ArgType (jadx.core.dex.instructions.args.ArgType)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 JClass (jadx.gui.treemodel.JClass)1 JField (jadx.gui.treemodel.JField)1 JMethod (jadx.gui.treemodel.JMethod)1 JNode (jadx.gui.treemodel.JNode)1 NLS (jadx.gui.utils.NLS)1 UiUtils (jadx.gui.utils.UiUtils)1 IntegrationTest (jadx.tests.api.IntegrationTest)1 JadxAssertions.assertThat (jadx.tests.api.utils.assertj.JadxAssertions.assertThat)1 ActionEvent (java.awt.event.ActionEvent)1