Skip to content

Commit

Permalink
fix(gui): add synchronizations to search index creation (#433)
Browse files Browse the repository at this point in the history
* fix: unsynchronized search index creation (code usage) results in ArrayIndexOutOfBoundsException and stuck at 99%

* fix: use computeIfAbsent instead of synchronized block
  • Loading branch information
jpstotz authored and skylot committed Jan 18, 2019
1 parent d1af751 commit 9e0cd2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions jadx-gui/src/main/java/jadx/gui/ui/HeapUsageBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public HeapUsageBar() {
setMaximum(maxKB);
maxGB = maxKB / TWO_TO_20;
update();
timer = new Timer(1000, this);
timer = new Timer(2000, this);
}

public void update() {
Expand All @@ -44,7 +44,7 @@ public void update() {
setValue(usedKB);
setString(String.format(textFormat, (usedKB / TWO_TO_20), maxGB));

if (used > r.totalMemory() * 0.8) {
if (used > r.maxMemory() * 0.8) {
setForeground(RED);
} else {
setForeground(GREEN);
Expand Down
23 changes: 12 additions & 11 deletions jadx-gui/src/main/java/jadx/gui/utils/CodeUsageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jadx.api.CodePosition;
import jadx.api.JavaClass;
Expand All @@ -21,6 +22,10 @@ public static class UsageInfo {
public List<CodeNode> getUsageList() {
return usageList;
}

public synchronized void addUsage(CodeNode codeNode) {
usageList.add(codeNode);
}
}

private final JNodeCache nodeCache;
Expand All @@ -29,7 +34,7 @@ public CodeUsageInfo(JNodeCache nodeCache) {
this.nodeCache = nodeCache;
}

private final Map<JNode, UsageInfo> usageMap = new HashMap<>();
private final Map<JNode, UsageInfo> usageMap = new ConcurrentHashMap<>();

public void processClass(JavaClass javaClass, CodeLinesInfo linesInfo, List<StringRef> lines) {
Map<CodePosition, JavaNode> usage = javaClass.getUsageMap();
Expand All @@ -42,17 +47,13 @@ public void processClass(JavaClass javaClass, CodeLinesInfo linesInfo, List<Stri

private void addUsage(JNode jNode, JavaClass javaClass,
CodeLinesInfo linesInfo, CodePosition codePosition, List<StringRef> lines) {
UsageInfo usageInfo = usageMap.get(jNode);
if (usageInfo == null) {
usageInfo = new UsageInfo();
usageMap.put(jNode, usageInfo);
}
int line = codePosition.getLine();
JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(line);
StringRef codeLine = lines.get(line - 1);
JNode node = nodeCache.makeFrom(javaNodeByLine == null ? javaClass : javaNodeByLine);
UsageInfo usageInfo = usageMap.computeIfAbsent(jNode, key -> new UsageInfo());
int line = codePosition.getLine();
JavaNode javaNodeByLine = linesInfo.getJavaNodeByLine(line);
StringRef codeLine = lines.get(line - 1);
JNode node = nodeCache.makeFrom(javaNodeByLine == null ? javaClass : javaNodeByLine);
CodeNode codeNode = new CodeNode(node, line, codeLine);
usageInfo.getUsageList().add(codeNode);
usageInfo.addUsage(codeNode);
}

public List<CodeNode> getUsageList(JNode node) {
Expand Down

0 comments on commit 9e0cd2e

Please sign in to comment.