Search in sources :

Example 1 with EnrichedField

use of com.palantir.conjure.java.types.BeanGenerator.EnrichedField in project conjure-java by palantir.

the class BeanBuilderGenerator method createSetters.

private Iterable<MethodSpec> createSetters(Collection<EnrichedField> fields, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, boolean override) {
    Collection<MethodSpec> setters = Lists.newArrayListWithExpectedSize(fields.size());
    for (EnrichedField field : fields) {
        setters.add(createSetter(field, typesMap, override));
        setters.addAll(createAuxiliarySetters(field, override));
    }
    return setters;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) EnrichedField(com.palantir.conjure.java.types.BeanGenerator.EnrichedField)

Example 2 with EnrichedField

use of com.palantir.conjure.java.types.BeanGenerator.EnrichedField 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 3 with EnrichedField

use of com.palantir.conjure.java.types.BeanGenerator.EnrichedField in project conjure-java by palantir.

the class BeanBuilderGenerator method createValidateFieldsMethod.

private static MethodSpec createValidateFieldsMethod(List<EnrichedField> primitives) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("validatePrimitiveFieldsHaveBeenInitialized").addModifiers(Modifier.PRIVATE);
    builder.addStatement("$T missingFields = null", ParameterizedTypeName.get(List.class, String.class));
    for (EnrichedField field : primitives) {
        String name = deriveFieldInitializedName(field);
        builder.addStatement("missingFields = addFieldIfMissing(missingFields, $N, $S)", name, field.fieldName().get());
    }
    builder.beginControlFlow("if (missingFields != null)").addStatement("throw new $T(\"Some required fields have not been set\"," + " $T.of(\"missingFields\", missingFields))", SafeIllegalArgumentException.class, SafeArg.class).endControlFlow();
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) EnrichedField(com.palantir.conjure.java.types.BeanGenerator.EnrichedField) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) SafeArg(com.palantir.logsafe.SafeArg)

Aggregations

EnrichedField (com.palantir.conjure.java.types.BeanGenerator.EnrichedField)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 ImmutableList (com.google.common.collect.ImmutableList)2 SafeArg (com.palantir.logsafe.SafeArg)2 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 JsonSetter (com.fasterxml.jackson.annotation.JsonSetter)1 Nulls (com.fasterxml.jackson.annotation.Nulls)1 Collections2 (com.google.common.collect.Collections2)1 Lists (com.google.common.collect.Lists)1 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)1 Options (com.palantir.conjure.java.Options)1 ConjureCollections (com.palantir.conjure.java.lib.internal.ConjureCollections)1 JavaNameSanitizer (com.palantir.conjure.java.util.JavaNameSanitizer)1 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)1 DefaultableTypeVisitor (com.palantir.conjure.java.visitor.DefaultableTypeVisitor)1 MoreVisitors (com.palantir.conjure.java.visitor.MoreVisitors)1 ExternalReference (com.palantir.conjure.spec.ExternalReference)1