From eac554e2ffd528bd06960d8bd3850cd7b9a0b709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20L=C3=B6vdahl?= Date: Mon, 11 Mar 2024 10:13:54 +0200 Subject: [PATCH] Avoid instantiating Plugin class if a plugin is already unloaded Given the following preconditions: - `PluginClassLoader` is configured to use `ClassLoadingStrategy` `PDA`, `DPA` or `DAP`. - There is a `PluginStateListener` that calls `event.getPlugin().getPlugin()`. - A plugin (`plugin-a`) with a missing dependency is loaded (`plugin-a`). - The `Plugin` class in `plugin-a` extends, implements or references a class in either `plugin-b` or in the main application. In that scenario, when `plugin-a` is unloaded by pf4j, and the `PluginStateListener` is run, the `event.getPlugin().getPlugin()` call will fail with a `NullPointerException`. This is caused by the class loading done when instantiating the `Plugin` class in `plugin-a`. Because not all classes needed are found in `plugin-a` itself, pf4j will continue by looking up classes in `plugin-a`'s dependencies. Because `plugin-b` is missing and has no associated `ClassLoader`. --- pf4j/src/main/java/org/pf4j/PluginWrapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pf4j/src/main/java/org/pf4j/PluginWrapper.java b/pf4j/src/main/java/org/pf4j/PluginWrapper.java index 21f7b25..54b5376 100644 --- a/pf4j/src/main/java/org/pf4j/PluginWrapper.java +++ b/pf4j/src/main/java/org/pf4j/PluginWrapper.java @@ -90,7 +90,7 @@ public class PluginWrapper { * @return the plugin instance */ public Plugin getPlugin() { - if (plugin == null) { + if (plugin == null && pluginState != PluginState.UNLOADED) { plugin = pluginFactory.create(this); }