Search in sources :

Example 1 with EventAnnotation

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();
}
Also used : FieldCreator(io.quarkus.gizmo.FieldCreator) ActionLiteral(io.quarkiverse.githubaction.Action.ActionLiteral) ArrayList(java.util.ArrayList) ActionDispatchingConfiguration(io.quarkiverse.githubaction.deployment.DispatchingConfiguration.ActionDispatchingConfiguration) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) ClassCreator(io.quarkus.gizmo.ClassCreator) EventAnnotation(io.quarkiverse.githubaction.deployment.DispatchingConfiguration.EventAnnotation) MethodCreator(io.quarkus.gizmo.MethodCreator) ResultHandle(io.quarkus.gizmo.ResultHandle) GitHubEventHandler(io.quarkiverse.githubaction.runtime.GitHubEventHandler) Map(java.util.Map) HashMap(java.util.HashMap) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem)

Aggregations

ActionLiteral (io.quarkiverse.githubaction.Action.ActionLiteral)1 ActionDispatchingConfiguration (io.quarkiverse.githubaction.deployment.DispatchingConfiguration.ActionDispatchingConfiguration)1 EventAnnotation (io.quarkiverse.githubaction.deployment.DispatchingConfiguration.EventAnnotation)1 GitHubEventHandler (io.quarkiverse.githubaction.runtime.GitHubEventHandler)1 ReflectiveClassBuildItem (io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem)1 BytecodeCreator (io.quarkus.gizmo.BytecodeCreator)1 ClassCreator (io.quarkus.gizmo.ClassCreator)1 FieldCreator (io.quarkus.gizmo.FieldCreator)1 MethodCreator (io.quarkus.gizmo.MethodCreator)1 ResultHandle (io.quarkus.gizmo.ResultHandle)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1