*/
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) {
info("Options %s", processingEnv.getOptions());
initStorage();
- initIgnoreExtensionPoint();
+ initCheckExtensionPoint();
}
@Override
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;
}
}
// 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()) {
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);
}
// 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) {
}
}
- 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) {
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);