Search in sources :

Example 1 with JadxCodeRename

use of jadx.api.data.impl.JadxCodeRename in project jadx by skylot.

the class RenameDialog method processRename.

private void processRename(JNode node, String newName, Set<ICodeRename> renames) {
    JadxCodeRename rename = buildRename(node, newName, renames);
    renames.remove(rename);
    JavaNode javaNode = node.getJavaNode();
    if (javaNode != null) {
        javaNode.removeAlias();
    }
    if (!newName.isEmpty()) {
        renames.add(rename);
    }
}
Also used : JadxCodeRename(jadx.api.data.impl.JadxCodeRename) JavaNode(jadx.api.JavaNode)

Example 2 with JadxCodeRename

use of jadx.api.data.impl.JadxCodeRename 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 3 with JadxCodeRename

use of jadx.api.data.impl.JadxCodeRename in project jadx by skylot.

the class TestUserRenames method test.

@Test
public void test() {
    getArgs().setDeobfuscationOn(false);
    List<ICodeRename> renames = new ArrayList<>();
    String baseClsId = TestCls.class.getName();
    renames.add(new JadxCodeRename(JadxNodeRef.forPkg("jadx.tests"), "renamedPkgTests"));
    renames.add(new JadxCodeRename(JadxNodeRef.forPkg("jadx.tests.integration.rename"), "renamedPkgRename"));
    renames.add(new JadxCodeRename(JadxNodeRef.forCls(baseClsId), "RenamedTestCls"));
    renames.add(new JadxCodeRename(JadxNodeRef.forCls(baseClsId + "$A"), "RenamedInnerCls"));
    renames.add(new JadxCodeRename(new JadxNodeRef(RefType.FIELD, baseClsId, "intField:I"), "renamedField"));
    JadxNodeRef mthRef = new JadxNodeRef(RefType.METHOD, baseClsId, "test(I)I");
    renames.add(new JadxCodeRename(mthRef, "renamedTestMth"));
    renames.add(new JadxCodeRename(mthRef, new JadxCodeRef(CodeRefType.MTH_ARG, 0), "renamedX"));
    JadxCodeRef varDeclareRef = isJavaInput() ? JadxCodeRef.forVar(0, 1) : JadxCodeRef.forVar(0, 0);
    renames.add(new JadxCodeRename(mthRef, varDeclareRef, "renamedY"));
    IJavaCodeRef varUseRef = isJavaInput() ? JadxCodeRef.forVar(0, 4) : JadxCodeRef.forVar(1, 0);
    renames.add(new JadxCodeRename(mthRef, varUseRef, "renamedZ"));
    JadxCodeData codeData = new JadxCodeData();
    codeData.setRenames(renames);
    getArgs().setCodeData(codeData);
    ClassNode cls = getClassNode(TestCls.class);
    assertThat(cls).decompile().checkCodeOffsets().code().containsOne("package jadx.renamedPkgTests.integration.renamedPkgRename;").containsOne("public class RenamedTestCls {").containsOne("private int renamedField").containsOne("public static class RenamedInnerCls {").containsOne("public int renamedTestMth(int renamedX) {").containsOne("int renamedY = renamedX + \"test\".length();").containsOne("int renamedZ = renamedY + 1;").containsOne("return renamedZ;");
    String code = cls.getCode().getCodeStr();
    assertThat(cls).reloadCode(this).isEqualTo(code);
    ICodeRename updVarRename = new JadxCodeRename(mthRef, varUseRef, "anotherZ");
    codeData.setRenames(Collections.singletonList(updVarRename));
    jadxDecompiler.reloadCodeData();
    assertThat(cls).reloadCode(this).containsOne("int anotherZ = y + 1;").doesNotContain("int z").doesNotContain("int renamedZ");
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ICodeRename(jadx.api.data.ICodeRename) ArrayList(java.util.ArrayList) JadxCodeRef(jadx.api.data.impl.JadxCodeRef) IJavaCodeRef(jadx.api.data.IJavaCodeRef) JadxNodeRef(jadx.api.data.impl.JadxNodeRef) JadxCodeData(jadx.api.data.impl.JadxCodeData) JadxCodeRename(jadx.api.data.impl.JadxCodeRename) Test(org.junit.jupiter.api.Test) IntegrationTest(jadx.tests.api.IntegrationTest)

Aggregations

JadxCodeRename (jadx.api.data.impl.JadxCodeRename)3 JavaMethod (jadx.api.JavaMethod)1 JavaNode (jadx.api.JavaNode)1 JavaVariable (jadx.api.JavaVariable)1 ICodeRename (jadx.api.data.ICodeRename)1 IJavaCodeRef (jadx.api.data.IJavaCodeRef)1 JadxCodeData (jadx.api.data.impl.JadxCodeData)1 JadxCodeRef (jadx.api.data.impl.JadxCodeRef)1 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)1 ClassNode (jadx.core.dex.nodes.ClassNode)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 JPackage (jadx.gui.treemodel.JPackage)1 JVariable (jadx.gui.treemodel.JVariable)1 IntegrationTest (jadx.tests.api.IntegrationTest)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.jupiter.api.Test)1