Merge pull request #530 from PlaceholderAPI/fix/515-proper-expansion-null-checks

Fix contract violations & add null checks which should close #515
This commit is contained in:
PiggyPiglet 2021-01-04 10:25:48 +08:00 committed by GitHub
commit 093ba4d68c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -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<? extends PlaceholderExpansion>>> findExpansionsOnDisk() {
public CompletableFuture<@NotNull List<@Nullable Class<? extends PlaceholderExpansion>>> 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<? extends PlaceholderExpansion> expansionClass = FileUtil.findClass(file, PlaceholderExpansion.class);
if (expansionClass == null) {
plugin.getLogger().severe("Failed to load Expansion: " + file.getName() + ", as it does not have" +
" 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?)");

View File

@ -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;
}