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 org.jetbrains.annotations.NotNull;
+ +
+import java.io.Closeable; +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 + * 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. + * @param plugin the {@link JavaPlugin} that should be interlinked with this class loader.
+ */ + */
+ void init(JavaPlugin plugin); + 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 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 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 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 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java --- 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 Map<String, Class<?>> classes = new ConcurrentHashMap<String, Class<?>>();
private final PluginDescriptionFile description; private final PluginDescriptionFile description;
@@ -0,0 +0,0 @@ final class PluginClassLoader extends URLClassLoader { @@ -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 JavaPlugin pluginInit;
private IllegalStateException pluginState; private IllegalStateException pluginState;
private final Set<String> seenIllegalAccess = Collections.newSetFromMap(new ConcurrentHashMap<>()); private final Set<String> seenIllegalAccess = Collections.newSetFromMap(new ConcurrentHashMap<>());
@ -2056,6 +2087,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ public void init(JavaPlugin plugin) { + public void init(JavaPlugin plugin) {
+ this.initialize(plugin); + this.initialize(plugin);
+ } + }
+
+ @Override
+ public JavaPlugin getPlugin() {
+ return this.plugin;
+ }
+ // Paper end + // Paper end
+ +
@Override @Override

View File

@ -3514,18 +3514,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
if (false) { // Spigot - RL handles useTimings check now if (false) { // Spigot - RL handles useTimings check now
eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled())); eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} else { } 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 diff --git a/src/main/java/org/bukkit/util/CachedServerIcon.java b/src/main/java/org/bukkit/util/CachedServerIcon.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/util/CachedServerIcon.java --- a/src/main/java/org/bukkit/util/CachedServerIcon.java

View File

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