Search in sources :

Example 1 with ServiceDefinition

use of com.palantir.conjure.java.undertow.processor.data.ServiceDefinition 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)

Example 2 with ServiceDefinition

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

the class ConjureUndertowEndpointsGenerator method generate.

public TypeSpec generate() {
    FieldSpec delegate = FieldSpec.builder(serviceDefinition.serviceInterface(), DELEGATE_NAME).addModifiers(Modifier.PRIVATE, Modifier.FINAL).build();
    ImmutableList<TypeSpec> endpoints = serviceDefinition.endpoints().stream().map(endpoint -> endpoint(serviceDefinition, endpoint)).collect(ImmutableList.toImmutableList());
    return TypeSpec.classBuilder(serviceDefinition.undertowService()).addAnnotation(AnnotationSpec.builder(ClassName.get(Generated.class)).addMember("value", "$S", getClass().getCanonicalName()).build()).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(ClassName.get(UndertowService.class)).addField(delegate).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(serviceDefinition.serviceInterface(), DELEGATE_NAME).addStatement("this.$N = $N", delegate, DELEGATE_NAME).build()).addMethod(MethodSpec.methodBuilder("of").addParameter(serviceDefinition.serviceInterface(), DELEGATE_NAME).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addStatement("return new $T($N)", serviceDefinition.undertowService(), DELEGATE_NAME).returns(UndertowService.class).build()).addMethod(MethodSpec.methodBuilder("endpoints").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(UndertowRuntime.class, serviceDefinition.conjureRuntimeArgName()).returns(ParameterizedTypeName.get(List.class, Endpoint.class)).addStatement("return $T.of($L)", ImmutableList.class, endpoints.stream().map(endpoint -> CodeBlock.of("new $N($N, $N)", endpoint.name, RUNTIME_NAME, DELEGATE_NAME)).collect(CodeBlock.joining(", "))).build()).addTypes(endpoints).build();
}
Also used : ReturnType(com.palantir.conjure.java.undertow.processor.data.ReturnType) Endpoint(com.palantir.conjure.java.undertow.lib.Endpoint) SafeLoggingAnnotation(com.palantir.conjure.java.undertow.processor.data.ParameterType.SafeLoggingAnnotation) Modifier(javax.lang.model.element.Modifier) ServiceDefinition(com.palantir.conjure.java.undertow.processor.data.ServiceDefinition) UndertowService(com.palantir.conjure.java.undertow.lib.UndertowService) FieldSpec(com.squareup.javapoet.FieldSpec) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) HttpServerExchange(io.undertow.server.HttpServerExchange) AuthHeader(com.palantir.tokens.auth.AuthHeader) ClassName(com.squareup.javapoet.ClassName) ReturnValueWriter(com.palantir.conjure.java.undertow.lib.ReturnValueWriter) CookieDeserializer(com.palantir.conjure.java.undertow.annotations.CookieDeserializer) PathMultiParamDeserializer(com.palantir.conjure.java.undertow.annotations.PathMultiParamDeserializer) BearerTokenCookieDeserializer(com.palantir.conjure.java.undertow.annotations.BearerTokenCookieDeserializer) RequestContext(com.palantir.conjure.java.undertow.lib.RequestContext) HttpString(io.undertow.util.HttpString) ArrayList(java.util.ArrayList) EndpointName(com.palantir.conjure.java.undertow.processor.data.EndpointName) EndpointDefinition(com.palantir.conjure.java.undertow.processor.data.EndpointDefinition) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Value(org.immutables.value.Value) Generated(javax.annotation.processing.Generated) TypeMarker(com.palantir.conjure.java.undertow.lib.TypeMarker) StatusCodes(io.undertow.util.StatusCodes) CodeBlock(com.squareup.javapoet.CodeBlock) Serializer(com.palantir.conjure.java.undertow.lib.Serializer) ArgumentDefinition(com.palantir.conjure.java.undertow.processor.data.ArgumentDefinition) UsesRequestContextVisitor(com.palantir.conjure.java.undertow.processor.data.ParameterTypeVisitors.UsesRequestContextVisitor) MethodSpec(com.squareup.javapoet.MethodSpec) PathParamDeserializer(com.palantir.conjure.java.undertow.annotations.PathParamDeserializer) Cases(com.palantir.conjure.java.undertow.processor.data.ParameterType.Cases) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec) HttpHandler(io.undertow.server.HttpHandler) List(java.util.List) HeaderParamDeserializer(com.palantir.conjure.java.undertow.annotations.HeaderParamDeserializer) UnsafeArg(com.palantir.logsafe.UnsafeArg) UndertowRuntime(com.palantir.conjure.java.undertow.lib.UndertowRuntime) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Deserializer(com.palantir.conjure.java.undertow.lib.Deserializer) QueryParamDeserializer(com.palantir.conjure.java.undertow.annotations.QueryParamDeserializer) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Methods(io.undertow.util.Methods) ParameterType(com.palantir.conjure.java.undertow.processor.data.ParameterType) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec) UndertowService(com.palantir.conjure.java.undertow.lib.UndertowService)

Aggregations

EndpointDefinition (com.palantir.conjure.java.undertow.processor.data.EndpointDefinition)2 ServiceDefinition (com.palantir.conjure.java.undertow.processor.data.ServiceDefinition)2 ClassName (com.squareup.javapoet.ClassName)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 Optional (java.util.Optional)2 MoreElements (com.google.auto.common.MoreElements)1 ImmutableList (com.google.common.collect.ImmutableList)1 BearerTokenCookieDeserializer (com.palantir.conjure.java.undertow.annotations.BearerTokenCookieDeserializer)1 CookieDeserializer (com.palantir.conjure.java.undertow.annotations.CookieDeserializer)1 HeaderParamDeserializer (com.palantir.conjure.java.undertow.annotations.HeaderParamDeserializer)1 PathMultiParamDeserializer (com.palantir.conjure.java.undertow.annotations.PathMultiParamDeserializer)1 PathParamDeserializer (com.palantir.conjure.java.undertow.annotations.PathParamDeserializer)1 QueryParamDeserializer (com.palantir.conjure.java.undertow.annotations.QueryParamDeserializer)1 Deserializer (com.palantir.conjure.java.undertow.lib.Deserializer)1 Endpoint (com.palantir.conjure.java.undertow.lib.Endpoint)1 RequestContext (com.palantir.conjure.java.undertow.lib.RequestContext)1 ReturnValueWriter (com.palantir.conjure.java.undertow.lib.ReturnValueWriter)1 Serializer (com.palantir.conjure.java.undertow.lib.Serializer)1 TypeMarker (com.palantir.conjure.java.undertow.lib.TypeMarker)1 UndertowRuntime (com.palantir.conjure.java.undertow.lib.UndertowRuntime)1