AbstractExtensionFinder implements ExtensionFinder, PluginStateListener { @Override @SuppressWarnings("unchecked") public <T> List<ExtensionWrapper<T>> find(Class<T> type) { log.debug("Finding extensions of extension point '{}'", type.getName()); Map<String, Set<String>> entries = getEntries(); List<ExtensionWrapper<T>> result = new ArrayList<>(); for (String pluginId : entries.keySet()) { List<ExtensionWrapper<T>> pluginExtensions = find(type, pluginId); result.addAll(pluginExtensions); } if (result.isEmpty()) { log.debug("No extensions found for extension point '{}'", type.getName()); } else { log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName()); } Collections.sort(result); return result; } AbstractExtensionFinder(PluginManager pluginManager); abstract Map<String, Set<String>> readPluginsStorages(); abstract Map<String, Set<String>> readClasspathStorages(); @Override @SuppressWarnings("unchecked") List<ExtensionWrapper<T>> find(Class<T> type); @Override @SuppressWarnings("unchecked") List<ExtensionWrapper<T>> find(Class<T> type, String pluginId); @Override List<ExtensionWrapper> find(String pluginId); @Override Set<String> findClassNames(String pluginId); @Override void pluginStateChanged(PluginStateEvent event); final boolean isCheckForExtensionDependencies(); void setCheckForExtensionDependencies(boolean checkForExtensionDependencies); static Extension findExtensionAnnotation(Class<?> clazz);  }
@Test public void testFindFailType() { ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) { @Override public Map<String, Set<String>> readPluginsStorages() { return Collections.emptyMap(); } @Override public Map<String, Set<String>> readClasspathStorages() { return Collections.emptyMap(); } }; List<ExtensionWrapper<FailTestPlugin>> list = instance.find(FailTestPlugin.class); assertEquals(0, list.size()); }
@Test public void testFindFromClasspath() { ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) { @Override public Map<String, Set<String>> readPluginsStorages() { return Collections.emptyMap(); } @Override public Map<String, Set<String>> readClasspathStorages() { Map<String, Set<String>> entries = new LinkedHashMap<>(); Set<String> bucket = new HashSet<>(); bucket.add("org.pf4j.plugin.TestExtension"); bucket.add("org.pf4j.plugin.FailTestExtension"); entries.put(null, bucket); return entries; } }; List<ExtensionWrapper<TestExtensionPoint>> list = instance.find(TestExtensionPoint.class); assertEquals(2, list.size()); }
@Test public void testFindFromPlugin() { ExtensionFinder instance = new AbstractExtensionFinder(pluginManager) { @Override public Map<String, Set<String>> readPluginsStorages() { Map<String, Set<String>> entries = new LinkedHashMap<>(); Set<String> bucket = new HashSet<>(); bucket.add("org.pf4j.plugin.TestExtension"); bucket.add("org.pf4j.plugin.FailTestExtension"); entries.put("plugin1", bucket); bucket = new HashSet<>(); bucket.add("org.pf4j.plugin.TestExtension"); entries.put("plugin2", bucket); return entries; } @Override public Map<String, Set<String>> readClasspathStorages() { return Collections.emptyMap(); } }; List<ExtensionWrapper<TestExtensionPoint>> list = instance.find(TestExtensionPoint.class); assertEquals(2, list.size()); list = instance.find(TestExtensionPoint.class, "plugin1"); assertEquals(2, list.size()); list = instance.find(TestExtensionPoint.class, "plugin2"); assertEquals(0, list.size()); }