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

* *
* @return classes of extension points, that are implemented by this 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. * 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

*/ */
public class ExtensionAnnotationProcessor extends AbstractProcessor { 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>> extensions = new HashMap<>(); // the key is the extension point
private Map<String, Set<String>> oldExtensions = 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 ExtensionStorage storage;
private boolean ignoreExtensionPoint;
private boolean checkExtensionPoint;


@Override @Override
public synchronized void init(ProcessingEnvironment processingEnv) { public synchronized void init(ProcessingEnvironment processingEnv) {
info("Options %s", processingEnv.getOptions()); info("Options %s", processingEnv.getOptions());


initStorage(); initStorage();
initIgnoreExtensionPoint();
initCheckExtensionPoint();
} }


@Override @Override
public Set<String> getSupportedOptions() { public Set<String> getSupportedOptions() {
Set<String> options = new HashSet<>(); Set<String> options = new HashSet<>();
options.add(STORAGE_CLASS_NAME); options.add(STORAGE_CLASS_NAME);
options.add(IGNORE_EXTENSION_POINT);
options.add(CHECK_EXTENSION_POINT);


return options; return options;
} }
} }


// check if class extends/implements an extension point // 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); error(element, "%s is not an extension (it doesn't implement ExtensionPoint)", element);
return; return;
} }


// collect all extension points of extension
TypeElement extensionElement = (TypeElement) element; TypeElement extensionElement = (TypeElement) element;
List<TypeElement> extensionPointElements = findExtensionPoints(extensionElement); List<TypeElement> extensionPointElements = findExtensionPoints(extensionElement);
if (extensionPointElements.isEmpty()) { if (extensionPointElements.isEmpty()) {
return; return;
} }


// add the extension together with the extension point in the list with extensions
String extension = getBinaryName(extensionElement); String extension = getBinaryName(extensionElement);
for (TypeElement extensionPointElement : extensionPointElements) { for (TypeElement extensionPointElement : extensionPointElements) {
String extensionPoint = getBinaryName(extensionPointElement); String extensionPoint = getBinaryName(extensionPointElement);
} }


// pickup the first interface // pickup the first interface
if (extensionPointElements.isEmpty() && ignoreExtensionPoint) {
if (extensionPointElements.isEmpty() && !checkExtensionPoint) {
if (interfaces.isEmpty()) { if (interfaces.isEmpty()) {
error(extensionElement, "Cannot use %s as extension point with %s compiler arg (it doesn't implement any interface)", error(extensionElement, "Cannot use %s as extension point with %s compiler arg (it doesn't implement any interface)",
extensionElement, IGNORE_EXTENSION_POINT); extensionElement, IGNORE_EXTENSION_POINT);
} }
} }


private void initIgnoreExtensionPoint() {
private void initCheckExtensionPoint() {
// search in processing options and system properties // 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) { private TypeElement getElement(TypeMirror typeMirror) {

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

assertThat(compilation).succeededWithoutWarnings(); 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 @Test
public void compileWithError() { public void compileWithError() {
Compilation compilation = compile(JavaSources.Greeting, WhazzupGreeting_NoExtensionPoint); Compilation compilation = compile(JavaSources.Greeting, WhazzupGreeting_NoExtensionPoint);
assertThat(compilation).failed(); assertThat(compilation).failed();
assertThat(compilation).hadErrorContaining("it doesn't implement ExtensionPoint")
assertThat(compilation).hadErrorContaining("it doesn't implement any interface")
.inFile(WhazzupGreeting_NoExtensionPoint) .inFile(WhazzupGreeting_NoExtensionPoint)
.onLine(5) .onLine(5)
.atColumn(8); .atColumn(8);

Loading…
Cancel
Save