Search in sources :

Example 1 with ICodeRename

use of jadx.api.data.ICodeRename in project jadx by skylot.

the class RenameDialog method updateCodeRenames.

private void updateCodeRenames(Consumer<Set<ICodeRename>> updater) {
    JadxProject project = mainWindow.getProject();
    JadxCodeData codeData = project.getCodeData();
    if (codeData == null) {
        codeData = new JadxCodeData();
    }
    Set<ICodeRename> set = new HashSet<>(codeData.getRenames());
    updater.accept(set);
    List<ICodeRename> list = new ArrayList<>(set);
    Collections.sort(list);
    codeData.setRenames(list);
    project.setCodeData(codeData);
    mainWindow.getWrapper().getDecompiler().reloadCodeData();
}
Also used : ICodeRename(jadx.api.data.ICodeRename) ArrayList(java.util.ArrayList) JadxProject(jadx.gui.settings.JadxProject) JadxCodeData(jadx.api.data.impl.JadxCodeData) HashSet(java.util.HashSet)

Example 2 with ICodeRename

use of jadx.api.data.ICodeRename 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)

Example 3 with ICodeRename

use of jadx.api.data.ICodeRename in project jadx by skylot.

the class UserRenames method applyPkgRenames.

// TODO: Very inefficient!!! Add PackageInfo class to build package hierarchy
private static void applyPkgRenames(RootNode root, List<ICodeRename> renames) {
    List<ClassNode> classes = root.getClasses(false);
    renames.stream().filter(r -> r.getNodeRef().getType() == IJavaNodeRef.RefType.PKG).forEach(pkgRename -> {
        String pkgFullName = pkgRename.getNodeRef().getDeclaringClass();
        String pkgFullNameDot = pkgFullName + ".";
        for (ClassNode cls : classes) {
            ClassInfo clsInfo = cls.getClassInfo();
            String pkg = clsInfo.getPackage();
            if (pkg.equals(pkgFullName)) {
                clsInfo.changePkg(cutLastPkgPart(clsInfo.getAliasPkg()) + '.' + pkgRename.getNewName());
            } else if (pkg.startsWith(pkgFullNameDot)) {
                clsInfo.changePkg(rebuildPkgMiddle(clsInfo.getAliasPkg(), pkgFullName, pkgRename.getNewName()));
            }
        }
    });
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) MethodNode(jadx.core.dex.nodes.MethodNode) Logger(org.slf4j.Logger) AType(jadx.core.dex.attributes.AType) ICodeRename(jadx.api.data.ICodeRename) LoggerFactory(org.slf4j.LoggerFactory) RootNode(jadx.core.dex.nodes.RootNode) ClassInfo(jadx.core.dex.info.ClassInfo) Collectors(java.util.stream.Collectors) IJavaNodeRef(jadx.api.data.IJavaNodeRef) ICodeData(jadx.api.data.ICodeData) IJavaCodeRef(jadx.api.data.IJavaCodeRef) List(java.util.List) ClassNode(jadx.core.dex.nodes.ClassNode) InfoStorage(jadx.core.dex.info.InfoStorage) FieldNode(jadx.core.dex.nodes.FieldNode) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) NotNull(org.jetbrains.annotations.NotNull) ClassNode(jadx.core.dex.nodes.ClassNode) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 4 with ICodeRename

use of jadx.api.data.ICodeRename in project jadx by skylot.

the class UserRenames method applyForNodes.

public static void applyForNodes(RootNode root) {
    ICodeData codeData = root.getArgs().getCodeData();
    if (codeData == null || codeData.getRenames().isEmpty()) {
        return;
    }
    InfoStorage infoStorage = root.getInfoStorage();
    codeData.getRenames().stream().filter(r -> r.getCodeRef() == null && r.getNodeRef().getType() != IJavaNodeRef.RefType.PKG).collect(Collectors.groupingBy(r -> r.getNodeRef().getDeclaringClass())).forEach((clsRawName, renames) -> {
        ClassInfo clsInfo = infoStorage.getCls(ArgType.object(clsRawName));
        if (clsInfo != null) {
            ClassNode cls = root.resolveClass(clsInfo);
            if (cls != null) {
                for (ICodeRename rename : renames) {
                    applyRename(cls, rename);
                }
                return;
            }
        }
        LOG.warn("Class info with reference '{}' not found", clsRawName);
    });
    applyPkgRenames(root, codeData.getRenames());
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ICodeRename(jadx.api.data.ICodeRename) InfoStorage(jadx.core.dex.info.InfoStorage) ICodeData(jadx.api.data.ICodeData) ClassInfo(jadx.core.dex.info.ClassInfo)

Aggregations

ICodeRename (jadx.api.data.ICodeRename)4 ClassNode (jadx.core.dex.nodes.ClassNode)3 ICodeData (jadx.api.data.ICodeData)2 IJavaCodeRef (jadx.api.data.IJavaCodeRef)2 JadxCodeData (jadx.api.data.impl.JadxCodeData)2 ClassInfo (jadx.core.dex.info.ClassInfo)2 InfoStorage (jadx.core.dex.info.InfoStorage)2 ArrayList (java.util.ArrayList)2 IJavaNodeRef (jadx.api.data.IJavaNodeRef)1 JadxCodeRef (jadx.api.data.impl.JadxCodeRef)1 JadxCodeRename (jadx.api.data.impl.JadxCodeRename)1 JadxNodeRef (jadx.api.data.impl.JadxNodeRef)1 AType (jadx.core.dex.attributes.AType)1 MethodOverrideAttr (jadx.core.dex.attributes.nodes.MethodOverrideAttr)1 ArgType (jadx.core.dex.instructions.args.ArgType)1 FieldNode (jadx.core.dex.nodes.FieldNode)1 MethodNode (jadx.core.dex.nodes.MethodNode)1 RootNode (jadx.core.dex.nodes.RootNode)1 JadxProject (jadx.gui.settings.JadxProject)1 IntegrationTest (jadx.tests.api.IntegrationTest)1