use of io.quarkiverse.githubaction.deployment.DispatchingConfiguration.EventAnnotation in project quarkus-github-action by quarkiverse.
the class GitHubActionProcessor method generateActionMain.
/**
* This method generates the <code>@QuarkusMain</code> class.
* <p>
* It emits the GitHub events as CDI events that will then be caught by the multiplexers.
*/
private static void generateActionMain(ClassOutput beanClassOutput, CombinedIndexBuildItem combinedIndex, LaunchModeBuildItem launchMode, DispatchingConfiguration dispatchingConfiguration, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
String gitHubEventHandlerClassName = GitHubEventHandler.class.getName() + "Impl";
reflectiveClasses.produce(new ReflectiveClassBuildItem(true, true, gitHubEventHandlerClassName));
ClassCreator gitHubEventHandlerClassCreator = ClassCreator.builder().classOutput(beanClassOutput).className(gitHubEventHandlerClassName).interfaces(GitHubEventHandler.class).build();
gitHubEventHandlerClassCreator.addAnnotation(Singleton.class);
FieldCreator eventFieldCreator = gitHubEventHandlerClassCreator.getFieldCreator(EVENT_EMITTER_FIELD, Event.class);
eventFieldCreator.addAnnotation(Inject.class);
eventFieldCreator.setModifiers(Modifier.PROTECTED);
MethodCreator handleMethodCreator = gitHubEventHandlerClassCreator.getMethodCreator("handle", void.class, GitHubEvent.class);
handleMethodCreator.setModifiers(Modifier.PUBLIC);
ResultHandle gitHubEventRh = handleMethodCreator.getMethodParam(0);
ResultHandle dispatchedNameRh = handleMethodCreator.invokeVirtualMethod(GITHUB_EVENT_GET_NAME, gitHubEventRh);
ResultHandle dispatchedEventRh = handleMethodCreator.invokeVirtualMethod(GITHUB_EVENT_GET_EVENT, gitHubEventRh);
ResultHandle dispatchedActionRh = handleMethodCreator.invokeVirtualMethod(GITHUB_EVENT_GET_EVENT_ACTION, gitHubEventRh);
for (Entry<String, Map<String, ActionDispatchingConfiguration>> actionConfigurationEntry : dispatchingConfiguration.getActionConfigurations().entrySet()) {
String name = actionConfigurationEntry.getKey();
Map<String, ActionDispatchingConfiguration> actionConfiguration = actionConfigurationEntry.getValue();
BytecodeCreator nameMatchesCreator = handleMethodCreator.ifTrue(handleMethodCreator.invokeVirtualMethod(MethodDescriptors.OBJECT_EQUALS, handleMethodCreator.load(name), dispatchedNameRh)).trueBranch();
ResultHandle actionAnnotationLiteralRh = nameMatchesCreator.newInstance(MethodDescriptor.ofConstructor(ActionLiteral.class, String.class), new ResultHandle[] { nameMatchesCreator.load(name) });
for (Entry<String, ActionDispatchingConfiguration> eventConfigurationEntry : actionConfiguration.entrySet()) {
String event = eventConfigurationEntry.getKey();
ActionDispatchingConfiguration eventDispatchingConfiguration = eventConfigurationEntry.getValue();
if (EventDefinition.ALL.equals(event)) {
ResultHandle annotationLiteralArrayRh = nameMatchesCreator.newArray(Annotation.class, 1);
nameMatchesCreator.writeArrayValue(annotationLiteralArrayRh, 0, actionAnnotationLiteralRh);
fireEvent(nameMatchesCreator, gitHubEventHandlerClassCreator.getClassName(), gitHubEventRh, annotationLiteralArrayRh);
continue;
}
BytecodeCreator eventMatchesCreator = nameMatchesCreator.ifTrue(nameMatchesCreator.invokeVirtualMethod(MethodDescriptors.OBJECT_EQUALS, nameMatchesCreator.load(event), dispatchedEventRh)).trueBranch();
for (Entry<String, EventAnnotation> eventAnnotationEntry : eventDispatchingConfiguration.getEventAnnotations().entrySet()) {
String action = eventAnnotationEntry.getKey();
EventAnnotation eventAnnotation = eventAnnotationEntry.getValue();
Class<?>[] literalParameterTypes = new Class<?>[eventAnnotation.getValues().size()];
Arrays.fill(literalParameterTypes, String.class);
List<ResultHandle> literalParameters = new ArrayList<>();
ResultHandle eventAnnotationLiteralRh = eventMatchesCreator.newInstance(MethodDescriptor.ofConstructor(getLiteralClassName(eventAnnotation.getName()), (Object[]) literalParameterTypes), literalParameters.toArray(ResultHandle[]::new));
ResultHandle annotationLiteralArrayRh = eventMatchesCreator.newArray(Annotation.class, 2);
eventMatchesCreator.writeArrayValue(annotationLiteralArrayRh, 0, actionAnnotationLiteralRh);
eventMatchesCreator.writeArrayValue(annotationLiteralArrayRh, 1, eventAnnotationLiteralRh);
if (Actions.ALL.equals(action)) {
fireEvent(eventMatchesCreator, gitHubEventHandlerClassCreator.getClassName(), gitHubEventRh, annotationLiteralArrayRh);
} else {
BytecodeCreator actionMatchesCreator = eventMatchesCreator.ifTrue(eventMatchesCreator.invokeVirtualMethod(MethodDescriptors.OBJECT_EQUALS, eventMatchesCreator.load(action), dispatchedActionRh)).trueBranch();
fireEvent(actionMatchesCreator, gitHubEventHandlerClassCreator.getClassName(), gitHubEventRh, annotationLiteralArrayRh);
}
}
}
}
handleMethodCreator.returnValue(null);
gitHubEventHandlerClassCreator.close();
}
Aggregations