use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event in project mvp4g2 by mvp4g.
the class PresenterTest method testPresenterWithWrongImplementation01.
@Test
public void testPresenterWithWrongImplementation01() {
Compilation compilation = javac().withProcessors(new Mvp4g2Processor()).compile(new ArrayList<JavaFileObject>() {
{
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation01/EventBusHandlerWithNotImplementedEvent.java"));
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation01/MockShellPresenter01.java"));
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation01/IMockShellView01.java"));
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation01/MockShellView01.java"));
}
});
CompilationSubject.assertThat(compilation).failed();
CompilationSubject.assertThat(compilation).hadErrorContaining("event >>doSomething()<< is never handled by a presenter or handler");
}
use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event in project mvp4g2 by mvp4g.
the class PresenterTest method testEventBusEventhandlerWithHanderlsAttributeAndEventHandlerAnnotation.
/**
* Check, that compilation works, if handler-attribute and EventHandler annotation is used for one event
*/
@Test
public void testEventBusEventhandlerWithHanderlsAttributeAndEventHandlerAnnotation() {
Compilation compilation = javac().withProcessors(new Mvp4g2Processor()).compile(new ArrayList<JavaFileObject>() {
{
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/eventhandlerWithHanderlsAttributeAndEventHandlerAnnotation/EventBusEventhandlerWithHanderlsAttributeAndEventHandlerAnnotation.java"));
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/eventhandlerWithHanderlsAttributeAndEventHandlerAnnotation/MockShellPresenter01.java"));
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/eventhandlerWithHanderlsAttributeAndEventHandlerAnnotation/IMockShellView01.java"));
add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/eventhandlerWithHanderlsAttributeAndEventHandlerAnnotation/MockShellView01.java"));
}
});
CompilationSubject.assertThat(compilation).succeeded();
JavaFileObjectSubject.assertThat(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/eventhandlerWithHanderlsAttributeAndEventHandlerAnnotation/EventBusEventhandlerWithHanderlsAttributeAndEventHandlerAnnotationImpl.java")).hasSourceEquivalentTo(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/eventhandlerWithHanderlsAttributeAndEventHandlerAnnotation/EventBusEventhandlerWithHanderlsAttributeAndEventHandlerAnnotationImpl.java"));
}
use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event in project mvp4g2 by mvp4g.
the class EventAnnotationScanner method scan.
public EventBusMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
EventAnnotationValidator eventAnnotationValidator = EventAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).eventBusMetaModel(this.eventBusMetaModel).eventBusTypeElement(eventBusTypeElement).build();
// validate event bus
eventAnnotationValidator.validate();
// handle events
for (Element element : roundEnvironment.getElementsAnnotatedWith(Event.class)) {
// do validation
EventAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).eventBusMetaModel(this.eventBusMetaModel).eventBusTypeElement(eventBusTypeElement).build().validate(element);
ExecutableElement executableElement = (ExecutableElement) element;
// restore meta data (if there is one)
// First we try to read an already created resource ...
EventMetaModel model = this.restore(element);
// get event annoation
Event eventAnnotation = element.getAnnotation(Event.class);
// scan for data
// internal event name
model.setEventInternalName(this.processorUtils.createInternalEventName(executableElement));
// event name
model.setEventName(executableElement.getSimpleName().toString());
// history name of hte event
model.setHistoryEventName(eventAnnotation.historyName());
// navigation event
model.setNavigationEvent(Boolean.toString(eventAnnotation.navigationEvent()));
// passive
model.setPassive(Boolean.toString(eventAnnotation.passive()));
// handlers
model.setHandlers(this.getElementsFromAnnotationAsList(executableElement, "handlers"));
// List of binding handlers (full class names as String)
model.setBindings(this.getElementsFromAnnotationAsList(executableElement, "bind"));
// List of activate handlers (full class names as String)
model.setActivateHandlers(this.getElementsFromAnnotationAsList(executableElement, "activate"));
// List of deactivate handlers (full class names as String)
model.setDeactivateHandlers(this.getElementsFromAnnotationAsList(executableElement, "deactivate"));
// history converter
model.setHistoryConverter(Objects.requireNonNull(this.getHistoryConverterTypeElement(eventAnnotation)).getQualifiedName().toString());
// start event?
model.setStartEvent(isNull(executableElement.getAnnotation(Start.class)) ? "false" : "true");
// initHistory event?
model.setInitHistory(isNull(executableElement.getAnnotation(InitHistory.class)) ? "false" : "true");
// start event?
model.setNotFoundHistory(isNull(executableElement.getAnnotation(NotFoundHistory.class)) ? "false" : "true");
// parameters
executableElement.getParameters().forEach(v -> model.addParameter(v.getSimpleName().toString(), v.asType().toString()));
// let's store the updated model
this.processorUtils.store(model, this.createRelativeFileName(element));
// store the event meta data model in the eventbus meta data model
this.eventBusMetaModel.add(model);
}
return this.eventBusMetaModel;
}
use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event in project mvp4g2 by mvp4g.
the class EventBusAnnotationScanner method scan.
public EventBusMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
// First we try to read an already created resource ...
EventBusMetaModel model = this.restore();
// Check if we have an element annotated with @Application
if (!roundEnvironment.getElementsAnnotatedWith(EventBus.class).isEmpty()) {
// create validator
EventBusAnnotationValidator eventBusValidaitor = EventBusAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).build();
// check, whether we have o do something ...
eventBusValidaitor.validate();
// should only be one, so we can search for the first! ...
Optional<? extends Element> optionalElement = this.roundEnvironment.getElementsAnnotatedWith(EventBus.class).stream().findFirst();
if (optionalElement.isPresent()) {
Element eventBusAnnotationElement = optionalElement.get();
eventBusValidaitor.validate(eventBusAnnotationElement);
EventBus eventBusAnnotation = eventBusAnnotationElement.getAnnotation(EventBus.class);
if (!isNull(eventBusAnnotation)) {
TypeElement shellTypeElement = this.getShellTypeElement(eventBusAnnotation);
model = new EventBusMetaModel(eventBusAnnotationElement.toString(), isNull(shellTypeElement) ? "" : shellTypeElement.toString());
// Debug-Annotation
model = DebugAnnotationScanner.builder().processingEnvironment(processingEnvironment).eventBusTypeElement((TypeElement) eventBusAnnotationElement).eventBusMetaModel(model).build().scan(roundEnvironment);
// Filters-Annotation
model = FiltersAnnotationScanner.builder().processingEnvironment(processingEnvironment).eventBusTypeElement((TypeElement) eventBusAnnotationElement).eventBusMetaModel(model).build().scan(roundEnvironment);
// handle Event-annotation
model = EventAnnotationScanner.builder().processingEnvironment(processingEnvironment).eventBusTypeElement((TypeElement) eventBusAnnotationElement).eventBusMetaModel(model).build().scan(roundEnvironment);
// let's store the updated model
this.processorUtils.store(model, this.createRelativeFileName());
}
}
} else {
// @Debug annotation with no @EventBus annotation ... bad idea!
Optional<? extends Element> optionalDebugElement = this.roundEnvironment.getElementsAnnotatedWith(Debug.class).stream().findFirst();
if (optionalDebugElement.isPresent()) {
EventBus eventBusAnnotation = optionalDebugElement.get().getAnnotation(EventBus.class);
if (eventBusAnnotation == null) {
throw new ProcessorException(((TypeElement) optionalDebugElement.get()).getQualifiedName().toString() + " -> @Debug can only be used with an interfaces annotated with @EventBus");
}
}
// @Filters annotation with no @EventBus annotation ... bad idea!
Optional<? extends Element> optionalFiltersElement = this.roundEnvironment.getElementsAnnotatedWith(Filters.class).stream().findFirst();
if (optionalFiltersElement.isPresent()) {
EventBus eventBusAnnotation = optionalFiltersElement.get().getAnnotation(EventBus.class);
if (eventBusAnnotation == null) {
throw new ProcessorException(((TypeElement) optionalFiltersElement.get()).getQualifiedName().toString() + " -> @Filters can only be used with an interfaces annotated with @EventBus");
}
}
}
return model;
}
use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event in project mvp4g2 by mvp4g.
the class EventAnnotationValidator method validate.
public void validate() throws ProcessorException {
// check if all historyNames are unique!
List<String> historyNames = new ArrayList<>();
for (Element element : this.roundEnvironment.getElementsAnnotatedWith(Event.class)) {
Event eventAnnotation = element.getAnnotation(Event.class);
// check, that the parent element extends IsEventBus
if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), element.getEnclosingElement().asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsEventBus.class.getCanonicalName()).asType())) {
throw new ProcessorException("Mvp4g2Processor: @Event can only be used inside a event bus! >>" + element.getEnclosingElement().toString() + "<< does no implement IsEventBus");
}
if (!Event.DEFAULT_HISTORY_NAME.equals(eventAnnotation.historyName())) {
if (historyNames.contains(eventAnnotation.historyName())) {
throw new ProcessorException("Mvp4g2Processor: EventElement: >>" + element.getSimpleName().toString() + "<< using a already used historyName -> >>" + eventAnnotation.historyName() + "<<");
} else {
historyNames.add(eventAnnotation.historyName());
}
}
ExecutableElement eventExecutableElement = (ExecutableElement) element;
if (!"void".equals(eventExecutableElement.getReturnType().toString())) {
throw new ProcessorException("Mvp4g2Processor: EventElement: >>" + element.getSimpleName().toString() + "<< must return 'void'");
}
}
// check, if there are more than one InitHistory-annotation!
if (this.roundEnvironment.getElementsAnnotatedWith(InitHistory.class).size() > 1) {
throw new ProcessorException("Mvp4g2Processor: @InitHistory can only be set a single time inside a event bus");
} else if (this.roundEnvironment.getElementsAnnotatedWith(InitHistory.class).size() == 1) {
for (Element element : this.roundEnvironment.getElementsAnnotatedWith(InitHistory.class)) {
ExecutableElement executableElement = (ExecutableElement) element;
if (((ExecutableElement) element).getParameters().size() > 0) {
throw new ProcessorException("Mvp4g2Processor: @InitHistory can only be used on a method with no arguments");
}
}
}
// check, if there are more than one NotFoundHistory-annotation!
if (this.roundEnvironment.getElementsAnnotatedWith(NotFoundHistory.class).size() > 1) {
throw new ProcessorException("Mvp4g2Processor: @NotFoundHistory can only be set a single time inside a event bus");
} else if (this.roundEnvironment.getElementsAnnotatedWith(NotFoundHistory.class).size() == 1) {
for (Element element : this.roundEnvironment.getElementsAnnotatedWith(NotFoundHistory.class)) {
ExecutableElement executableElement = (ExecutableElement) element;
if (((ExecutableElement) element).getParameters().size() > 0) {
throw new ProcessorException("Mvp4g2Processor: @NotFoundHistory can only be used on a method with no arguments");
}
}
}
}
Aggregations