Browse Source

Merge 2ca3589fd7 into 74aa987ca9

pull/360/merge
Decebal Suiu 1 month ago
parent
commit
9b724463ac
No account linked to committer's email address

+ 1
- 1
pf4j/src/main/java/org/pf4j/Extension.java View File

@@ -55,7 +55,7 @@ public @interface Extension {
*
* @return classes of extension points, that are implemented by this extension
*/
Class<? extends ExtensionPoint>[] points() default {};
Class<?>[] points() default {};

/**
* An array of plugin IDs, that have to be available in order to load this extension.

+ 12
- 10
pf4j/src/main/java/org/pf4j/processor/ExtensionAnnotationProcessor.java View File

@@ -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<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;
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<String> getSupportedOptions() {
Set<String> options = new HashSet<>();
options.add(STORAGE_CLASS_NAME);
options.add(IGNORE_EXTENSION_POINT);
options.add(CHECK_EXTENSION_POINT);

return options;
}
@@ -182,11 +182,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<TypeElement> extensionPointElements = findExtensionPoints(extensionElement);
if (extensionPointElements.isEmpty()) {
@@ -194,6 +195,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);
@@ -239,7 +241,7 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
}

// pickup the first interface
if (extensionPointElements.isEmpty() && ignoreExtensionPoint) {
if (extensionPointElements.isEmpty() && !checkExtensionPoint) {
if (interfaces.isEmpty()) {
error(extensionElement, "Cannot use %s as extension point with %s compiler arg (it doesn't implement any interface)",
extensionElement, IGNORE_EXTENSION_POINT);
@@ -289,10 +291,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) {

+ 13
- 1
pf4j/src/test/java/org/pf4j/ExtensionAnnotationProcessorTest.java View File

@@ -146,11 +146,23 @@ 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() {
Compilation compilation = compile(JavaSources.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);

Loading…
Cancel
Save