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();
}
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");
}
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()));
}
}
});
}
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());
}
Aggregations