Search in sources :

Example 1 with Options

use of com.palantir.conjure.java.Options in project conjure-java by palantir.

the class BeanGenerator method generateBeanType.

@SuppressWarnings("CyclomaticComplexity")
public static JavaFile generateBeanType(TypeMapper typeMapper, ObjectDefinition typeDef, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, Options options) {
    ImmutableList<EnrichedField> fields = createFields(typeMapper, typeDef.getFields());
    ImmutableList<FieldSpec> poetFields = EnrichedField.toPoetSpecs(fields);
    ImmutableList<EnrichedField> nonPrimitiveEnrichedFields = fields.stream().filter(field -> !field.isPrimitive()).collect(ImmutableList.toImmutableList());
    com.palantir.conjure.spec.TypeName prefixedName = Packages.getPrefixedName(typeDef.getTypeName(), options.packagePrefix());
    ClassName objectClass = ClassName.get(prefixedName.getPackage(), prefixedName.getName());
    ClassName builderClass = ClassName.get(objectClass.packageName(), objectClass.simpleName(), "Builder");
    ClassName builderImplementation = options.useStagedBuilders() && fields.stream().anyMatch(field -> !fieldShouldBeInFinalStage(field)) ? ClassName.get(objectClass.packageName(), objectClass.simpleName(), "DefaultBuilder") : builderClass;
    TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(prefixedName.getName()).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(poetFields).addMethod(createConstructor(fields, poetFields)).addMethods(createGetters(fields, typesMap, options));
    if (!poetFields.isEmpty()) {
        typeBuilder.addMethod(MethodSpecs.createEquals(objectClass)).addMethod(MethodSpecs.createEqualTo(objectClass, poetFields));
        if (useCachedHashCode(fields)) {
            MethodSpecs.addCachedHashCode(typeBuilder, poetFields);
        } else {
            typeBuilder.addMethod(MethodSpecs.createHashCode(poetFields));
        }
    }
    typeBuilder.addMethod(MethodSpecs.createToString(prefixedName.getName(), fields.stream().map(EnrichedField::fieldName).collect(Collectors.toList())));
    if (poetFields.size() <= MAX_NUM_PARAMS_FOR_FACTORY) {
        typeBuilder.addMethod(createStaticFactoryMethod(fields, objectClass));
    }
    if (!nonPrimitiveEnrichedFields.isEmpty()) {
        typeBuilder.addMethod(createValidateFields(nonPrimitiveEnrichedFields)).addMethod(createAddFieldIfMissing(nonPrimitiveEnrichedFields.size()));
    }
    if (poetFields.isEmpty()) {
        // Need to add JsonSerialize annotation which indicates that the empty bean serializer should be used to
        // serialize this class. Without this annotation no serializer will be set for this class, thus preventing
        // serialization.
        typeBuilder.addAnnotation(JsonSerialize.class).addField(createSingletonField(objectClass));
        if (!options.strictObjects()) {
            typeBuilder.addAnnotation(AnnotationSpec.builder(JsonIgnoreProperties.class).addMember("ignoreUnknown", "$L", true).build());
        }
    } else {
        ImmutableList<EnrichedField> fieldsNeedingBuilderStage = fields.stream().filter(field -> !fieldShouldBeInFinalStage(field)).collect(ImmutableList.toImmutableList());
        typeBuilder.addAnnotation(AnnotationSpec.builder(JsonDeserialize.class).addMember("builder", "$T.class", builderImplementation).build());
        if (!options.useStagedBuilders() || fieldsNeedingBuilderStage.isEmpty()) {
            typeBuilder.addMethod(createBuilder(builderClass)).addType(BeanBuilderGenerator.generate(typeMapper, objectClass, builderClass, typeDef, typesMap, options, Optional.empty()));
        } else {
            List<TypeSpec> interfaces = generateStageInterfaces(objectClass, builderClass, typeMapper, fieldsNeedingBuilderStage, fields.stream().filter(BeanGenerator::fieldShouldBeInFinalStage).collect(ImmutableList.toImmutableList()));
            List<ClassName> interfacesAsClasses = interfaces.stream().map(stageInterface -> ClassName.get(objectClass.packageName(), objectClass.simpleName(), stageInterface.name)).collect(Collectors.toList());
            TypeSpec builderInterface = TypeSpec.interfaceBuilder("Builder").addModifiers(Modifier.PUBLIC).addMethods(interfaces.stream().map(stageInterface -> stageInterface.methodSpecs).flatMap(List::stream).map(method -> method.toBuilder().addAnnotation(Override.class).returns(method.name.equals("build") ? method.returnType : ClassName.get(objectClass.packageName(), objectClass.simpleName(), "Builder")).build()).collect(Collectors.toSet())).addSuperinterfaces(interfacesAsClasses.stream().map(ClassName::box).collect(Collectors.toList())).build();
            typeBuilder.addTypes(interfaces).addType(builderInterface).addMethod(MethodSpec.methodBuilder("builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(interfacesAsClasses.get(0)).addStatement("return new DefaultBuilder()").build()).addType(BeanBuilderGenerator.generate(typeMapper, objectClass, builderClass, typeDef, typesMap, options, Optional.of(ClassName.get(objectClass.packageName(), objectClass.simpleName(), builderInterface.name))));
        }
    }
    typeBuilder.addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(BeanGenerator.class));
    typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs)));
    return JavaFile.builder(prefixedName.getPackage(), typeBuilder.build()).skipJavaLangImports(true).indent("    ").build();
}
Also used : DefaultTypeVisitor(com.palantir.conjure.java.visitor.DefaultTypeVisitor) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) ClassName(com.squareup.javapoet.ClassName) Collections2(com.google.common.collect.Collections2) PeekingIterator(com.google.common.collect.PeekingIterator) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) MapType(com.palantir.conjure.spec.MapType) Map(java.util.Map) OptionalType(com.palantir.conjure.spec.OptionalType) MoreVisitors(com.palantir.conjure.java.visitor.MoreVisitors) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) ParameterSpec(com.squareup.javapoet.ParameterSpec) Collection(java.util.Collection) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) Stream(java.util.stream.Stream) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) CaseConverter(com.palantir.conjure.CaseConverter) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Iterables(com.google.common.collect.Iterables) ListType(com.palantir.conjure.spec.ListType) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Value(org.immutables.value.Value) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) SetType(com.palantir.conjure.spec.SetType) Nonnull(javax.annotation.Nonnull) CodeBlock(com.squareup.javapoet.CodeBlock) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) FieldName(com.palantir.conjure.spec.FieldName) Comparator(java.util.Comparator) Collections(java.util.Collections) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) FieldSpec(com.squareup.javapoet.FieldSpec) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with Options

