Increase clarity of errors when loading malformed plugin main classes

By: md_5 <git@md-5.net>
This commit is contained in:
Bukkit/Spigot 2023-10-15 10:59:48 +11:00
parent 943fb1db3d
commit a1daa7077c

View file

@ -5,6 +5,8 @@ import com.google.common.io.ByteStreams;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
@ -61,7 +63,6 @@ final class PluginClassLoader extends URLClassLoader {
this.url = file.toURI().toURL();
this.libraryLoader = libraryLoader;
try {
Class<?> jarClass;
try {
jarClass = Class.forName(description.getMain(), true, this);
@ -73,14 +74,26 @@ final class PluginClassLoader extends URLClassLoader {
try {
pluginClass = jarClass.asSubclass(JavaPlugin.class);
} catch (ClassCastException ex) {
throw new InvalidPluginException("main class `" + description.getMain() + "' does not extend JavaPlugin", ex);
throw new InvalidPluginException("main class `" + description.getMain() + "' must extend JavaPlugin", ex);
}
plugin = pluginClass.newInstance();
Constructor<? extends JavaPlugin> pluginConstructor;
try {
pluginConstructor = pluginClass.getDeclaredConstructor();
} catch (NoSuchMethodException ex) {
throw new InvalidPluginException("main class `" + description.getMain() + "' must have a public no-args constructor", ex);
}
try {
plugin = pluginConstructor.newInstance();
} catch (IllegalAccessException ex) {
throw new InvalidPluginException("No public constructor", ex);
throw new InvalidPluginException("main class `" + description.getMain() + "' constructor must be public", ex);
} catch (InstantiationException ex) {
throw new InvalidPluginException("Abnormal plugin type", ex);
throw new InvalidPluginException("main class `" + description.getMain() + "' must not be abstract", ex);
} catch (IllegalArgumentException ex) {
throw new InvalidPluginException("Could not invoke main class `" + description.getMain() + "' constructor", ex);
} catch (ExceptionInInitializerError | InvocationTargetException ex) {
throw new InvalidPluginException("Exception initializing main class `" + description.getMain() + "'", ex);
}
}