From 08bdbbb36249c143abde35128342a899fb4e0f38 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Fri, 20 Dec 2019 23:17:30 +0200 Subject: [PATCH] Relax the concept of extension point --- pf4j/src/main/java/org/pf4j/Extension.java | 2 +- .../ExtensionAnnotationProcessor.java | 22 ++++++++++--------- .../ExtensionAnnotationProcessorTest.java | 14 +++++++++++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/Extension.java b/pf4j/src/main/java/org/pf4j/Extension.java index 57a435c..b9fe6a1 100644 --- a/pf4j/src/main/java/org/pf4j/Extension.java +++ b/pf4j/src/main/java/org/pf4j/Extension.java @@ -45,7 +45,7 @@ public @interface Extension { * * @return classes of extension points, that are implemented by this extension */ - Class[] points() default {}; + Class[] points() default {}; /** * An array of plugin IDs, that have to be available in order to load this extension. diff --git a/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java b/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java index 797fe04..29c44e3 100644 --- a/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java +++ b/pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java @@ -51,14 +51,14 @@ 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"; + public static final String STORAGE_CLASS_NAME = "pf4j.storageClassName"; + public static final String CHECK_EXTENSION_POINT = "pf4j.checkExtensionPoint"; private Map> extensions = new HashMap<>(); // the key is the extension point private Map> oldExtensions = new HashMap<>(); // the key is the extension point private ExtensionStorage storage; - private boolean ignoreExtensionPoint; + private boolean checkExtensionPoint; @Override public synchronized void init(ProcessingEnvironment processingEnv) { @@ -68,7 +68,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { info("Options %s", processingEnv.getOptions()); initStorage(); - initIgnoreExtensionPoint(); + initCheckExtensionPoint(); } @Override @@ -85,7 +85,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { public Set getSupportedOptions() { Set options = new HashSet<>(); options.add(STORAGE_CLASS_NAME); - options.add(IGNORE_EXTENSION_POINT); + options.add(CHECK_EXTENSION_POINT); return options; } @@ -180,11 +180,12 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { } // check if class extends/implements an extension point - if (!ignoreExtensionPoint && !isExtension(element.asType())) { + if (checkExtensionPoint && !isExtension(element.asType())) { error(element, "%s is not an extension (it doesn't implement ExtensionPoint)", element); return; } + // collect all extension points of extension TypeElement extensionElement = (TypeElement) element; List extensionPointElements = findExtensionPoints(extensionElement); if (extensionPointElements.isEmpty()) { @@ -192,6 +193,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { return; } + // add the extension together with the extension point in the list with extensions String extension = getBinaryName(extensionElement); for (TypeElement extensionPointElement : extensionPointElements) { String extensionPoint = getBinaryName(extensionPointElement); @@ -237,7 +239,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { } // pickup the first interface - if (extensionPointElements.isEmpty() && ignoreExtensionPoint) { + if (extensionPointElements.isEmpty() && !checkExtensionPoint) { if (interfaces.isEmpty()) { error(extensionElement, "%s is not an extension (it doesn't implement any interface)", extensionElement); } else if (interfaces.size() == 1) { @@ -285,10 +287,10 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { } } - private void initIgnoreExtensionPoint() { + private void initCheckExtensionPoint() { // search in processing options and system properties - ignoreExtensionPoint = getProcessingEnvironment().getOptions().containsKey(IGNORE_EXTENSION_POINT) || - System.getProperty(IGNORE_EXTENSION_POINT) != null; + checkExtensionPoint = getProcessingEnvironment().getOptions().containsKey(CHECK_EXTENSION_POINT) || + System.getProperty(CHECK_EXTENSION_POINT) != null; } private TypeElement getElement(TypeMirror typeMirror) { diff --git a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java index 081be99..11467c8 100644 --- a/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java +++ b/pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java @@ -145,12 +145,24 @@ public class ExtensionAnnotationProcessorTest { assertThat(compilation).succeededWithoutWarnings(); } + @Test + public void compileWithErrorNoExtensionPoint() { + ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); + Compilation compilation = javac().withProcessors(processor).withOptions("-A" + ExtensionAnnotationProcessor.CHECK_EXTENSION_POINT) + .compile(Greeting, WhazzupGreeting_NoExtensionPoint); + assertThat(compilation).failed(); + assertThat(compilation).hadErrorContaining("it doesn't implement ExtensionPoint") + .inFile(WhazzupGreeting_NoExtensionPoint) + .onLine(5) + .atColumn(8); + } + @Test public void compileWithError() { ExtensionAnnotationProcessor processor = new ExtensionAnnotationProcessor(); Compilation compilation = javac().withProcessors(processor).compile(Greeting, WhazzupGreeting_NoExtensionPoint); assertThat(compilation).failed(); - assertThat(compilation).hadErrorContaining("it doesn't implement ExtensionPoint") + assertThat(compilation).hadErrorContaining("it doesn't implement any interface") .inFile(WhazzupGreeting_NoExtensionPoint) .onLine(5) .atColumn(8); -- 2.39.5