fix: use alias for variable names (#1487)

This commit is contained in:
Skylot 2022-05-20 20:53:44 +01:00
parent e4b19ab560
commit 7b1c7b967a
No known key found for this signature in database
GPG Key ID: 1E23F5B52567AA39
2 changed files with 44 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import java.util.Set;
import jadx.core.Consts; import jadx.core.Consts;
import jadx.core.deobf.NameMapper; import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.nodes.LoopLabelAttr; import jadx.core.dex.attributes.nodes.LoopLabelAttr;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.MethodInfo; import jadx.core.dex.info.MethodInfo;
@ -173,7 +174,7 @@ public class NameGen {
private String makeNameForType(ArgType type) { private String makeNameForType(ArgType type) {
if (type.isPrimitive()) { if (type.isPrimitive()) {
return makeNameForPrimitive(type); return type.getPrimitiveType().getShortName().toLowerCase();
} }
if (type.isArray()) { if (type.isArray()) {
return makeNameForType(type.getArrayRootElement()) + "Arr"; return makeNameForType(type.getArrayRootElement()) + "Arr";
@ -181,10 +182,6 @@ public class NameGen {
return makeNameForObject(type); return makeNameForObject(type);
} }
private static String makeNameForPrimitive(ArgType type) {
return type.getPrimitiveType().getShortName().toLowerCase();
}
private String makeNameForObject(ArgType type) { private String makeNameForObject(ArgType type) {
if (type.isGenericType()) { if (type.isGenericType()) {
return StringUtils.escape(type.getObject().toLowerCase()); return StringUtils.escape(type.getObject().toLowerCase());
@ -194,21 +191,30 @@ public class NameGen {
if (alias != null) { if (alias != null) {
return alias; return alias;
} }
ClassInfo extClsInfo = ClassInfo.fromType(mth.root(), type); return makeNameForCheckedClass(ClassInfo.fromType(mth.root(), type));
String shortName = extClsInfo.getShortName(); }
return StringUtils.escape(type.toString());
}
private String makeNameForCheckedClass(ClassInfo classInfo) {
String shortName = classInfo.getAliasShortName();
String vName = fromName(shortName); String vName = fromName(shortName);
if (vName != null) { if (vName != null) {
return vName; return vName;
} }
if (shortName != null) {
String lower = StringUtils.escape(shortName.toLowerCase()); String lower = StringUtils.escape(shortName.toLowerCase());
if (shortName.equals(lower)) { if (shortName.equals(lower)) {
return lower + "Var"; return lower + "Var";
} }
return lower; return lower;
} }
private String makeNameForClass(ClassInfo classInfo) {
String alias = getAliasForObject(classInfo.getFullName());
if (alias != null) {
return alias;
} }
return StringUtils.escape(type.toString()); return makeNameForCheckedClass(classInfo);
} }
private static String fromName(String name) { private static String fromName(String name) {
@ -241,7 +247,12 @@ public class NameGen {
case CONSTRUCTOR: case CONSTRUCTOR:
ConstructorInsn co = (ConstructorInsn) insn; ConstructorInsn co = (ConstructorInsn) insn;
return makeNameForObject(co.getClassType().getType()); MethodNode callMth = mth.root().getMethodUtils().resolveMethod(co);
if (callMth != null && callMth.contains(AFlag.ANONYMOUS_CONSTRUCTOR)) {
// don't use name of anonymous class
return null;
}
return makeNameForClass(co.getClassType());
case ARRAY_LENGTH: case ARRAY_LENGTH:
return "length"; return "length";
@ -267,11 +278,11 @@ public class NameGen {
} }
private String makeNameFromInvoke(MethodInfo callMth) { private String makeNameFromInvoke(MethodInfo callMth) {
String name = callMth.getName(); String name = callMth.getAlias();
ArgType declType = callMth.getDeclClass().getType(); ClassInfo declClass = callMth.getDeclClass();
if ("getInstance".equals(name)) { if ("getInstance".equals(name)) {
// e.g. Cipher.getInstance // e.g. Cipher.getInstance
return makeNameForType(declType); return makeNameForClass(declClass);
} }
if (name.startsWith("get") || name.startsWith("set")) { if (name.startsWith("get") || name.startsWith("set")) {
return fromName(name.substring(3)); return fromName(name.substring(3));
@ -280,9 +291,9 @@ public class NameGen {
return "it"; return "it";
} }
if ("toString".equals(name)) { if ("toString".equals(name)) {
return makeNameForType(declType); return makeNameForClass(declClass);
} }
if ("forName".equals(name) && declType.equals(ArgType.CLASS)) { if ("forName".equals(name) && declClass.getType().equals(ArgType.CLASS)) {
return OBJ_ALIAS.get(Consts.CLASS_CLASS); return OBJ_ALIAS.get(Consts.CLASS_CLASS);
} }
if (name.startsWith("to")) { if (name.startsWith("to")) {

View File

@ -45,6 +45,15 @@ public class MethodUtils {
return root.getClsp().getMethodDetails(callMth); return root.getClsp().getMethodDetails(callMth);
} }
@Nullable
public MethodNode resolveMethod(BaseInvokeNode invokeNode) {
IMethodDetails methodDetails = getMethodDetails(invokeNode);
if (methodDetails instanceof MethodNode) {
return ((MethodNode) methodDetails);
}
return null;
}
/** /**
* Search methods with same name and args count in class hierarchy starting from {@code startCls} * Search methods with same name and args count in class hierarchy starting from {@code startCls}
* Beware {@code startCls} can be different from {@code mthInfo.getDeclClass()} * Beware {@code startCls} can be different from {@code mthInfo.getDeclClass()}