diff options
3 files changed, 35 insertions, 9 deletions
diff --git a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java index caa415f..a20f8a8 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java +++ b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java @@ -146,7 +146,9 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin log.debug("Added extension '{}' with ordinal {}", className, extensionWrapper.getOrdinal()); } else { log.trace("'{}' is not an extension for extension point '{}'", className, type.getName()); - checkDifferentClassLoaders(type, extensionClass); + if (checkDifferentClassLoaders(type, extensionClass)) { + log.error("Different class loaders: '{}' (E) and '{}' (EP)", extensionClass.getClassLoader(), type.getClassLoader()); + } } } catch (ClassNotFoundException | NoClassDefFoundError e) { log.error(e.getMessage(), e); @@ -374,17 +376,13 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin return null; } - private void checkDifferentClassLoaders(Class<?> type, Class<?> extensionClass) { + boolean checkDifferentClassLoaders(Class<?> type, Class<?> extensionClass) { ClassLoader typeClassLoader = type.getClassLoader(); // class loader of extension point ClassLoader extensionClassLoader = extensionClass.getClassLoader(); boolean match = ClassUtils.getAllInterfacesNames(extensionClass).contains(type.getSimpleName()); - if (!match) { - log.error("Extension '{}' does not implement extension point '{}'", extensionClass, type); - } else if (!extensionClassLoader.equals(typeClassLoader)) { - // in this scenario the method 'isAssignableFrom' returns only FALSE - // see http://www.coderanch.com/t/557846/java/java/FWIW-FYI-isAssignableFrom-isInstance-differing - log.error("Different class loaders: '{}' (E) and '{}' (EP)", extensionClassLoader, typeClassLoader); - } + // in this scenario the method 'isAssignableFrom' returns only FALSE + // see http://www.coderanch.com/t/557846/java/java/FWIW-FYI-isAssignableFrom-isInstance-differing + return match && extensionClassLoader != typeClassLoader; } } diff --git a/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java b/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java index 5d31f5a..796106d 100644 --- a/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java +++ b/pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java @@ -36,6 +36,7 @@ import java.util.Set; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -282,4 +283,29 @@ public class AbstractExtensionFinderTest { Assertions.assertNull(result); } + + @Test + void checkDifferentClassLoaders() { + AbstractExtensionFinder extensionFinder = new AbstractExtensionFinder(pluginManager) { + + @Override + public Map<String, Set<String>> readPluginsStorages() { + return Collections.emptyMap(); + } + + @Override + public Map<String, Set<String>> readClasspathStorages() { + return Collections.emptyMap(); + } + + }; + + List<JavaFileObject> generatedFiles = JavaSources.compileAll(JavaSources.Greeting, JavaSources.WhazzupGreeting); + assertEquals(2, generatedFiles.size()); + Class<?> extensionPointClass = new JavaFileObjectClassLoader().load(generatedFiles).get(JavaSources.GREETING_CLASS_NAME); + Class<?> extensionClass = new JavaFileObjectClassLoader().load(generatedFiles).get(JavaSources.WHAZZUP_GREETING_CLASS_NAME); + + assertTrue(extensionFinder.checkDifferentClassLoaders(extensionPointClass, extensionClass)); + } + } diff --git a/pf4j/src/test/java/org/pf4j/test/JavaSources.java b/pf4j/src/test/java/org/pf4j/test/JavaSources.java index 2031edc..e3b08fe 100644 --- a/pf4j/src/test/java/org/pf4j/test/JavaSources.java +++ b/pf4j/src/test/java/org/pf4j/test/JavaSources.java @@ -30,6 +30,7 @@ import static com.google.testing.compile.Compiler.javac; */ public class JavaSources { + public static final String GREETING_CLASS_NAME = "test.Greeting"; public static final JavaFileObject Greeting = JavaFileObjects.forSourceLines("Greeting", "package test;", "import org.pf4j.ExtensionPoint;", @@ -38,6 +39,7 @@ public class JavaSources { " String getGreeting();", "}"); + public static final String WHAZZUP_GREETING_CLASS_NAME = "test.WhazzupGreeting"; public static final JavaFileObject WhazzupGreeting = JavaFileObjects.forSourceLines("WhazzupGreeting", "package test;", "import org.pf4j.Extension;", |