Fix JavaPlugin static getPlugin methods (#8888)

This commit is contained in:
Jake Potrebic 2023-02-21 19:27:49 -08:00
parent 37d171544a
commit ee1dffb8d5
3 changed files with 48 additions and 25 deletions

View file

@ -925,6 +925,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import org.jetbrains.annotations.NotNull;
+
+import java.io.Closeable;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * The configured plugin class loader represents an <b>internal</b> abstraction over the classloaders used by the server
@ -971,6 +972,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @param plugin the {@link JavaPlugin} that should be interlinked with this class loader.
+ */
+ void init(JavaPlugin plugin);
+
+ /**
+ * Gets the plugin held by this class loader.
+ *
+ * @return the plugin or null if it doesn't exist yet
+ */
+ @Nullable JavaPlugin getPlugin();
+}
diff --git a/src/main/java/io/papermc/paper/plugin/provider/classloader/PaperClassLoaderStorage.java b/src/main/java/io/papermc/paper/plugin/provider/classloader/PaperClassLoaderStorage.java
new file mode 100644
@ -1941,6 +1949,34 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
}
/**
@@ -0,0 +0,0 @@ public abstract class JavaPlugin extends PluginBase {
throw new IllegalArgumentException(clazz + " does not extend " + JavaPlugin.class);
}
final ClassLoader cl = clazz.getClassLoader();
- if (!(cl instanceof PluginClassLoader)) {
- throw new IllegalArgumentException(clazz + " is not initialized by " + PluginClassLoader.class);
+ if (!(cl instanceof io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader configuredPluginClassLoader)) { // Paper
+ throw new IllegalArgumentException(clazz + " is not initialized by a " + io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader.class); // Paper
}
- JavaPlugin plugin = ((PluginClassLoader) cl).plugin;
+ JavaPlugin plugin = configuredPluginClassLoader.getPlugin(); // Paper
if (plugin == null) {
throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer");
}
@@ -0,0 +0,0 @@ public abstract class JavaPlugin extends PluginBase {
public static JavaPlugin getProvidingPlugin(@NotNull Class<?> clazz) {
Preconditions.checkArgument(clazz != null, "Null class cannot have a plugin");
final ClassLoader cl = clazz.getClassLoader();
- if (!(cl instanceof PluginClassLoader)) {
- throw new IllegalArgumentException(clazz + " is not provided by " + PluginClassLoader.class);
+ if (!(cl instanceof io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader configuredPluginClassLoader)) { // Paper
+ throw new IllegalArgumentException(clazz + " is not provided by a " + io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader.class); // Paper
}
- JavaPlugin plugin = ((PluginClassLoader) cl).plugin;
+ JavaPlugin plugin = configuredPluginClassLoader.getPlugin(); // Paper
if (plugin == null) {
throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer");
}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@ -2001,11 +2037,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
private final Map<String, Class<?>> classes = new ConcurrentHashMap<String, Class<?>>();
private final PluginDescriptionFile description;
@@ -0,0 +0,0 @@ final class PluginClassLoader extends URLClassLoader {
private final Manifest manifest;
private final URL url;
private final ClassLoader libraryLoader;
- final JavaPlugin plugin;
+ public final JavaPlugin plugin; // Paper
private JavaPlugin pluginInit;
private IllegalStateException pluginState;
private final Set<String> seenIllegalAccess = Collections.newSetFromMap(new ConcurrentHashMap<>());
@ -2056,6 +2087,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ public void init(JavaPlugin plugin) {
+ this.initialize(plugin);
+ }
+
+ @Override
+ public JavaPlugin getPlugin() {
+ return this.plugin;
+ }
+ // Paper end
+
@Override

View file

@ -3514,18 +3514,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
if (false) { // Spigot - RL handles useTimings check now
eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} else {
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
*/
@org.jetbrains.annotations.ApiStatus.Internal // Paper
public final class PluginClassLoader extends URLClassLoader implements io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader { // Paper
+ public JavaPlugin getPlugin() { return plugin; } // Spigot
private final JavaPluginLoader loader;
private final Map<String, Class<?>> classes = new ConcurrentHashMap<String, Class<?>>();
private final PluginDescriptionFile description;
diff --git a/src/main/java/org/bukkit/util/CachedServerIcon.java b/src/main/java/org/bukkit/util/CachedServerIcon.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/util/CachedServerIcon.java

View file

@ -929,7 +929,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Nullable
+ public JavaPlugin getLoadedJavaPlugin() {
+ @Override
+ public JavaPlugin getPlugin() {
+ return this.loadedJavaPlugin;
+ }
+
@ -5819,7 +5820,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+
+ this.status = ProviderStatus.INITIALIZED;
+ return loader.plugin;
+ return loader.getPlugin();
+ } catch (Throwable ex) {
+ this.status = ProviderStatus.ERRORED;
+ SneakyThrow.sneaky(ex);
@ -6288,9 +6289,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@
package io.papermc.paper.util;
+import io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader;
+import io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.PluginClassLoader;
-import org.bukkit.plugin.java.PluginClassLoader;
import org.jetbrains.annotations.Nullable;
+import java.util.Objects;
@ -6307,10 +6308,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
- return classLoader.getPlugin();
+ ClassLoader classLoader = frame.getDeclaringClass().getClassLoader();
+ JavaPlugin plugin;
+ if (classLoader instanceof PaperPluginClassLoader pluginClassLoader) {
+ plugin = pluginClassLoader.getLoadedJavaPlugin();
+ } else if (classLoader instanceof PluginClassLoader spigotClassloader) {
+ plugin = spigotClassloader.getPlugin();
+ if (classLoader instanceof ConfiguredPluginClassLoader configuredPluginClassLoader) {
+ plugin = configuredPluginClassLoader.getPlugin();
+ } else {
+ plugin = null;
+ }