Search in sources :

Example 11 with FieldDefinition

use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.

the class BeanBuilderAuxiliarySettersUtils method createCollectionSetterBuilder.

public static MethodSpec.Builder createCollectionSetterBuilder(String prefix, EnrichedField enriched, TypeMapper typeMapper, ClassName returnClass) {
    FieldSpec field = enriched.poetSpec();
    FieldDefinition definition = enriched.conjureDef();
    Type type = definition.getType();
    return MethodSpec.methodBuilder(prefix + StringUtils.capitalize(field.name)).addJavadoc(Javadoc.render(definition.getDocs(), definition.getDeprecated()).map(rendered -> CodeBlock.of("$L", rendered)).orElseGet(() -> CodeBlock.builder().build())).addAnnotations(ConjureAnnotations.deprecation(definition.getDeprecated())).addModifiers(Modifier.PUBLIC).returns(returnClass).addParameter(Parameters.nonnullParameter(widenParameterIfPossible(field.type, type, typeMapper), field.name));
}
Also used : Type(com.palantir.conjure.spec.Type) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) FieldSpec(com.squareup.javapoet.FieldSpec)

Example 12 with FieldDefinition

use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.

the class BeanBuilderGenerator method createField.

private EnrichedField createField(FieldName fieldName, FieldDefinition field) {
    FieldSpec.Builder spec = FieldSpec.builder(typeMapper.getClassName(field.getType()), JavaNameSanitizer.sanitize(fieldName), Modifier.PRIVATE);
    if (field.getType().accept(TypeVisitor.IS_LIST)) {
        spec.initializer("new $T<>()", ArrayList.class);
    } else if (field.getType().accept(TypeVisitor.IS_SET)) {
        spec.initializer("new $T<>()", LinkedHashSet.class);
    } else if (field.getType().accept(TypeVisitor.IS_MAP)) {
        spec.initializer("new $T<>()", LinkedHashMap.class);
    } else if (field.getType().accept(TypeVisitor.IS_OPTIONAL)) {
        spec.initializer("$T.empty()", asRawType(typeMapper.getClassName(field.getType())));
    } else if (field.getType().accept(MoreVisitors.IS_INTERNAL_REFERENCE)) {
        com.palantir.conjure.spec.TypeName name = field.getType().accept(TypeVisitor.REFERENCE);
        typeMapper.getType(name).filter(definition -> definition.accept(TypeDefinitionVisitor.IS_ALIAS)).map(definition -> definition.accept(TypeDefinitionVisitor.ALIAS)).ifPresent(aliasDefinition -> {
            Type aliasType = aliasDefinition.getAlias();
            if (aliasType.accept(MoreVisitors.IS_COLLECTION) || aliasType.accept(TypeVisitor.IS_OPTIONAL)) {
                spec.initializer("$T.empty()", typeMapper.getClassName(field.getType()));
            }
        });
    }
    return EnrichedField.of(fieldName, field, spec.build());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ClassName(com.squareup.javapoet.ClassName) Collections2(com.google.common.collect.Collections2) Options(com.palantir.conjure.java.Options) ByteBuffer(java.nio.ByteBuffer) MapType(com.palantir.conjure.spec.MapType) Map(java.util.Map) OptionalType(com.palantir.conjure.spec.OptionalType) MoreVisitors(com.palantir.conjure.java.visitor.MoreVisitors) EnumSet(java.util.EnumSet) 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) List(java.util.List) EnrichedField(com.palantir.conjure.java.types.BeanGenerator.EnrichedField) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Nulls(com.fasterxml.jackson.annotation.Nulls) ListType(com.palantir.conjure.spec.ListType) ExternalReference(com.palantir.conjure.spec.ExternalReference) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) ConjureCollections(com.palantir.conjure.java.lib.internal.ConjureCollections) Type(com.palantir.conjure.spec.Type) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SafeArg(com.palantir.logsafe.SafeArg) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Buffer(java.nio.Buffer) SetType(com.palantir.conjure.spec.SetType) CodeBlock(com.squareup.javapoet.CodeBlock) LinkedHashSet(java.util.LinkedHashSet) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) 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) Collections(java.util.Collections) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) Preconditions(com.palantir.logsafe.Preconditions) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) FieldSpec(com.squareup.javapoet.FieldSpec)

