aboutsummaryrefslogtreecommitdiffstats
path: root/pf4j
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2019-11-16 00:40:40 +0200
committerGitHub <noreply@github.com>2019-11-16 00:40:40 +0200
commit45eeec8a724029d9f8c2175528e1b0a975de205e (patch)
tree8c3c7577b79effcb0facefd79768a56b3911b8ca /pf4j
parent6d442e97d200b01a2546e61a01a6e530eb9e1cf2 (diff)
downloadpf4j-45eeec8a724029d9f8c2175528e1b0a975de205e.tar.gz
pf4j-45eeec8a724029d9f8c2175528e1b0a975de205e.zip
Support any interface as an ExtensionPoint (#350)
Diffstat (limited to 'pf4j')
-rw-r--r--pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java37
-rw-r--r--pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java2
2 files changed, 32 insertions, 7 deletions
diff --git a/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java b/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java
index 9c629de..2faf143 100644
--- a/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java
+++ b/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java
@@ -52,19 +52,23 @@ import java.util.TreeSet;
public class ExtensionAnnotationProcessor extends AbstractProcessor {
private static final String STORAGE_CLASS_NAME = "pf4j.storageClassName";
+ private static final String IGNORE_EXTENSION_POINT = "pf4j.ignoreExtensionPoint";
private Map<String, Set<String>> extensions = new HashMap<>(); // the key is the extension point
private Map<String, Set<String>> oldExtensions = new HashMap<>(); // the key is the extension point
private ExtensionStorage storage;
+ private boolean ignoreExtensionPoint;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
info("%s init", ExtensionAnnotationProcessor.class.getName());
+ info("Options %s", processingEnv.getOptions());
initStorage();
+ initIgnoreExtensionPoint();
}
@Override
@@ -81,6 +85,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
public Set<String> getSupportedOptions() {
Set<String> options = new HashSet<>();
options.add(STORAGE_CLASS_NAME);
+ options.add(IGNORE_EXTENSION_POINT);
return options;
}
@@ -186,11 +191,11 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
// detect extension points automatically, if they are not explicitly configured (default behaviour)
else {
// search in interfaces
- for (TypeMirror item : extensionElement.getInterfaces()) {
+ List<? extends TypeMirror> interfaces = extensionElement.getInterfaces();
+ for (TypeMirror item : interfaces) {
boolean isExtensionPoint = processingEnv.getTypeUtils().isSubtype(item, getExtensionPointType());
if (isExtensionPoint) {
- TypeElement extensionPointElement = (TypeElement) ((DeclaredType) item).asElement();
- extensionPointElements.add(extensionPointElement);
+ extensionPointElements.add(getElement(item));
}
}
@@ -199,8 +204,18 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
if (superclass.getKind() != TypeKind.NONE) {
boolean isExtensionPoint = processingEnv.getTypeUtils().isSubtype(superclass, getExtensionPointType());
if (isExtensionPoint) {
- TypeElement extensionPointElement = (TypeElement) ((DeclaredType) superclass).asElement();
- extensionPointElements.add(extensionPointElement);
+ extensionPointElements.add(getElement(superclass));
+ }
+ }
+
+ // pickup the first interface
+ if (extensionPointElements.isEmpty() && ignoreExtensionPoint) {
+ if (interfaces.isEmpty()) {
+ error(extensionElement, "%s is not an extension (it doesn't implement any interface)", extensionElement);
+ } else if (interfaces.size() == 1) {
+ extensionPointElements.add(getElement(interfaces.get(0)));
+ } else {
+ error(extensionElement, "%s is not an extension (it implements multiple interfaces)", extensionElement);
}
}
}
@@ -242,6 +257,12 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
}
}
+ private void initIgnoreExtensionPoint() {
+ // search in processing options and system properties
+ ignoreExtensionPoint = getProcessingEnvironment().getOptions().containsKey(IGNORE_EXTENSION_POINT) ||
+ System.getProperty(IGNORE_EXTENSION_POINT) != null;
+ }
+
private void processExtensionElement(Element element) {
// check if @Extension is put on class and not on method or constructor
if (!(element instanceof TypeElement)) {
@@ -250,7 +271,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
}
// check if class extends/implements an extension point
- if (!isExtension(element.asType())) {
+ if (!ignoreExtensionPoint && !isExtension(element.asType())) {
error(element, "%s is not an extension (it doesn't implement ExtensionPoint)", element);
return;
}
@@ -270,4 +291,8 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
}
}
+ private TypeElement getElement(TypeMirror typeMirror) {
+ return (TypeElement) ((DeclaredType) typeMirror).asElement();
+ }
+
}
diff --git a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java
index 4f8dd2e..081be99 100644
--- a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java
+++ b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java
@@ -115,7 +115,7 @@ public class ExtensionAnnotationProcessorTest {
public void getSupportedOptions() {
ExtensionAnnotationProcessor instance = new ExtensionAnnotationProcessor();
Set<String> result = instance.getSupportedOptions();
- assertEquals(1, result.size());
+ assertEquals(2, result.size());
}
@Test