use of com.palantir.conjure.java.Options in project conjure-java by palantir.

the class DialogueServiceGenerator method generate.

@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
    Map<TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
    DialogueEndpointsGenerator endpoints = new DialogueEndpointsGenerator(options);
    TypeMapper parameterTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(BinaryRequestBody.class)));
    TypeMapper returnTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(InputStream.class)));
    Map<TypeName, TypeDefinition> typeDefinitionsByName = conjureDefinition.getTypes().stream().collect(Collectors.toMap(type -> type.accept(TypeDefinitionVisitor.TYPE_NAME), Function.identity()));
    DialogueInterfaceGenerator interfaceGenerator = new DialogueInterfaceGenerator(options, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes));
    TypeNameResolver typeNameResolver = typeName -> Preconditions.checkNotNull(typeDefinitionsByName.get(typeName), "Referenced unknown TypeName", SafeArg.of("typeName", typeName));
    StaticFactoryMethodGenerator asyncGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.ASYNC);
    StaticFactoryMethodGenerator blockingGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.BLOCKING);
    return conjureDefinition.getServices().stream().flatMap(serviceDef -> Stream.of(endpoints.endpointsClass(serviceDef), interfaceGenerator.generateBlocking(serviceDef, blockingGenerator), interfaceGenerator.generateAsync(serviceDef, asyncGenerator)));
}
Also used : TypeMapper(com.palantir.conjure.java.types.TypeMapper) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) ClassName(com.squareup.javapoet.ClassName) TypeMapper(com.palantir.conjure.java.types.TypeMapper) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) BinaryRequestBody(com.palantir.dialogue.BinaryRequestBody) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Options(com.palantir.conjure.java.Options) JavaFile(com.squareup.javapoet.JavaFile) SafeArg(com.palantir.logsafe.SafeArg) Stream(java.util.stream.Stream) Generator(com.palantir.conjure.java.Generator) TypeName(com.palantir.conjure.spec.TypeName) Map(java.util.Map) Preconditions(com.palantir.logsafe.Preconditions) InputStream(java.io.InputStream) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) TypeName(com.palantir.conjure.spec.TypeName) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Example 3 with Options

