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();
}
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();
}
Aggregations