Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]find usage should find the same method in super and interfaces #1349 #1352

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions jadx-gui/src/main/java/jadx/gui/ui/dialog/UsageDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
Expand All @@ -17,9 +18,12 @@
import jadx.api.CodePosition;
import jadx.api.ICodeWriter;
import jadx.api.JavaClass;
import jadx.api.JavaMethod;
import jadx.api.JavaNode;
import jadx.core.dex.attributes.AType;
import jadx.gui.jobs.TaskStatus;
import jadx.gui.treemodel.CodeNode;
import jadx.gui.treemodel.JMethod;
import jadx.gui.treemodel.JNode;
import jadx.gui.ui.MainWindow;
import jadx.gui.utils.CodeLinesInfo;
Expand Down Expand Up @@ -59,32 +63,55 @@ protected void openInit() {

private void collectUsageData() {
usageList = new ArrayList<>();
node.getJavaNode().getUseIn()
getMethodUseIn()
.stream()
.map(JavaNode::getTopParentClass)
.distinct()
.sorted(Comparator.comparing(JavaClass::getFullName))
.forEach(this::processUsageClass);
}

private void processUsageClass(JavaClass cls) {
private List<JavaNode> getMethodUseIn() {
if (node instanceof JMethod) {
JavaMethod method = ((JMethod) node).getJavaMethod();
if (null != method.getMethodNode().get(AType.METHOD_OVERRIDE)) {
return method.getOverrideRelatedMethods().stream().flatMap(m -> m.getUseIn().stream()).collect(Collectors.toList());
}
}
return node.getJavaNode().getUseIn();
}

private void processUsageClass(JavaNode usageNode) {
JavaClass cls = usageNode.getTopParentClass();
String code = cls.getCodeInfo().getCodeStr();
CodeLinesInfo linesInfo = new CodeLinesInfo(cls);
JavaNode javaNode = node.getJavaNode();
List<CodePosition> usage = cls.getUsageFor(javaNode);
for (CodePosition pos : usage) {
if (javaNode.getTopParentClass().equals(cls) && pos.getPos() == javaNode.getDefPos()) {
// skip declaration
continue;
List<? extends JavaNode> targetNodes = getMethodWithOverride();
for (JavaNode javaNode : targetNodes) {
List<CodePosition> usage = cls.getUsageFor(javaNode);
for (CodePosition pos : usage) {
if (javaNode.getTopParentClass().equals(cls) && pos.getPos() == javaNode.getDefPos()) {
// skip declaration
continue;
}
StringRef line = getLineStrAt(code, pos.getPos());
if (line.startsWith("import ")) {
continue;
}
JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(pos.getLine());
JNode useAtNode = javaNodeByLine == null ? node : getNodeCache().makeFrom(javaNodeByLine);
usageList.add(new CodeNode(useAtNode, line, pos.getLine(), pos.getPos()));
}
StringRef line = getLineStrAt(code, pos.getPos());
if (line.startsWith("import ")) {
continue;
}
}

private List<? extends JavaNode> getMethodWithOverride() {
if (node instanceof JMethod) {
JavaMethod method = ((JMethod) node).getJavaMethod();
if (null != method.getMethodNode().get(AType.METHOD_OVERRIDE)) {
return method.getOverrideRelatedMethods();
}
JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(pos.getLine());
JNode useAtNode = javaNodeByLine == null ? node : getNodeCache().makeFrom(javaNodeByLine);
usageList.add(new CodeNode(useAtNode, line, pos.getLine(), pos.getPos()));
}
return Collections.singletonList(node.getJavaNode());
}

private StringRef getLineStrAt(String code, int pos) {
Expand Down