Example 13 with FieldDefinition

use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.

the class BeanBuilderGenerator method createCollectionSetter.

private MethodSpec createCollectionSetter(String prefix, EnrichedField enriched, boolean override) {
    FieldDefinition definition = enriched.conjureDef();
    Type type = definition.getType();
    boolean shouldClearFirst = false;
    return BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder(prefix, enriched, typeMapper, builderClass).addAnnotations(ConjureAnnotations.override(override)).addCode(verifyNotBuilt()).addCode(typeAwareAssignment(enriched, type, shouldClearFirst)).addStatement("return this").build();
}
Also used : MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) FieldDefinition(com.palantir.conjure.spec.FieldDefinition)

Example 14 with FieldDefinition

use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.

the class BeanGenerator method generateMethodsForFinalStageField.

private static List<MethodSpec> generateMethodsForFinalStageField(EnrichedField enriched, TypeMapper typeMapper, ClassName returnClass) {
    List<MethodSpec> methodSpecs = new ArrayList<>();
    Type type = enriched.conjureDef().getType();
    FieldDefinition definition = enriched.conjureDef();
    methodSpecs.add(MethodSpec.methodBuilder(JavaNameSanitizer.sanitize(enriched.fieldName())).addParameter(ParameterSpec.builder(BeanBuilderAuxiliarySettersUtils.widenParameterIfPossible(enriched.poetSpec().type, type, typeMapper), JavaNameSanitizer.sanitize(enriched.fieldName())).addAnnotation(Nonnull.class).build()).addJavadoc(Javadoc.render(definition.getDocs(), definition.getDeprecated()).map(rendered -> CodeBlock.of("$L", rendered)).orElseGet(() -> CodeBlock.builder().build())).addAnnotations(ConjureAnnotations.deprecation(definition.getDeprecated())).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(returnClass.box()).build());
    if (type.accept(TypeVisitor.IS_LIST)) {
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder("addAll", enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createItemSetterBuilder(enriched, type.accept(TypeVisitor.LIST).getItemType(), typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
    }
    if (type.accept(TypeVisitor.IS_SET)) {
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder("addAll", enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createItemSetterBuilder(enriched, type.accept(TypeVisitor.SET).getItemType(), typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
    }
    if (type.accept(TypeVisitor.IS_MAP)) {
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder("putAll", enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createMapSetterBuilder(enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
    }
    if (type.accept(TypeVisitor.IS_OPTIONAL)) {
        methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createOptionalSetterBuilder(enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
    }
    return methodSpecs;
}
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) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) MethodSpec(com.squareup.javapoet.MethodSpec) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) ArrayList(java.util.ArrayList)

Aggregations

FieldDefinition (com.palantir.conjure.spec.FieldDefinition)14 Type (com.palantir.conjure.spec.Type)8 FieldName (com.palantir.conjure.spec.FieldName)5 MapType (com.palantir.conjure.spec.MapType)5 PrimitiveType (com.palantir.conjure.spec.PrimitiveType)5 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)5 FieldSpec (com.squareup.javapoet.FieldSpec)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 Test (org.junit.jupiter.api.Test)5 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)4 ImmutableList (com.google.common.collect.ImmutableList)4 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)4 Options (com.palantir.conjure.java.Options)4 JavaNameSanitizer (com.palantir.conjure.java.util.JavaNameSanitizer)4 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)4 ListType (com.palantir.conjure.spec.ListType)4 ObjectDefinition (com.palantir.conjure.spec.ObjectDefinition)4 OptionalType (com.palantir.conjure.spec.OptionalType)4