From a1537d2746b03e3f729fd1e0998426b9a507f9f4 Mon Sep 17 00:00:00 2001 From: Nico Mexis Date: Sun, 18 Jul 2021 22:38:53 +0200 Subject: [PATCH] Fix some NPEs --- .../java/the/bytecode/club/bootloader/Boot.java | 13 +++++++------ .../decompilers/impl/CFRDecompiler.java | 6 +++--- .../decompilers/impl/JADXDecompiler.java | 4 ++-- .../bytecodeviewer/resources/ExternalResources.java | 5 +++-- .../importing/impl/DirectoryResourceImporter.java | 13 ++++++------- .../club/bytecodeviewer/util/MiscUtils.java | 10 ++++++++++ 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/java/the/bytecode/club/bootloader/Boot.java b/src/main/java/the/bytecode/club/bootloader/Boot.java index 9d48feef..c2671871 100644 --- a/src/main/java/the/bytecode/club/bootloader/Boot.java +++ b/src/main/java/the/bytecode/club/bootloader/Boot.java @@ -17,6 +17,7 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.Constants; import the.bytecode.club.bytecodeviewer.api.ExceptionUI; +import the.bytecode.club.bytecodeviewer.util.MiscUtils; import the.bytecode.club.bytecodeviewer.util.ZipUtils; import static the.bytecode.club.bytecodeviewer.Constants.*; @@ -267,7 +268,7 @@ public class Boot { public static void setState(String s) { - if(screen != null) + if (screen != null) screen.setTitle(s); } @@ -295,7 +296,7 @@ public class Boot { public static void populateLibsDirectory() { File libsDir = libsDir(); if (libsDir.exists()) - for (File f : Objects.requireNonNull(libsDir.listFiles())) { + for (File f : MiscUtils.listFiles(libsDir)) { libsList.add(f.getName()); libsFileList.add(f.getAbsolutePath()); } @@ -450,14 +451,14 @@ public class Boot { setState("Bytecode Viewer Boot Screen - Checking Enjarify..."); System.out.println("Checking enjarify"); File enjarifyZip = null; - for (File f : Objects.requireNonNull(new File(Constants.libsDirectory).listFiles())) { + for (File f : MiscUtils.listFiles(new File(Constants.libsDirectory))) { if (f.getName().toLowerCase().startsWith("enjarify-")) { Constants.enjarifyVersion = f.getName().split("-")[1].split("\\.")[0]; enjarifyZip = f; } } - for (File f : Objects.requireNonNull(new File(getBCVDirectory()).listFiles())) { + for (File f : MiscUtils.listFiles(new File(getBCVDirectory()))) { if (f.getName().toLowerCase().startsWith("enjarify_") && !f.getName().split("_")[1].split("\\.")[0].equals(Constants.enjarifyVersion)) { setState("Bytecode Viewer Boot Screen - Removing Outdated " + f.getName() + "..."); System.out.println("Removing oudated " + f.getName()); @@ -493,7 +494,7 @@ public class Boot { System.out.println("Checking krakatau"); File krakatauZip = null; - for (File f : Objects.requireNonNull(new File(Constants.libsDirectory).listFiles())) { + for (File f : MiscUtils.listFiles(new File(Constants.libsDirectory))) { if (f.getName().toLowerCase().startsWith("krakatau-")) { //System.out.println(f.getName()); Constants.krakatauVersion = f.getName().split("-")[1].split("\\.")[0]; @@ -501,7 +502,7 @@ public class Boot { } } - for (File f : Objects.requireNonNull(new File(getBCVDirectory()).listFiles())) { + for (File f : MiscUtils.listFiles(new File(getBCVDirectory()))) { if (f.getName().toLowerCase().startsWith("krakatau_") && !f.getName().split("_")[1].split("\\.")[0].equals(Constants.krakatauVersion)) { setState("Bytecode Viewer Boot Screen - Removing Outdated " + f.getName() + "..."); System.out.println("Removing oudated " + f.getName()); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/CFRDecompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/CFRDecompiler.java index d4ebc398..e4a0243f 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/CFRDecompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/CFRDecompiler.java @@ -141,7 +141,7 @@ public class CFRDecompiler extends InternalDecompiler File file = new File(fuckery); if (file.exists()) - return findFile(Objects.requireNonNull(file.listFiles())); + return findFile(MiscUtils.listFiles(file)); return CFR + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + nl + nl + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + @@ -162,7 +162,7 @@ public class CFRDecompiler extends InternalDecompiler public String findFile(File[] fA) { for (File f : fA) { if (f.isDirectory()) - return findFile(Objects.requireNonNull(f.listFiles())); + return findFile(MiscUtils.listFiles(f)); else { String s; try { @@ -331,7 +331,7 @@ public class CFRDecompiler extends InternalDecompiler res = zout; while (!queue.isEmpty()) { directory = queue.pop(); - for (File kid : Objects.requireNonNull(directory.listFiles())) { + for (File kid : MiscUtils.listFiles(directory)) { String name = base.relativize(kid.toURI()).getPath(); if (kid.isDirectory()) { queue.push(kid); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/JADXDecompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/JADXDecompiler.java index 6221c9bc..292bc0a3 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/JADXDecompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/impl/JADXDecompiler.java @@ -86,7 +86,7 @@ public class JADXDecompiler extends InternalDecompiler tempClass.delete(); if (fuckery.exists()) - return findFile(Objects.requireNonNull(fuckery.listFiles())); + return findFile(MiscUtils.listFiles(fuckery)); if(exception.isEmpty()) exception = "Decompiled source file not found!"; @@ -113,7 +113,7 @@ public class JADXDecompiler extends InternalDecompiler public String findFile(File[] fA) { for (File f : fA) { if (f.isDirectory()) - return findFile(Objects.requireNonNull(f.listFiles())); + return findFile(MiscUtils.listFiles(f)); else { String s; try { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/ExternalResources.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/ExternalResources.java index fdec5f19..4b4ca6f7 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/ExternalResources.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/ExternalResources.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.Objects; +import the.bytecode.club.bytecodeviewer.util.MiscUtils; import static the.bytecode.club.bytecodeviewer.Constants.*; @@ -319,7 +320,7 @@ public class ExternalResources */ public String findLibrary(String nameContains) { - for (File f : Objects.requireNonNull(new File(libsDirectory).listFiles())) + for (File f : MiscUtils.listFiles(new File(libsDirectory))) if (f.getName().contains(nameContains)) return f.getAbsolutePath(); @@ -331,7 +332,7 @@ public class ExternalResources */ public File findFile(File basePath, String extension) { - for(File f : basePath.listFiles()) + for(File f : MiscUtils.listFiles(basePath)) { if(f.isDirectory()) { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java index 3ca70a23..983f6146 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java @@ -40,13 +40,12 @@ public class DirectoryResourceImporter implements Importer for (int i = 0; i < totalFiles.size(); i++) { File child = totalFiles.get(i); - if (child.listFiles() != null) - for (File rocket : Objects.requireNonNull(child.listFiles())) - if (!totalFiles.contains(rocket)) - { - totalFiles.add(rocket); - added = true; - } + for (File rocket : MiscUtils.listFiles(child)) + if (!totalFiles.contains(rocket)) + { + totalFiles.add(rocket); + added = true; + } } if (!added) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java index 0358f37f..e9b71911 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/MiscUtils.java @@ -295,4 +295,14 @@ public class MiscUtils baos.close(); return baos.toByteArray(); } + + public static File[] listFiles(File file) { + if (file == null) + return new File[0]; + File[] list = file.listFiles(); + if (list != null) + return list; + return new File[0]; + } + }