Browse Source

check that a java type is an ExtensionPoint

pull/4/head
Decebal Suiu 11 years ago
parent
commit
d89e4bcead
  1. 27
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java

27
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java

@ -48,7 +48,14 @@ public class DefaultExtensionFinder implements ExtensionFinder {
@Override @Override
public <T> List<ExtensionWrapper<T>> find(Class<T> type) { public <T> List<ExtensionWrapper<T>> find(Class<T> type) {
log.debug("Find extensions for extension point {}", type.getName()); log.debug("Checking extension point '{}'", type.getName());
if (!isExtensionPoint(type)) {
log.warn("'{}' is not an extension point", type.getName());
return Collections.emptyList(); // or return null ?!
}
log.debug("Finding extensions for extension point '{}'", type.getName());
List<ExtensionWrapper<T>> result = new ArrayList<ExtensionWrapper<T>>(); List<ExtensionWrapper<T>> result = new ArrayList<ExtensionWrapper<T>>();
if (entries == null) { if (entries == null) {
entries = readIndexFiles(); entries = readIndexFiles();
@ -57,16 +64,16 @@ public class DefaultExtensionFinder implements ExtensionFinder {
for (String entry : entries) { for (String entry : entries) {
try { try {
Class<?> extensionType = classLoader.loadClass(entry); Class<?> extensionType = classLoader.loadClass(entry);
log.debug("Checking extension type {}", extensionType.getName()); log.debug("Checking extension type '{}'", extensionType.getName());
if (type.isAssignableFrom(extensionType)) { if (type.isAssignableFrom(extensionType)) {
Object instance = extensionFactory.create(extensionType); Object instance = extensionFactory.create(extensionType);
if (instance != null) { if (instance != null) {
Extension extension = extensionType.getAnnotation(Extension.class); Extension extension = extensionType.getAnnotation(Extension.class);
log.debug("Added extension {} with ordinal {}", extensionType.getName(), extension.ordinal()); log.debug("Added extension '{}' with ordinal {}", extensionType.getName(), extension.ordinal());
result.add(new ExtensionWrapper<T>(type.cast(instance), extension.ordinal())); result.add(new ExtensionWrapper<T>(type.cast(instance), extension.ordinal()));
} }
} else { } else {
log.warn("{} is not an extension for extension point {}", extensionType.getName(), type.getName()); log.warn("'{}' is not an extension for extension point '{}'", extensionType.getName(), type.getName());
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -74,9 +81,9 @@ public class DefaultExtensionFinder implements ExtensionFinder {
} }
if (entries.isEmpty()) { if (entries.isEmpty()) {
log.debug("No extensions found for extension point {}", type.getName()); log.debug("No extensions found for extension point '{}'", type.getName());
} else { } else {
log.debug("Found {} extensions for extension point {}", entries.size(), type.getName()); log.debug("Found {} extensions for extension point '{}'", entries.size(), type.getName());
} }
// sort by "ordinal" property // sort by "ordinal" property
@ -94,7 +101,7 @@ public class DefaultExtensionFinder implements ExtensionFinder {
@Override @Override
public Object create(Class<?> extensionType) { public Object create(Class<?> extensionType) {
log.debug("Create instance for extension {}", extensionType.getName()); log.debug("Create instance for extension '{}'", extensionType.getName());
try { try {
return extensionType.newInstance(); return extensionType.newInstance();
@ -131,7 +138,11 @@ public class DefaultExtensionFinder implements ExtensionFinder {
} }
return entries; return entries;
} }
private boolean isExtensionPoint(Class type) {
return ExtensionPoint.class.isAssignableFrom(type);
}
/** /**
* Creates an extension instance. * Creates an extension instance.

Loading…
Cancel
Save