use of com.palantir.conjure.java.Options in project conjure-java by palantir.

the class UnionGenerator method generateUnionType.

public static JavaFile generateUnionType(TypeMapper typeMapper, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, UnionDefinition typeDef, Options options) {
    com.palantir.conjure.spec.TypeName prefixedTypeName = Packages.getPrefixedName(typeDef.getTypeName(), options.packagePrefix());
    ClassName unionClass = ClassName.get(prefixedTypeName.getPackage(), prefixedTypeName.getName());
    ClassName baseClass = unionClass.nestedClass("Base");
    ClassName visitorClass = unionClass.nestedClass("Visitor");
    ClassName visitorBuilderClass = unionClass.nestedClass("VisitorBuilder");
    Map<FieldDefinition, TypeName> memberTypes = typeDef.getUnion().stream().collect(StableCollectors.toLinkedMap(Function.identity(), entry -> typeMapper.getClassName(entry.getType())));
    List<FieldSpec> fields = ImmutableList.of(FieldSpec.builder(baseClass, VALUE_FIELD_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
    TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(typeDef.getTypeName().getName()).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(UnionGenerator.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(fields).addMethod(generateConstructor(baseClass)).addMethod(generateGetValue(baseClass)).addMethods(generateStaticFactories(typeMapper, unionClass, typeDef.getUnion())).addMethod(generateAcceptVisitMethod(visitorClass)).addType(generateVisitor(unionClass, visitorClass, memberTypes, visitorBuilderClass, options)).addType(generateVisitorBuilder(unionClass, visitorClass, visitorBuilderClass, memberTypes, options)).addTypes(generateVisitorBuilderStageInterfaces(unionClass, visitorClass, memberTypes, options)).addType(generateBase(baseClass, visitorClass, memberTypes)).addTypes(generateWrapperClasses(typeMapper, typesMap, baseClass, visitorClass, typeDef.getUnion(), options)).addType(generateUnknownWrapper(baseClass, visitorClass, options)).addMethod(generateEquals(unionClass)).addMethod(MethodSpecs.createEqualTo(unionClass, fields)).addMethod(MethodSpecs.createHashCode(fields)).addMethod(MethodSpecs.createToString(unionClass.simpleName(), fields.stream().map(fieldSpec -> FieldName.of(fieldSpec.name)).collect(Collectors.toList())));
    typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs)));
    return JavaFile.builder(prefixedTypeName.getPackage(), typeBuilder.build()).skipJavaLangImports(true).indent("    ").build();
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) BiFunction(java.util.function.BiFunction) ClassName(com.squareup.javapoet.ClassName) PeekingIterator(com.google.common.collect.PeekingIterator) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) JsonValue(com.fasterxml.jackson.annotation.JsonValue) Map(java.util.Map) JsonAnyGetter(com.fasterxml.jackson.annotation.JsonAnyGetter) StableCollectors(com.palantir.conjure.java.util.StableCollectors) ParameterSpec(com.squareup.javapoet.ParameterSpec) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) TypeName(com.squareup.javapoet.TypeName) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Nulls(com.fasterxml.jackson.annotation.Nulls) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) DoubleFunction(java.util.function.DoubleFunction) HashMap(java.util.HashMap) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Nonnull(javax.annotation.Nonnull) CodeBlock(com.squareup.javapoet.CodeBlock) IntFunction(java.util.function.IntFunction) TypeVariableName(com.squareup.javapoet.TypeVariableName) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) FieldName(com.palantir.conjure.spec.FieldName) Comparator(java.util.Comparator) Collections(java.util.Collections) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) TypeName(com.squareup.javapoet.TypeName) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 4 with Options

use of com.palantir.conjure.java.Options in project conjure-java by palantir.

the class UndertowServiceHandlerGenerator method endpointInvocation.

