From 07ac8e41c554e912deb9034e048a80fd28eea566 Mon Sep 17 00:00:00 2001 From: PiggyPiglet Date: Sun, 3 Jan 2021 23:42:23 +0800 Subject: [PATCH 1/2] Fix contract violations & add null checks which should close #515 --- .../manager/LocalExpansionManager.java | 18 +++++++++++++----- .../me/clip/placeholderapi/util/FileUtil.java | 7 ++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java index 5e2e0b3..18a4c2b 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java +++ b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java @@ -300,7 +300,10 @@ public final class LocalExpansionManager implements Listener { return; } - final long registered = classes.stream().map(this::register).filter(Optional::isPresent) + final long registered = classes.stream() + .filter(Objects::nonNull) + .map(this::register) + .filter(Optional::isPresent) .count(); Msg.msg(sender, @@ -321,11 +324,9 @@ public final class LocalExpansionManager implements Listener { } } - @NotNull - public CompletableFuture<@NotNull List<@NotNull Class>> findExpansionsOnDisk() { + public CompletableFuture<@NotNull List<@Nullable Class>> findExpansionsOnDisk() { return Arrays.stream(folder.listFiles((dir, name) -> name.endsWith(".jar"))) - .filter(Objects::nonNull) .map(this::findExpansionInFile) .collect(Futures.collector()); } @@ -335,7 +336,14 @@ public final class LocalExpansionManager implements Listener { @NotNull final File file) { return CompletableFuture.supplyAsync(() -> { try { - return FileUtil.findClass(file, PlaceholderExpansion.class); + final Class expansionClass = FileUtil.findClass(file, PlaceholderExpansion.class); + + if (expansionClass == null) { + plugin.getLogger().severe("Failed to load Expansion: " + file.getName() + ", as it does not have" + + "an a class which extends PlaceholderExpansion."); + } + + return expansionClass; } catch (final VerifyError ex) { plugin.getLogger().severe("Failed to load Expansion class " + file.getName() + " (Is a dependency missing?)"); diff --git a/src/main/java/me/clip/placeholderapi/util/FileUtil.java b/src/main/java/me/clip/placeholderapi/util/FileUtil.java index 9741a0d..7e20de1 100644 --- a/src/main/java/me/clip/placeholderapi/util/FileUtil.java +++ b/src/main/java/me/clip/placeholderapi/util/FileUtil.java @@ -20,6 +20,9 @@ package me.clip.placeholderapi.util; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.io.File; import java.io.IOException; import java.net.URL; @@ -28,8 +31,6 @@ import java.util.ArrayList; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; public class FileUtil { @@ -51,7 +52,7 @@ public class FileUtil { JarEntry entry; while ((entry = stream.getNextJarEntry()) != null) { final String name = entry.getName(); - if (name == null || name.isEmpty() || !name.endsWith(".class")) { + if (name.isEmpty() || !name.endsWith(".class")) { continue; } From 1478aabb5e8bc65c067772d91efdbf2f3ee2ad27 Mon Sep 17 00:00:00 2001 From: PiggyPiglet Date: Mon, 4 Jan 2021 10:25:38 +0800 Subject: [PATCH 2/2] remove an from log message Co-authored-by: Andre_601 <11576465+Andre601@users.noreply.github.com> --- .../placeholderapi/expansion/manager/LocalExpansionManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java index 18a4c2b..2557ae2 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java +++ b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java @@ -340,7 +340,7 @@ public final class LocalExpansionManager implements Listener { if (expansionClass == null) { plugin.getLogger().severe("Failed to load Expansion: " + file.getName() + ", as it does not have" + - "an a class which extends PlaceholderExpansion."); + " a class which extends PlaceholderExpansion."); } return expansionClass;