Fix some NPEs

This commit is contained in:
Nico Mexis 2021-07-18 22:38:53 +02:00
parent cf6378d334
commit a1537d2746
No known key found for this signature in database
GPG key ID: 27D6E17CE092AB78
6 changed files with 31 additions and 20 deletions

View file

@ -17,6 +17,7 @@ import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.Constants; import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI; import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import the.bytecode.club.bytecodeviewer.util.ZipUtils; import the.bytecode.club.bytecodeviewer.util.ZipUtils;
import static the.bytecode.club.bytecodeviewer.Constants.*; import static the.bytecode.club.bytecodeviewer.Constants.*;
@ -267,7 +268,7 @@ public class Boot {
public static void setState(String s) public static void setState(String s)
{ {
if(screen != null) if (screen != null)
screen.setTitle(s); screen.setTitle(s);
} }
@ -295,7 +296,7 @@ public class Boot {
public static void populateLibsDirectory() { public static void populateLibsDirectory() {
File libsDir = libsDir(); File libsDir = libsDir();
if (libsDir.exists()) if (libsDir.exists())
for (File f : Objects.requireNonNull(libsDir.listFiles())) { for (File f : MiscUtils.listFiles(libsDir)) {
libsList.add(f.getName()); libsList.add(f.getName());
libsFileList.add(f.getAbsolutePath()); libsFileList.add(f.getAbsolutePath());
} }
@ -450,14 +451,14 @@ public class Boot {
setState("Bytecode Viewer Boot Screen - Checking Enjarify..."); setState("Bytecode Viewer Boot Screen - Checking Enjarify...");
System.out.println("Checking enjarify"); System.out.println("Checking enjarify");
File enjarifyZip = null; 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-")) { if (f.getName().toLowerCase().startsWith("enjarify-")) {
Constants.enjarifyVersion = f.getName().split("-")[1].split("\\.")[0]; Constants.enjarifyVersion = f.getName().split("-")[1].split("\\.")[0];
enjarifyZip = f; 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)) { if (f.getName().toLowerCase().startsWith("enjarify_") && !f.getName().split("_")[1].split("\\.")[0].equals(Constants.enjarifyVersion)) {
setState("Bytecode Viewer Boot Screen - Removing Outdated " + f.getName() + "..."); setState("Bytecode Viewer Boot Screen - Removing Outdated " + f.getName() + "...");
System.out.println("Removing oudated " + f.getName()); System.out.println("Removing oudated " + f.getName());
@ -493,7 +494,7 @@ public class Boot {
System.out.println("Checking krakatau"); System.out.println("Checking krakatau");
File krakatauZip = null; 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-")) { if (f.getName().toLowerCase().startsWith("krakatau-")) {
//System.out.println(f.getName()); //System.out.println(f.getName());
Constants.krakatauVersion = f.getName().split("-")[1].split("\\.")[0]; 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)) { if (f.getName().toLowerCase().startsWith("krakatau_") && !f.getName().split("_")[1].split("\\.")[0].equals(Constants.krakatauVersion)) {
setState("Bytecode Viewer Boot Screen - Removing Outdated " + f.getName() + "..."); setState("Bytecode Viewer Boot Screen - Removing Outdated " + f.getName() + "...");
System.out.println("Removing oudated " + f.getName()); System.out.println("Removing oudated " + f.getName());

View file

@ -141,7 +141,7 @@ public class CFRDecompiler extends InternalDecompiler
File file = new File(fuckery); File file = new File(fuckery);
if (file.exists()) if (file.exists())
return findFile(Objects.requireNonNull(file.listFiles())); return findFile(MiscUtils.listFiles(file));
return CFR + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO + return CFR + " " + ERROR + "! " + ExceptionUI.SEND_STACKTRACE_TO +
nl + nl + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR + nl + nl + TranslatedStrings.SUGGESTED_FIX_DECOMPILER_ERROR +
@ -162,7 +162,7 @@ public class CFRDecompiler extends InternalDecompiler
public String findFile(File[] fA) { public String findFile(File[] fA) {
for (File f : fA) { for (File f : fA) {
if (f.isDirectory()) if (f.isDirectory())
return findFile(Objects.requireNonNull(f.listFiles())); return findFile(MiscUtils.listFiles(f));
else { else {
String s; String s;
try { try {
@ -331,7 +331,7 @@ public class CFRDecompiler extends InternalDecompiler
res = zout; res = zout;
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
directory = queue.pop(); directory = queue.pop();
for (File kid : Objects.requireNonNull(directory.listFiles())) { for (File kid : MiscUtils.listFiles(directory)) {
String name = base.relativize(kid.toURI()).getPath(); String name = base.relativize(kid.toURI()).getPath();
if (kid.isDirectory()) { if (kid.isDirectory()) {
queue.push(kid); queue.push(kid);

View file

@ -86,7 +86,7 @@ public class JADXDecompiler extends InternalDecompiler
tempClass.delete(); tempClass.delete();
if (fuckery.exists()) if (fuckery.exists())
return findFile(Objects.requireNonNull(fuckery.listFiles())); return findFile(MiscUtils.listFiles(fuckery));
if(exception.isEmpty()) if(exception.isEmpty())
exception = "Decompiled source file not found!"; exception = "Decompiled source file not found!";
@ -113,7 +113,7 @@ public class JADXDecompiler extends InternalDecompiler
public String findFile(File[] fA) { public String findFile(File[] fA) {
for (File f : fA) { for (File f : fA) {
if (f.isDirectory()) if (f.isDirectory())
return findFile(Objects.requireNonNull(f.listFiles())); return findFile(MiscUtils.listFiles(f));
else { else {
String s; String s;
try { try {

View file

@ -11,6 +11,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Objects; import java.util.Objects;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
import static the.bytecode.club.bytecodeviewer.Constants.*; import static the.bytecode.club.bytecodeviewer.Constants.*;
@ -319,7 +320,7 @@ public class ExternalResources
*/ */
public String findLibrary(String nameContains) 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)) if (f.getName().contains(nameContains))
return f.getAbsolutePath(); return f.getAbsolutePath();
@ -331,7 +332,7 @@ public class ExternalResources
*/ */
public File findFile(File basePath, String extension) public File findFile(File basePath, String extension)
{ {
for(File f : basePath.listFiles()) for(File f : MiscUtils.listFiles(basePath))
{ {
if(f.isDirectory()) if(f.isDirectory())
{ {

View file

@ -40,8 +40,7 @@ public class DirectoryResourceImporter implements Importer
for (int i = 0; i < totalFiles.size(); i++) for (int i = 0; i < totalFiles.size(); i++)
{ {
File child = totalFiles.get(i); File child = totalFiles.get(i);
if (child.listFiles() != null) for (File rocket : MiscUtils.listFiles(child))
for (File rocket : Objects.requireNonNull(child.listFiles()))
if (!totalFiles.contains(rocket)) if (!totalFiles.contains(rocket))
{ {
totalFiles.add(rocket); totalFiles.add(rocket);

View file

@ -295,4 +295,14 @@ public class MiscUtils
baos.close(); baos.close();
return baos.toByteArray(); 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];
}
} }