fix(gui): improve constructors and classes usage list

This commit is contained in:
Skylot
2021-08-23 17:10:43 +01:00
parent c8d7fce938
commit 5af60b2ff4
4 changed files with 34 additions and 6 deletions
@@ -579,7 +579,7 @@ public class ClassGen {
addClsName(code, classNode.getClassInfo());
}
private void addClsName(ICodeWriter code, ClassInfo classInfo) {
public void addClsName(ICodeWriter code, ClassInfo classInfo) {
String clsName = useClassInternal(cls.getClassInfo(), classInfo);
code.add(clsName);
}
@@ -677,13 +677,17 @@ public class InsnGen {
if (insn.isSelf()) {
throw new JadxRuntimeException("Constructor 'self' invoke must be removed!");
}
MethodNode callMth = mth.root().resolveMethod(insn.getCallMth());
if (insn.isSuper()) {
code.attachAnnotation(callMth);
code.add("super");
} else if (insn.isThis()) {
code.attachAnnotation(callMth);
code.add("this");
} else {
code.add("new ");
useClass(code, insn.getClassType());
code.attachAnnotation(callMth);
mgen.getClassGen().addClsName(code, insn.getClassType());
GenericInfoAttr genericInfoAttr = insn.get(AType.GENERIC_INFO);
if (genericInfoAttr != null) {
code.add('<');
@@ -701,7 +705,6 @@ public class InsnGen {
code.add('>');
}
}
MethodNode callMth = mth.root().resolveMethod(insn.getCallMth());
generateMethodArguments(code, insn, 0, callMth);
}
@@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import jadx.api.CodePosition;
import jadx.api.JavaClass;
import jadx.api.JavaMethod;
import jadx.api.JavaNode;
import jadx.gui.treemodel.CodeNode;
import jadx.gui.treemodel.JNode;
@@ -55,7 +56,14 @@ public class CodeUsageInfo {
// skip declaration
continue;
}
addUsage(nodeCache.makeFrom(javaNode), javaClass, linesInfo, codePosition, lines);
JNode node = nodeCache.makeFrom(javaNode);
addUsage(node, javaClass, linesInfo, codePosition, lines);
if (javaNode instanceof JavaMethod) {
// add constructor usage also as class usage
if (((JavaMethod) javaNode).isConstructor()) {
addUsage(node.getJParent(), javaClass, linesInfo, codePosition, lines);
}
}
}
} catch (Exception e) {
LOG.error("Code usage process failed for class: {}", javaClass, e);
@@ -64,12 +72,16 @@ public class CodeUsageInfo {
private void addUsage(JNode jNode, JavaClass javaClass,
CodeLinesInfo linesInfo, CodePosition codePosition, List<StringRef> lines) {
UsageInfo usageInfo = usageMap.computeIfAbsent(jNode, key -> new UsageInfo());
int line = codePosition.getLine();
JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(line);
StringRef codeLine = lines.get(line - 1);
if (codeLine.startsWith("import ")) {
// skip imports
return;
}
JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(line);
JNode node = nodeCache.makeFrom(javaNodeByLine == null ? javaClass : javaNodeByLine);
CodeNode codeNode = new CodeNode(node, codeLine, line, codePosition.getPos());
UsageInfo usageInfo = usageMap.computeIfAbsent(jNode, key -> new UsageInfo());
usageInfo.addUsage(codeNode);
}
@@ -117,6 +117,19 @@ public class StringRef implements CharSequence {
return -1;
}
public boolean startsWith(String str) {
int strLen = str.length();
if (this.length < strLen) {
return false;
}
for (int i = 0; i < strLen; i++) {
if (charAt(i) != str.charAt(i)) {
return false;
}
}
return true;
}
public static List<StringRef> split(String str, String splitBy) {
int len = str.length();
int targetLen = splitBy.length();