Skip to content

Commit

Permalink
Add test for AbstractExtensionFinder#checkDifferentClassLoaders
Browse files Browse the repository at this point in the history
  • Loading branch information
decebals committed Nov 28, 2024
1 parent 3ff696d commit fb43bbf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
16 changes: 7 additions & 9 deletions pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public <T> List<ExtensionWrapper<T>> find(Class<T> type, String pluginId) {
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);
Expand Down Expand Up @@ -374,17 +376,13 @@ public static Extension findExtensionAnnotation(Class<?> clazz) {
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;
}

}
26 changes: 26 additions & 0 deletions pf4j/src/test/java/org/pf4j/AbstractExtensionFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;

Expand Down Expand Up @@ -282,4 +283,29 @@ public void runningOnNonExtensionKotlinClassDoesNotThrowException() {

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));
}

}
2 changes: 2 additions & 0 deletions pf4j/src/test/java/org/pf4j/test/JavaSources.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
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;",
Expand All @@ -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;",
Expand Down

0 comments on commit fb43bbf

Please sign in to comment.