use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.
the class EventMetaDataGenerator method generate.
public void generate() throws ProcessorException {
for (EventMetaModel eventMetaModel : this.eventBusMetaModel.getEventMetaModels()) {
TypeSpec.Builder typeSpec = TypeSpec.classBuilder(this.createEventMetaDataClassName(this.eventBusMetaModel.getEventBus().getClassName(), eventMetaModel.getEventName())).superclass(ParameterizedTypeName.get(ClassName.get(EventMetaData.class), this.eventBusMetaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
// constructor ...
MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode("super($S, $S, ", eventMetaModel.getEventInternalName(), eventMetaModel.getEventName());
if (Event.DEFAULT_HISTORY_NAME.equals(eventMetaModel.getHistoryEventName())) {
constructor.addCode("null, ");
} else {
constructor.addCode("$S, ", eventMetaModel.getHistoryEventName());
}
if (Event.NoHistoryConverter.class.getCanonicalName().equals(eventMetaModel.getHistoryConverter().getClassName())) {
constructor.addCode("null, null, ");
} else {
constructor.addCode("new $L(), new $T(), ", eventMetaModel.getHistoryConverter().getPackage() + "." + this.processorUtils.createHistoryMetaDataClassName(eventMetaModel.getHistoryConverter().getClassName()), eventMetaModel.getHistoryConverter().getTypeName());
}
constructor.addCode("$L, $L);\n", eventMetaModel.getPassive(), eventMetaModel.getNavigationEvent());
eventMetaModel.getBindings().forEach(s -> constructor.addStatement("super.addBindHandler($S)", s.getClassName()));
eventMetaModel.getHandlers().forEach(s -> constructor.addStatement("super.addHandler($S)", s.getClassName()));
eventMetaModel.getActivateHandlers().forEach(s -> constructor.addStatement("super.addActivateHandler($S)", s.getClassName()));
eventMetaModel.getDeactivateHandlers().forEach(s -> constructor.addStatement("super.addDeactivateHandler($S)", s.getClassName()));
typeSpec.addMethod(constructor.build());
JavaFile javaFile = JavaFile.builder(eventBusMetaModel.getEventBus().getPackage(), typeSpec.build()).build();
try {
javaFile.writeTo(this.processingEnvironment.getFiler());
} catch (IOException e) {
throw new ProcessorException("Unable to write generated file: >>" + eventBusMetaModel.getEventBus().getPackage() + "." + this.createEventMetaDataClassName(this.eventBusMetaModel.getEventBus().getClassName(), eventMetaModel.getEventName()) + "<< -> exception: " + e.getMessage());
}
}
}
use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.
the class ModelValidator method hasEventHandlingMethodImplemented.
private void hasEventHandlingMethodImplemented(EventMetaModel eventMetaModel, String eventInternalName, ClassNameModel classNameModel) throws ProcessorException {
TypeElement typeElement = this.processorUtils.getElements().getTypeElement(classNameModel.getClassName());
if (typeElement != null) {
// improvement: get all ExecutabelElement of type,
// convert to String and add to list and use this list
// for the compare!
Map<String, ExecutableElement> nameOfExecutableElements = new HashMap<>();
this.processorUtils.getElements().getAllMembers(typeElement).stream().filter(element -> element instanceof ExecutableElement).map(element -> (ExecutableElement) element).forEach(executableElement -> nameOfExecutableElements.put(executableElement.toString(), executableElement));
// method to look for
String methodNameToLookFor = this.createEventHandlungMethodName(eventInternalName);
// try to find in Map
ExecutableElement handlingElement = nameOfExecutableElements.get(methodNameToLookFor);
if (handlingElement != null) {
if (!"void".equals(handlingElement.getReturnType().toString())) {
throw new ProcessorException("Mvp4g2Processor: EventElement: >>" + eventInternalName.split(",")[0] + "<< must return 'void'");
}
return;
}
// otherwiese we throw an excpetion!
if (eventMetaModel.getBindings().size() > 0) {
this.processorUtils.createNoteMessage("Mvp4g2Processor: event >>" + eventInternalName.split(",")[0] + "<< is only used for binding");
} else {
this.processorUtils.createErrorMessage("Mvp4g2Processor: presenter >>" + classNameModel.getClassName() + "<< -> event >>" + createEventHandlungMethodName(eventInternalName) + "<< is not handled by presenter/handler and has no bindings");
}
}
}
use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.
the class PropertiesTest method testEventMetaModel.
@Test
public void testEventMetaModel() {
Properties properties = new Properties();
try {
properties.load(ClassLoader.getSystemResourceAsStream("META-INF" + File.separator + "mvp4g2" + File.separator + "gotoList_pPp_java_lang_String_pPp_java_lang_String.properties"));
} catch (IOException e) {
assert false : "IOException reading gotoList_pPp_java_lang_String_pPp_java_lang_String.properties";
}
EventMetaModel metaModel = new EventMetaModel(properties);
Properties newProperties = metaModel.createPropertes();
assertEquals(properties, newProperties);
}
use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.
the class PropertiesTest method testEventBusMetaModel.
@Test
public void testEventBusMetaModel() {
Properties properties = new Properties();
try {
properties.load(ClassLoader.getSystemResourceAsStream("META-INF" + File.separator + "mvp4g2" + File.separator + "eventBus.properties"));
} catch (IOException e) {
assert false : "IOException reading eventBus.properties";
}
EventBusMetaModel metaModel = new EventBusMetaModel(properties);
for (String eventInternalName : properties.getProperty("events").split(",")) {
try {
Properties eventProperties = new Properties();
eventProperties.load(ClassLoader.getSystemResourceAsStream("META-INF" + File.separator + "mvp4g2" + File.separator + eventInternalName + ".properties"));
metaModel.add(new EventMetaModel(eventProperties));
} catch (IOException e) {
assert false : "IOException reading " + eventInternalName + ".properties";
}
}
Properties newProperties = metaModel.createPropertes();
assertEquals(properties, newProperties);
}
use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.
the class EventHandlingMethodGenerator method generateEventHandlingMethod.
private void generateEventHandlingMethod(EventMetaModel eventMetaModel) {
MethodSpec.Builder eventHandlingMethod = MethodSpec.methodBuilder(eventMetaModel.getEventName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
eventHandlingMethod.addStatement("int startLogDepth = $T.logDepth", ClassName.get(AbstractEventBus.class));
eventHandlingMethod.beginControlFlow("try");
// add parametners to method signature ...
eventMetaModel.getParameterMetaDataList().stream().map(parameterMetaData -> ParameterSpec.builder(parameterMetaData.getType().getTypeName(), parameterMetaData.getName()).addModifiers(Modifier.FINAL).build()).forEach(eventHandlingMethod::addParameter);
if (eventMetaModel.isNavigationEvent()) {
eventHandlingMethod.addCode("super.logAskingForConfirmation(++$T.logDepth, $S", ClassName.get(AbstractEventBus.class), eventMetaModel.getEventName());
eventMetaModel.getParameterMetaDataList().forEach(parameterMetaData -> eventHandlingMethod.addCode(", $N", parameterMetaData.getName()));
eventHandlingMethod.addCode(");\n");
MethodSpec.Builder executeMethod = MethodSpec.methodBuilder("execute").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
this.callEventExecMethod(executeMethod, eventMetaModel);
TypeSpec confirmCommand = TypeSpec.anonymousClassBuilder(CodeBlock.builder().add("this").build()).addSuperinterface(NavigationEventCommand.class).addMethod(executeMethod.build()).build();
eventHandlingMethod.addStatement("super.placeService.confirmEvent($L)", confirmCommand);
} else {
this.callEventExecMethod(eventHandlingMethod, eventMetaModel);
}
eventHandlingMethod.nextControlFlow("finally");
eventHandlingMethod.addStatement("$T.logDepth = startLogDepth", ClassName.get(AbstractEventBus.class));
eventHandlingMethod.endControlFlow();
typeSpec.addMethod(eventHandlingMethod.build());
}
Aggregations