未验证 提交 c54dd77f 编写于 作者: S Skylot

fix(gui): resolve NPE and fix code style in BreakpointManager

上级 17fbc99f
package jadx.gui.device.debugger; package jadx.gui.device.debugger;
import java.io.Reader;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry; import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
...@@ -14,6 +16,8 @@ import java.util.Map; ...@@ -14,6 +16,8 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -28,52 +32,39 @@ import jadx.gui.treemodel.JClass; ...@@ -28,52 +32,39 @@ import jadx.gui.treemodel.JClass;
public class BreakpointManager { public class BreakpointManager {
private static final Logger LOG = LoggerFactory.getLogger(BreakpointManager.class); private static final Logger LOG = LoggerFactory.getLogger(BreakpointManager.class);
private static Gson gson = null; private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Type TYPE_TOKEN = new TypeToken<Map<String, List<FileBreakpoint>>>() { private static final Type TYPE_TOKEN = new TypeToken<Map<String, List<FileBreakpoint>>>() {
}.getType(); }.getType();
private static Map<String, List<FileBreakpoint>> bpm; private static @NotNull Map<String, List<FileBreakpoint>> bpm = Collections.emptyMap();
private static Path savePath; private static @Nullable Path savePath;
private static DebugController debugController; private static DebugController debugController;
private static Map<String, Entry<ClassNode, Listener>> listeners = Collections.emptyMap(); // class full name as key private static Map<String, Entry<ClassNode, Listener>> listeners = Collections.emptyMap(); // class full name as key
public static void saveAndExit() { public static void saveAndExit() {
if (bpm != null) {
if (bpm.size() == 0 && !Files.exists(savePath)) {
return; // user didn't do anything with breakpoint so don't output breakpoint file.
}
sync(); sync();
bpm = null; bpm = Collections.emptyMap();
savePath = null; savePath = null;
listeners = Collections.emptyMap(); listeners = Collections.emptyMap();
} }
}
public static void init(Path dirPath) { public static void init(@Nullable Path baseDir) {
if (gson == null) { Path saveDir = baseDir != null ? baseDir : Paths.get(".");
gson = new GsonBuilder() savePath = saveDir.resolve("breakpoints.json"); // TODO: move into project file or same dir as project file
.setPrettyPrinting()
.create();
}
savePath = dirPath.resolve("breakpoints.json");
if (Files.exists(savePath)) { if (Files.exists(savePath)) {
try { try (Reader reader = Files.newBufferedReader(savePath, StandardCharsets.UTF_8)) {
byte[] bytes = Files.readAllBytes(savePath); bpm = GSON.fromJson(reader, TYPE_TOKEN);
bpm = gson.fromJson(new String(bytes, StandardCharsets.UTF_8), TYPE_TOKEN);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Failed to read breakpoints config: {}", savePath, e); LOG.error("Failed to read breakpoints config: {}", savePath, e);
} }
} }
if (bpm == null) {
bpm = Collections.emptyMap();
}
} }
/** /**
* @param listener When breakpoint is failed to set during debugging, this listener will be called. * @param listener When breakpoint is failed to set during debugging, this listener will be called.
*/ */
public static void addListener(JClass topCls, Listener listener) { public static void addListener(JClass topCls, Listener listener) {
if (listeners == Collections.EMPTY_MAP) { if (listeners.isEmpty()) {
listeners = new HashMap<>(); listeners = new HashMap<>();
} }
listeners.put(DbgUtils.getRawFullName(topCls), listeners.put(DbgUtils.getRawFullName(topCls),
...@@ -149,8 +140,15 @@ public class BreakpointManager { ...@@ -149,8 +140,15 @@ public class BreakpointManager {
} }
private static void sync() { private static void sync() {
if (savePath == null) {
return;
}
if (bpm.isEmpty() && !Files.exists(savePath)) {
// user didn't do anything with breakpoint so don't output breakpoint file.
return;
}
try { try {
Files.write(savePath, gson.toJson(bpm).getBytes(StandardCharsets.UTF_8)); Files.write(savePath, GSON.toJson(bpm).getBytes(StandardCharsets.UTF_8));
} catch (Exception e) { } catch (Exception e) {
LOG.error("Failed to write breakpoints config: {}", savePath, e); LOG.error("Failed to write breakpoints config: {}", savePath, e);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册