Search in sources :

Example 1 with EndpointDefinitions

use of com.palantir.conjure.java.undertow.processor.data.EndpointDefinitions in project conjure-java by palantir.

the class ConjureUndertowAnnotationProcessor method generateUndertowServiceEndpoints.

private JavaFile generateUndertowServiceEndpoints(Element annotatedInterface, Collection<? extends Element> annotatedMethods) {
    validationStep(ctx -> {
        for (Element element : annotatedMethods) {
            if (!element.getKind().equals(ElementKind.METHOD)) {
                ctx.reportError("Only methods can be annotated with @Handle", element);
            } else if (element.getModifiers().contains(Modifier.PRIVATE) || element.getModifiers().contains(Modifier.PROTECTED)) {
                ctx.reportError("Methods annotated with '@Handle' must be accessible to classes in the same " + "package, neither 'private' nor 'protected' modifiers are allowed.", element);
            } else if (element.getModifiers().contains(Modifier.STATIC)) {
                ctx.reportError("The '@Handle' annotation does not support static methods.", element);
            }
        }
    });
    List<EndpointDefinition> endpoints = processingStep(ctx -> {
        EndpointDefinitions endpointDefinitions = new EndpointDefinitions(ctx, elements, types);
        List<Optional<EndpointDefinition>> maybeEndpoints = annotatedMethods.stream().map(MoreElements::asExecutable).map(endpointDefinitions::tryParseEndpointDefinition).collect(Collectors.toList());
        Preconditions.checkArgument(maybeEndpoints.stream().filter(Predicates.not(Optional::isPresent)).collect(Collectors.toList()).isEmpty(), "Failed validation");
        return maybeEndpoints.stream().map(Optional::get).collect(Collectors.toList());
    });
    ClassName serviceInterface = ClassName.get(MoreElements.getPackage(annotatedInterface).getQualifiedName().toString(), annotatedInterface.getSimpleName().toString());
    ServiceDefinition serviceDefinition = ImmutableServiceDefinition.builder().serviceInterface(serviceInterface).addAllEndpoints(endpoints).build();
    TypeSpec generatedClass = new ConjureUndertowEndpointsGenerator(serviceDefinition).generate();
    TypeSpec withOriginatingElement = generatedClass.toBuilder().addOriginatingElement(annotatedInterface).build();
    return JavaFile.builder(serviceInterface.packageName(), withOriginatingElement).build();
}
Also used : Optional(java.util.Optional) ConjureUndertowEndpointsGenerator(com.palantir.conjure.java.undertow.processor.generate.ConjureUndertowEndpointsGenerator) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ClassName(com.squareup.javapoet.ClassName) EndpointDefinition(com.palantir.conjure.java.undertow.processor.data.EndpointDefinition) MoreElements(com.google.auto.common.MoreElements) ServiceDefinition(com.palantir.conjure.java.undertow.processor.data.ServiceDefinition) ImmutableServiceDefinition(com.palantir.conjure.java.undertow.processor.data.ImmutableServiceDefinition) EndpointDefinitions(com.palantir.conjure.java.undertow.processor.data.EndpointDefinitions) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

MoreElements (com.google.auto.common.MoreElements)1 EndpointDefinition (com.palantir.conjure.java.undertow.processor.data.EndpointDefinition)1 EndpointDefinitions (com.palantir.conjure.java.undertow.processor.data.EndpointDefinitions)1 ImmutableServiceDefinition (com.palantir.conjure.java.undertow.processor.data.ImmutableServiceDefinition)1 ServiceDefinition (com.palantir.conjure.java.undertow.processor.data.ServiceDefinition)1 ConjureUndertowEndpointsGenerator (com.palantir.conjure.java.undertow.processor.generate.ConjureUndertowEndpointsGenerator)1 ClassName (com.squareup.javapoet.ClassName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Optional (java.util.Optional)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1