use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class Retrofit2ServiceGenerator method createCompatibilityBackfillMethod.
private MethodSpec createCompatibilityBackfillMethod(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper, Set<ArgumentName> encodedPathArgs, List<ArgumentDefinition> extraArgs) {
TypeName returnType = endpointDef.getReturns().map(returnTypeMapper::getClassName).orElse(ClassName.VOID);
// ensure the correct ordering of parameters by creating the complete sorted parameter list
List<ParameterSpec> sortedParams = createServiceMethodParameters(endpointDef, argumentTypeMapper, encodedPathArgs);
List<Optional<ArgumentDefinition>> sortedMaybeExtraArgs = sortedParams.stream().map(param -> extraArgs.stream().filter(arg -> arg.getArgName().get().equals(param.name)).findFirst()).collect(Collectors.toList());
// omit extraArgs from the back fill method signature
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(endpointDef.getEndpointName().get()).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addAnnotation(Deprecated.class).addParameters(IntStream.range(0, sortedParams.size()).filter(i -> !sortedMaybeExtraArgs.get(i).isPresent()).mapToObj(sortedParams::get).collect(Collectors.toList())).addAnnotations(ConjureAnnotations.getClientEndpointAnnotations(endpointDef));
endpointDef.getReturns().ifPresent(type -> methodBuilder.returns(ParameterizedTypeName.get(LISTENABLE_FUTURE_TYPE, returnType.box())));
// replace extraArgs with default values when invoking the complete method
StringBuilder sb = new StringBuilder(endpointDef.getReturns().isPresent() ? "return $N(" : "$N(");
List<Object> values = IntStream.range(0, sortedParams.size()).mapToObj(i -> {
Optional<ArgumentDefinition> maybeArgDef = sortedMaybeExtraArgs.get(i);
if (maybeArgDef.isPresent()) {
sb.append("$L, ");
return maybeArgDef.get().getType().accept(DefaultTypeValueVisitor.INSTANCE);
} else {
sb.append("$N, ");
return sortedParams.get(i);
}
}).collect(Collectors.toList());
// trim the end
sb.setLength(sb.length() - 2);
sb.append(")");
ImmutableList<Object> methodCallArgs = ImmutableList.builder().add(endpointDef.getEndpointName().get()).addAll(values).build();
methodBuilder.addStatement(sb.toString(), methodCallArgs.toArray(new Object[0]));
return methodBuilder.build();
}
use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class UndertowServiceGenerator method generate.
@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
Map<TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
ClassNameVisitor defaultVisitor = new DefaultClassNameVisitor(types.keySet(), options);
TypeMapper typeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, ClassName.get(InputStream.class)));
TypeMapper returnTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, ClassName.get(BinaryResponseBody.class)));
UndertowServiceInterfaceGenerator interfaceGenerator = new UndertowServiceInterfaceGenerator(options);
UndertowServiceHandlerGenerator handlerGenerator = new UndertowServiceHandlerGenerator(options);
return conjureDefinition.getServices().stream().flatMap(serviceDef -> Stream.of(interfaceGenerator.generateServiceInterface(serviceDef, typeMapper, returnTypeMapper), handlerGenerator.generateServiceHandler(serviceDef, types, typeMapper, returnTypeMapper)));
}
Aggregations