Fix JavaPlugin static getPlugin methods (#8888)

This commit is contained in:
Jake Potrebic 2023-02-21 19:27:49 -08:00 committed by GitHub
parent ddc0a99f52
commit bb63a6156d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 54 deletions

View file

@ -913,10 +913,10 @@ index 0000000000000000000000000000000000000000..64e46fdfa4d404cb08c67a456e5990b7
+}
diff --git a/src/main/java/io/papermc/paper/plugin/provider/classloader/ConfiguredPluginClassLoader.java b/src/main/java/io/papermc/paper/plugin/provider/classloader/ConfiguredPluginClassLoader.java
new file mode 100644
index 0000000000000000000000000000000000000000..a15e7474eb8eb1196f69070b7cf7fd5af6100f68
index 0000000000000000000000000000000000000000..ab1fb50912e411e708ba1e40303b1c08dabb086d
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/provider/classloader/ConfiguredPluginClassLoader.java
@@ -0,0 +1,55 @@
@@ -0,0 +1,63 @@
+package io.papermc.paper.plugin.provider.classloader;
+
+import io.papermc.paper.plugin.configuration.PluginMeta;
@ -925,6 +925,7 @@ index 0000000000000000000000000000000000000000..a15e7474eb8eb1196f69070b7cf7fd5a
+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..a15e7474eb8eb1196f69070b7cf7fd5a
+ * @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
@ -1835,7 +1843,7 @@ index a80251eff75430863b37db1c131e22593f3fcd5e..310c4041963a3f1e0a26e39a6da12a9b
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
index 669a70faa95d0d6525a731d73499ed6fb0b48320..6175b04327b12e74140a0885f7326546dfaf269a 100644
index 669a70faa95d0d6525a731d73499ed6fb0b48320..c9cf9d361a1289aba383718733a2098b5eafb38f 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
@@ -38,6 +38,7 @@ public abstract class JavaPlugin extends PluginBase {
@ -1941,6 +1949,34 @@ index 669a70faa95d0d6525a731d73499ed6fb0b48320..6175b04327b12e74140a0885f7326546
}
/**
@@ -394,10 +419,10 @@ 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");
}
@@ -420,10 +445,10 @@ 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 047c0304fd617cec990f80815b43916c6ef5a94c..fa39c93d76ebb9eecce1f4b5203cb361e4778b4f 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@ -1987,7 +2023,7 @@ index 6d634b0ea813ccb19f1562a7d0e5a59cea4eab21..f9e67e20133d349e43d126dbb48b34d4
private final Logger logger;
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f668e8e8a 100644
index 2f74ec96ece706de23156ebabfe493211bc05391..dd569f2fc6098650d202e834b343b1bff2d42284 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -29,7 +29,8 @@ import org.jetbrains.annotations.Nullable;
@ -2000,12 +2036,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
private final JavaPluginLoader loader;
private final Map<String, Class<?>> classes = new ConcurrentHashMap<String, Class<?>>();
private final PluginDescriptionFile description;
@@ -39,20 +40,22 @@ 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
@@ -43,16 +44,18 @@ final class PluginClassLoader extends URLClassLoader {
private JavaPlugin pluginInit;
private IllegalStateException pluginState;
private final Set<String> seenIllegalAccess = Collections.newSetFromMap(new ConcurrentHashMap<>());
@ -2038,7 +2069,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
try {
Class<?> jarClass;
try {
@@ -94,6 +101,22 @@ final class PluginClassLoader extends URLClassLoader {
@@ -94,6 +101,27 @@ final class PluginClassLoader extends URLClassLoader {
return findResources(name);
}
@ -2056,12 +2087,17 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
+ public void init(JavaPlugin plugin) {
+ this.initialize(plugin);
+ }
+
+ @Override
+ public JavaPlugin getPlugin() {
+ return this.plugin;
+ }
+ // Paper end
+
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
return loadClass0(name, resolve, true, true);
@@ -119,26 +142,11 @@ final class PluginClassLoader extends URLClassLoader {
@@ -119,26 +147,11 @@ final class PluginClassLoader extends URLClassLoader {
if (checkGlobal) {
// This ignores the libraries of other plugins, unless they are transitive dependencies.
@ -2090,7 +2126,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
return result;
}
@@ -167,7 +175,7 @@ final class PluginClassLoader extends URLClassLoader {
@@ -167,7 +180,7 @@ final class PluginClassLoader extends URLClassLoader {
throw new ClassNotFoundException(name, ex);
}
@ -2099,7 +2135,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
int dot = name.lastIndexOf('.');
if (dot != -1) {
@@ -197,8 +205,8 @@ final class PluginClassLoader extends URLClassLoader {
@@ -197,8 +210,8 @@ final class PluginClassLoader extends URLClassLoader {
result = super.findClass(name);
}
@ -2109,7 +2145,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
}
return result;
@@ -207,6 +215,12 @@ final class PluginClassLoader extends URLClassLoader {
@@ -207,6 +220,12 @@ final class PluginClassLoader extends URLClassLoader {
@Override
public void close() throws IOException {
try {
@ -2122,7 +2158,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
super.close();
} finally {
jar.close();
@@ -218,7 +232,7 @@ final class PluginClassLoader extends URLClassLoader {
@@ -218,7 +237,7 @@ final class PluginClassLoader extends URLClassLoader {
return classes.values();
}
@ -2131,7 +2167,7 @@ index 2f74ec96ece706de23156ebabfe493211bc05391..f92071617712a0f410475fae291fb85f
Preconditions.checkArgument(javaPlugin != null, "Initializing plugin cannot be null");
Preconditions.checkArgument(javaPlugin.getClass().getClassLoader() == this, "Cannot initialize plugin outside of this class loader");
if (this.plugin != null || this.pluginInit != null) {
@@ -228,6 +242,32 @@ final class PluginClassLoader extends URLClassLoader {
@@ -228,6 +247,32 @@ final class PluginClassLoader extends URLClassLoader {
pluginState = new IllegalStateException("Initial initialization");
this.pluginInit = javaPlugin;

View file

@ -3472,7 +3472,7 @@ index f64a90241ce7f73e111e6b5ac6ff87cd93583c0e..f13dcbe448111b24b36105d25527ba87
// Paper start
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
index d0ad072c832b8fc8a1cfdcafdd42c724531a2e29..02062284dbc1a6c923ceffa5e4d6a3be8c7429d3 100644
index fa39c93d76ebb9eecce1f4b5203cb361e4778b4f..aa6ce50ff2d0318bfcc328daa4fd9f2e50d34d01 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@@ -55,7 +55,6 @@ public final class JavaPluginLoader implements PluginLoader {
@ -3514,18 +3514,6 @@ index d0ad072c832b8fc8a1cfdcafdd42c724531a2e29..02062284dbc1a6c923ceffa5e4d6a3be
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 e721c58eafee2180d4ba1a73002cf850355a366e..39c9253d03bae99f8b5b19d22295f6172606d57b 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -31,6 +31,7 @@ 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 5ca863b3692b2e1b58e7fb4d82f554a92cc4f01e..612958a331575d1da2715531ebdf6b1168f2e860 100644
--- a/src/main/java/org/bukkit/util/CachedServerIcon.java

View file

@ -67,7 +67,7 @@ index 0000000000000000000000000000000000000000..087ee57fe5485bc760fadd45a176d4d9
+
+}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
index 79df67daf2fe8193fd83dd6a7bfc78b3f6e524c2..0e4cedc005466c600ff6b9d500febf338b12a842 100644
index 71c8d2345eef6895edb8d210553ec3cddd9c76d0..6d31f3a2569ae9c522a5e6cddd38ac8f252f1bfe 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
@@ -44,7 +44,7 @@ public abstract class JavaPlugin extends PluginBase {
@ -93,10 +93,10 @@ index 79df67daf2fe8193fd83dd6a7bfc78b3f6e524c2..0e4cedc005466c600ff6b9d500febf33
/**
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index 845b3f8a7dedf801d45489e807ab971dcfe2382e..666f182e5cf2f07a2cf6a17b0d28a100b3f0b891 100644
index dd569f2fc6098650d202e834b343b1bff2d42284..fade45ef475ae20922f5abea49a0f035d19b7819 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -65,7 +65,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@@ -64,7 +64,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
this.url = file.toURI().toURL();
this.libraryLoader = libraryLoader;
@ -105,7 +105,7 @@ index 845b3f8a7dedf801d45489e807ab971dcfe2382e..666f182e5cf2f07a2cf6a17b0d28a100
// Paper start
this.classLoaderGroup = io.papermc.paper.plugin.provider.classloader.PaperClassLoaderStorage.instance().registerSpigotGroup(this); // Paper
// Paper end
@@ -243,6 +243,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@@ -247,6 +247,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
pluginState = new IllegalStateException("Initial initialization");
this.pluginInit = javaPlugin;

View file

@ -5,10 +5,10 @@ Subject: [PATCH] Enable multi-release plugin jars
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index 666f182e5cf2f07a2cf6a17b0d28a100b3f0b891..14cdd833bb56186e7cb83e035348f4dc942e4740 100644
index fade45ef475ae20922f5abea49a0f035d19b7819..264d712d3399d24cb01b85fc2d055d9fbf11b23a 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -60,7 +60,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@@ -59,7 +59,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
this.description = description;
this.dataFolder = dataFolder;
this.file = file;

View file

@ -5,10 +5,10 @@ Subject: [PATCH] Rewrite LogEvents to contain the source jars in stack traces
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index 14cdd833bb56186e7cb83e035348f4dc942e4740..9b4b81f70624cd3906c94cbab99aabe7f9a4223e 100644
index 264d712d3399d24cb01b85fc2d055d9fbf11b23a..327ff03fa1978881fa6f6ba20e33e3c049c2e3cd 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -54,7 +54,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@@ -53,7 +53,7 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@org.jetbrains.annotations.ApiStatus.Internal // Paper
public PluginClassLoader(@Nullable final ClassLoader parent, @NotNull final PluginDescriptionFile description, @NotNull final File dataFolder, @NotNull final File file, @Nullable ClassLoader libraryLoader) throws IOException, InvalidPluginException, MalformedURLException { // Paper

View file

@ -5,10 +5,10 @@ Subject: [PATCH] Also load resources from LibraryLoader
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index 9b4b81f70624cd3906c94cbab99aabe7f9a4223e..4666d29d27e6247422a94e99511a190ce9484f76 100644
index 327ff03fa1978881fa6f6ba20e33e3c049c2e3cd..7d300a539ac2ef1c773cfa90cecc8655490a8686 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -94,14 +94,35 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@@ -93,14 +93,35 @@ public final class PluginClassLoader extends URLClassLoader implements io.paperm
@Override
public URL getResource(String name) {

View file

@ -746,10 +746,10 @@ index 0000000000000000000000000000000000000000..f9a2c55a354c877749db3f92956de802
+}
diff --git a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java
new file mode 100644
index 0000000000000000000000000000000000000000..f1a2d25079c841daff19f41f2b24fb8e97d2c7d3
index 0000000000000000000000000000000000000000..4fa7147d0dcbfd7e40f6eab1f7b0c6d8bbcf6906
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java
@@ -0,0 +1,193 @@
@@ -0,0 +1,194 @@
+package io.papermc.paper.plugin.entrypoint.classloader;
+
+import io.papermc.paper.plugin.configuration.PluginMeta;
@ -929,7 +929,8 @@ index 0000000000000000000000000000000000000000..f1a2d25079c841daff19f41f2b24fb8e
+ }
+
+ @Nullable
+ public JavaPlugin getLoadedJavaPlugin() {
+ @Override
+ public JavaPlugin getPlugin() {
+ return this.loadedJavaPlugin;
+ }
+
@ -5682,7 +5683,7 @@ index 0000000000000000000000000000000000000000..b2a6544e321fa61c58bdf5684231de10
+}
diff --git a/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java b/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..0da361611bd93bc3299b8559d5c7c2127a8a6f0f
index 0000000000000000000000000000000000000000..600023770e19f02258327816122298f58d73e4ab
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java
@@ -0,0 +1,188 @@
@ -5819,7 +5820,7 @@ index 0000000000000000000000000000000000000000..0da361611bd93bc3299b8559d5c7c212
+
+
+ this.status = ProviderStatus.INITIALIZED;
+ return loader.plugin;
+ return loader.getPlugin();
+ } catch (Throwable ex) {
+ this.status = ProviderStatus.ERRORED;
+ SneakyThrow.sneaky(ex);
@ -6282,22 +6283,22 @@ index 0000000000000000000000000000000000000000..fd55fd1d6518ebd1bc2513dd331f0720
+ }
+}
diff --git a/src/main/java/io/papermc/paper/util/StackWalkerUtil.java b/src/main/java/io/papermc/paper/util/StackWalkerUtil.java
index f7114d5b8f2f93f62883e24da29afaf9f74ee1a6..00e78a495575abb9496b1849822605b613afe3b4 100644
index f7114d5b8f2f93f62883e24da29afaf9f74ee1a6..8bf0630c0e06950cd99b7ae9898137f70c22063f 100644
--- a/src/main/java/io/papermc/paper/util/StackWalkerUtil.java
+++ b/src/main/java/io/papermc/paper/util/StackWalkerUtil.java
@@ -1,9 +1,11 @@
@@ -1,9 +1,10 @@
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;
import java.util.Optional;
public class StackWalkerUtil {
@@ -12,11 +14,20 @@ public class StackWalkerUtil {
@@ -12,11 +13,18 @@ public class StackWalkerUtil {
public static JavaPlugin getFirstPluginCaller() {
Optional<JavaPlugin> foundFrame = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(stream -> stream
@ -6307,10 +6308,8 @@ index f7114d5b8f2f93f62883e24da29afaf9f74ee1a6..00e78a495575abb9496b1849822605b6
- 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;
+ }