private CodeBlock endpointInvocation(EndpointDefinition endpointDefinition, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper, TypeMapper returnTypeMapper) {
    CodeBlock.Builder code = CodeBlock.builder();
    // auth code
    Optional<String> authVarName = addAuthCode(code, endpointDefinition);
    // body parameter
    getBodyParamTypeArgument(endpointDefinition.getArgs()).ifPresent(bodyParam -> {
        String paramName = sanitizeVarName(bodyParam.getArgName().get(), endpointDefinition);
        Type dealiased = TypeFunctions.toConjureTypeWithoutAliases(bodyParam.getType(), typeDefinitions);
        if (TypeFunctions.isBinaryOrOptionalBinary(dealiased)) {
            code.addStatement("$1T $2N = $3N.bodySerDe().deserializeInputStream($4N)", InputStream.class, paramName, RUNTIME_VAR_NAME, EXCHANGE_VAR_NAME);
        } else {
            code.addStatement("$1T $2N = $3N.deserialize($4N)", typeMapper.getClassName(bodyParam.getType()).box(), paramName, DESERIALIZER_VAR_NAME, EXCHANGE_VAR_NAME);
        }
        code.add(generateParamMetadata(bodyParam, bodyParam.getArgName().get(), paramName, typeMapper));
    });
    // path parameters
    addPathParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
    // header parameters
    addHeaderParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
    // query parameters
    addQueryParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
    List<CodeBlock> methodArgs = new ArrayList<>();
    authVarName.ifPresent(name -> methodArgs.add(CodeBlock.of("$N", name)));
    ParameterOrder.sorted(endpointDefinition.getArgs()).stream().map(arg -> arg.getArgName().get()).map(arg -> sanitizeVarName(arg, endpointDefinition)).map(arg -> CodeBlock.of("$N", arg)).forEach(methodArgs::add);
    if (Tags.hasServerRequestContext(endpointDefinition)) {
        methodArgs.add(CodeBlock.of("$N.contexts().createContext($N, this)", RUNTIME_VAR_NAME, EXCHANGE_VAR_NAME));
    }
    Optional<AsyncRequestProcessingMetadata> async = UndertowTypeFunctions.async(endpointDefinition, options);
    if (async.isPresent() || endpointDefinition.getReturns().isPresent()) {
        code.addStatement("$1T $2N = $3N.$4L($5L)", async.isPresent() ? UndertowTypeFunctions.getAsyncReturnType(endpointDefinition, returnTypeMapper, options) : returnTypeMapper.getClassName(endpointDefinition.getReturns().get()), RESULT_VAR_NAME, DELEGATE_VAR_NAME, JavaNameSanitizer.sanitize(endpointDefinition.getEndpointName().get()), methodArgs.stream().collect(CodeBlock.joining(",")));
    } else {
        code.addStatement("$1N.$2L($3L)", DELEGATE_VAR_NAME, endpointDefinition.getEndpointName(), methodArgs.stream().collect(CodeBlock.joining(",")));
    }
    if (async.isPresent()) {
        AsyncRequestProcessingMetadata metadata = async.get();
        if (metadata.timeout().isPresent()) {
            HumanReadableDuration timeout = metadata.timeout().get();
            code.add(CodeBlocks.statement("$N.async().register($N, this, $T.ofMillis(/* $L */ $L), $N)", RUNTIME_VAR_NAME, RESULT_VAR_NAME, Duration.class, timeout.toString(), timeout.toMilliseconds(), EXCHANGE_VAR_NAME));
        } else {
            code.add(CodeBlocks.statement("$1N.async().register($2N, this, $3N)", RUNTIME_VAR_NAME, RESULT_VAR_NAME, EXCHANGE_VAR_NAME));
        }
    } else {
        code.add(generateReturnValueCodeBlock(endpointDefinition, typeDefinitions));
    }
    return code.build();
}
Also used : DefaultTypeVisitor(com.palantir.conjure.java.visitor.DefaultTypeVisitor) Endpoint(com.palantir.conjure.java.undertow.lib.Endpoint) HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) UndertowService(com.palantir.conjure.java.undertow.lib.UndertowService) HttpServerExchange(io.undertow.server.HttpServerExchange) AuthHeader(com.palantir.tokens.auth.AuthHeader) ClassName(com.squareup.javapoet.ClassName) ReturnValueWriter(com.palantir.conjure.java.undertow.lib.ReturnValueWriter) Collections2(com.google.common.collect.Collections2) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) HttpString(io.undertow.util.HttpString) TypeMarker(com.palantir.conjure.java.undertow.lib.TypeMarker) Duration(java.time.Duration) Map(java.util.Map) CodeBlocks(com.palantir.conjure.java.types.CodeBlocks) OptionalType(com.palantir.conjure.spec.OptionalType) MoreVisitors(com.palantir.conjure.java.visitor.MoreVisitors) HumanReadableDuration(com.palantir.humanreadabletypes.HumanReadableDuration) ImmutableSet(com.google.common.collect.ImmutableSet) AuthType(com.palantir.conjure.spec.AuthType) MoreCollectors(com.google.common.collect.MoreCollectors) Collection(java.util.Collection) Set(java.util.Set) TypeMapper(com.palantir.conjure.java.types.TypeMapper) Streams(com.google.common.collect.Streams) ParameterType(com.palantir.conjure.spec.ParameterType) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) Tags(com.palantir.conjure.java.util.Tags) UndertowRuntime(com.palantir.conjure.java.undertow.lib.UndertowRuntime) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Methods(io.undertow.util.Methods) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Iterables(com.google.common.collect.Iterables) ListType(com.palantir.conjure.spec.ListType) ExternalReference(com.palantir.conjure.spec.ExternalReference) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) AuthTypeVisitor(com.palantir.conjure.visitor.AuthTypeVisitor) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) Deque(java.util.Deque) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) BearerToken(com.palantir.tokens.auth.BearerToken) ImmutableList(com.google.common.collect.ImmutableList) ConjureTags(com.palantir.conjure.java.ConjureTags) AsyncRequestProcessingMetadata(com.palantir.conjure.java.services.UndertowTypeFunctions.AsyncRequestProcessingMetadata) SetType(com.palantir.conjure.spec.SetType) ParameterOrder(com.palantir.conjure.java.util.ParameterOrder) StatusCodes(io.undertow.util.StatusCodes) CodeBlock(com.squareup.javapoet.CodeBlock) Serializer(com.palantir.conjure.java.undertow.lib.Serializer) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec) EndpointName(com.palantir.conjure.spec.EndpointName) HttpHandler(io.undertow.server.HttpHandler) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) Deserializer(com.palantir.conjure.java.undertow.lib.Deserializer) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) InputStream(java.io.InputStream) CodeBlock(com.squareup.javapoet.CodeBlock) ArrayList(java.util.ArrayList) Duration(java.time.Duration) HumanReadableDuration(com.palantir.humanreadabletypes.HumanReadableDuration) HttpString(io.undertow.util.HttpString) HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) OptionalType(com.palantir.conjure.spec.OptionalType) AuthType(com.palantir.conjure.spec.AuthType) ParameterType(com.palantir.conjure.spec.ParameterType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) HumanReadableDuration(com.palantir.humanreadabletypes.HumanReadableDuration) AsyncRequestProcessingMetadata(com.palantir.conjure.java.services.UndertowTypeFunctions.AsyncRequestProcessingMetadata)

