Search in sources :

Example 6 with TypeMapper

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();
}
Also used : Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) ClassName(com.squareup.javapoet.ClassName) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) MediaType(javax.ws.rs.core.MediaType) ClassNameVisitor(com.palantir.conjure.java.types.ClassNameVisitor) Matcher(java.util.regex.Matcher) Map(java.util.Map) ArgumentName(com.palantir.conjure.spec.ArgumentName) ParameterId(com.palantir.conjure.spec.ParameterId) ImmutableSet(com.google.common.collect.ImmutableSet) ParameterSpec(com.squareup.javapoet.ParameterSpec) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) AuthType(com.palantir.conjure.spec.AuthType) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) Collection(java.util.Collection) Set(java.util.Set) TypeMapper(com.palantir.conjure.java.types.TypeMapper) Collectors(java.util.stream.Collectors) ParameterType(com.palantir.conjure.spec.ParameterType) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) Stream(java.util.stream.Stream) Paths(com.palantir.util.syntacticpath.Paths) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) AuthTypeVisitor(com.palantir.conjure.visitor.AuthTypeVisitor) HttpPath(com.palantir.conjure.spec.HttpPath) SafeLogger(com.palantir.logsafe.logger.SafeLogger) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Generator(com.palantir.conjure.java.Generator) ParameterOrder(com.palantir.conjure.java.util.ParameterOrder) Path(com.palantir.util.syntacticpath.Path) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Optional(java.util.Optional) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 7 with TypeMapper

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)));
}
Also used : TypeMapper(com.palantir.conjure.java.types.TypeMapper) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) TypeName(com.palantir.conjure.spec.TypeName) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) ClassNameVisitor(com.palantir.conjure.java.types.ClassNameVisitor) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Aggregations

TypeMapper (com.palantir.conjure.java.types.TypeMapper)7 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)7 DefaultClassNameVisitor (com.palantir.conjure.java.types.DefaultClassNameVisitor)5 SpecializeBinaryClassNameVisitor (com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor)5 ClassName (com.squareup.javapoet.ClassName)5 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)5 TypeName (com.squareup.javapoet.TypeName)5 Options (com.palantir.conjure.java.Options)4 ClassNameVisitor (com.palantir.conjure.java.types.ClassNameVisitor)4 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)4 JavaFile (com.squareup.javapoet.JavaFile)4 Map (java.util.Map)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)3 Packages (com.palantir.conjure.java.util.Packages)3 ParameterOrder (com.palantir.conjure.java.util.ParameterOrder)3 ArgumentDefinition (com.palantir.conjure.spec.ArgumentDefinition)3 AuthType (com.palantir.conjure.spec.AuthType)3 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)3