Search in sources :

Example 1 with ActionDispatchingConfiguration

use of io.quarkiverse.githubaction.deployment.DispatchingConfiguration.ActionDispatchingConfiguration 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)

Example 2 with ActionDispatchingConfiguration

use of io.quarkiverse.githubaction.deployment.DispatchingConfiguration.ActionDispatchingConfiguration in project quarkus-github-action by quarkiverse.

the class GitHubActionProcessor method generateAnnotationLiterals.

private static void generateAnnotationLiterals(ClassOutput classOutput, DispatchingConfiguration dispatchingConfiguration) {
    for (ActionDispatchingConfiguration eventDispatchingConfiguration : dispatchingConfiguration.getActionDispatchingConfigurations()) {
        for (EventAnnotationLiteral eventAnnotationLiteral : eventDispatchingConfiguration.getEventAnnotationLiterals()) {
            String literalClassName = getLiteralClassName(eventAnnotationLiteral.getName());
            String signature = String.format("Ljavax/enterprise/util/AnnotationLiteral<L%1$s;>;L%1$s;", eventAnnotationLiteral.getName().toString().replace('.', '/'));
            ClassCreator literalClassCreator = ClassCreator.builder().classOutput(classOutput).className(literalClassName).signature(signature).superClass(AnnotationLiteral.class).interfaces(eventAnnotationLiteral.getName().toString()).build();
            Class<?>[] parameterTypes = new Class<?>[eventAnnotationLiteral.getAttributes().size()];
            Arrays.fill(parameterTypes, String.class);
            MethodCreator constructorCreator = literalClassCreator.getMethodCreator("<init>", "V", (Object[]) parameterTypes);
            constructorCreator.invokeSpecialMethod(MethodDescriptor.ofConstructor(AnnotationLiteral.class), constructorCreator.getThis());
            for (int i = 0; i < eventAnnotationLiteral.getAttributes().size(); i++) {
                constructorCreator.writeInstanceField(FieldDescriptor.of(literalClassName, eventAnnotationLiteral.getAttributes().get(i), String.class), constructorCreator.getThis(), constructorCreator.getMethodParam(i));
                constructorCreator.setModifiers(Modifier.PUBLIC);
            }
            constructorCreator.returnValue(null);
            for (String attribute : eventAnnotationLiteral.getAttributes()) {
                // we only support String for now
                literalClassCreator.getFieldCreator(attribute, String.class).setModifiers(Modifier.PRIVATE);
                MethodCreator getterCreator = literalClassCreator.getMethodCreator(attribute, String.class);
                getterCreator.setModifiers(Modifier.PUBLIC);
                getterCreator.returnValue(getterCreator.readInstanceField(FieldDescriptor.of(literalClassName, attribute, String.class), getterCreator.getThis()));
            }
            literalClassCreator.close();
        }
    }
}
Also used : MethodCreator(io.quarkus.gizmo.MethodCreator) AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) EventAnnotationLiteral(io.quarkiverse.githubaction.deployment.DispatchingConfiguration.EventAnnotationLiteral) ActionDispatchingConfiguration(io.quarkiverse.githubaction.deployment.DispatchingConfiguration.ActionDispatchingConfiguration) EventAnnotationLiteral(io.quarkiverse.githubaction.deployment.DispatchingConfiguration.EventAnnotationLiteral) ClassCreator(io.quarkus.gizmo.ClassCreator)

Aggregations

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