fix(gui): handle paths where file name is null (#2136)(PR #2137)

* fix: Ignore invalid files

Avoid NullPointerException when using "Open files" or drag-n-drop

* refactor: Replace Stream API chain with loop

IntelliJ

* fix: Ignore invalid files

Avoid NullPointerException when using "Add files"

* fix: Fall back to complete path string

Instead of empty project name

* fix: Render tree

Project tree (sidebar) didn’t load
Toggling "View > Show flatten packages" threw a NPE here

* fix code formatting

---------

Co-authored-by: Skylot <skylot@gmail.com>
This commit is contained in:
xnumad
2024-03-29 23:30:01 +01:00
committed by GitHub
parent 2807dc5090
commit 6b4976c593
3 changed files with 22 additions and 6 deletions
@@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -113,11 +114,19 @@ public class JadxProject {
} else {
Collections.sort(files);
data.setFiles(files);
String joinedName = files.stream()
.map(p -> p.getFileName().toString())
.filter(file -> !file.endsWith(".jadx.kts"))
.map(CommonFileUtils::removeFileExtension)
.collect(Collectors.joining("_"));
StringJoiner joiner = new StringJoiner("_");
for (Path p : files) {
Path fileNamePart = p.getFileName();
if (fileNamePart == null) {
joiner.add(p.toString());
continue;
}
String fileName = fileNamePart.toString();
if (!fileName.endsWith(".jadx.kts")) {
joiner.add(CommonFileUtils.removeFileExtension(fileName));
}
}
String joinedName = joiner.toString();
name = StringUtils.abbreviate(joinedName, 100);
}
changed();
@@ -173,7 +173,11 @@ public class JRoot extends JNode {
return "File not open";
}
if (count == 1) {
return paths.get(0).getFileName().toString();
Path fileNamePath = paths.get(0).getFileName();
if (fileNamePath != null) {
return fileNamePath.toString();
}
return paths.get(0).toString();
}
return count + " files";
}
@@ -460,6 +460,9 @@ public class MainWindow extends JFrame {
}
private boolean openSingleFile(Path singleFile, Runnable onFinish) {
if (singleFile.getFileName() == null) {
return false;
}
String fileExtension = CommonFileUtils.getFileExtension(singleFile.getFileName().toString());
if (fileExtension != null && fileExtension.equalsIgnoreCase(JadxProject.PROJECT_EXTENSION)) {
openProject(singleFile, onFinish);