Example 5 with Options

use of com.palantir.conjure.java.Options in project conjure-java by palantir.

the class UnionGenerator method generateUnknownWrapper.

private static TypeSpec generateUnknownWrapper(ClassName baseClass, ClassName visitorClass, Options options) {
    ParameterizedTypeName genericMapType = ParameterizedTypeName.get(Map.class, String.class, Object.class);
    ParameterizedTypeName genericHashMapType = ParameterizedTypeName.get(HashMap.class, String.class, Object.class);
    ParameterSpec typeParameter = ParameterSpec.builder(String.class, "type").addAnnotation(Nonnull.class).build();
    ParameterSpec annotatedTypeParameter = ParameterSpec.builder(UNKNOWN_MEMBER_TYPE, "type").addAnnotation(AnnotationSpec.builder(JsonProperty.class).addMember("value", "\"type\"").build()).build();
    ClassName wrapperClass = baseClass.peerClass(UNKNOWN_WRAPPER_CLASS_NAME);
    List<FieldSpec> fields = ImmutableList.of(FieldSpec.builder(UNKNOWN_MEMBER_TYPE, "type", Modifier.PRIVATE, Modifier.FINAL).build(), FieldSpec.builder(genericMapType, VALUE_FIELD_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
    TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(wrapperClass).addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).addSuperinterface(baseClass).addFields(fields).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addAnnotation(ConjureAnnotations.propertiesJsonCreator()).addParameter(annotatedTypeParameter).addStatement("this($N, new $T())", typeParameter, genericHashMapType).build()).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(typeParameter).addParameter(ParameterSpec.builder(genericMapType, VALUE_FIELD_NAME).addAnnotation(Nonnull.class).build()).addStatement("$L", Expressions.requireNonNull(typeParameter.name, "type cannot be null")).addStatement("$L", Expressions.requireNonNull(VALUE_FIELD_NAME, String.format("%s cannot be null", VALUE_FIELD_NAME))).addStatement("this.$1N = $1N", typeParameter).addStatement("this.$1L = $1L", VALUE_FIELD_NAME).build()).addMethod(MethodSpec.methodBuilder("getType").addModifiers(Modifier.PRIVATE).addAnnotation(AnnotationSpec.builder(JsonProperty.class).build()).addStatement("return type").returns(UNKNOWN_MEMBER_TYPE).build()).addMethod(MethodSpec.methodBuilder("getValue").addModifiers(Modifier.PRIVATE).addAnnotation(AnnotationSpec.builder(JsonAnyGetter.class).build()).addStatement("return $L", VALUE_FIELD_NAME).returns(genericMapType).build()).addMethod(MethodSpec.methodBuilder("put").addModifiers(Modifier.PRIVATE).addParameter(String.class, "key").addParameter(Object.class, "val").addAnnotation(AnnotationSpec.builder(JsonAnySetter.class).build()).addStatement("$L.put(key, val)", VALUE_FIELD_NAME).build()).addMethod(createWrapperAcceptMethod(visitorClass, VISIT_UNKNOWN_METHOD_NAME, typeParameter.name, false, options)).addMethod(MethodSpecs.createEquals(wrapperClass)).addMethod(MethodSpecs.createEqualTo(wrapperClass, fields)).addMethod(MethodSpecs.createHashCode(fields)).addMethod(MethodSpecs.createToString(wrapperClass.simpleName(), fields.stream().map(fieldSpec -> FieldName.of(fieldSpec.name)).collect(Collectors.toList())));
    return typeBuilder.build();
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) BiFunction(java.util.function.BiFunction) ClassName(com.squareup.javapoet.ClassName) PeekingIterator(com.google.common.collect.PeekingIterator) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) JsonValue(com.fasterxml.jackson.annotation.JsonValue) Map(java.util.Map) JsonAnyGetter(com.fasterxml.jackson.annotation.JsonAnyGetter) StableCollectors(com.palantir.conjure.java.util.StableCollectors) ParameterSpec(com.squareup.javapoet.ParameterSpec) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) TypeName(com.squareup.javapoet.TypeName) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Nulls(com.fasterxml.jackson.annotation.Nulls) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) DoubleFunction(java.util.function.DoubleFunction) HashMap(java.util.HashMap) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Nonnull(javax.annotation.Nonnull) CodeBlock(com.squareup.javapoet.CodeBlock) IntFunction(java.util.function.IntFunction) TypeVariableName(com.squareup.javapoet.TypeVariableName) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) FieldName(com.palantir.conjure.spec.FieldName) Comparator(java.util.Comparator) Collections(java.util.Collections) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ParameterSpec(com.squareup.javapoet.ParameterSpec) Nonnull(javax.annotation.Nonnull) ClassName(com.squareup.javapoet.ClassName) FieldSpec(com.squareup.javapoet.FieldSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

Options (com.palantir.conjure.java.Options)5 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)5 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)5 ClassName (com.squareup.javapoet.ClassName)5 JavaFile (com.squareup.javapoet.JavaFile)5 Map (java.util.Map)5 ImmutableList (com.google.common.collect.ImmutableList)4 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)4 JavaNameSanitizer (com.palantir.conjure.java.util.JavaNameSanitizer)4 Packages (com.palantir.conjure.java.util.Packages)4 Type (com.palantir.conjure.spec.Type)4 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)4 CodeBlock (com.squareup.javapoet.CodeBlock)4 FieldSpec (com.squareup.javapoet.FieldSpec)4 MethodSpec (com.squareup.javapoet.MethodSpec)4 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)4 TypeName (com.squareup.javapoet.TypeName)4 TypeSpec (com.squareup.javapoet.TypeSpec)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4