Search in sources :

Example 1 with FieldName

use of com.palantir.conjure.spec.FieldName 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 FieldName

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

the class BeanGenerator method generateStageInterfaces.

private static List<TypeSpec> generateStageInterfaces(ClassName objectClass, ClassName builderClass, TypeMapper typeMapper, ImmutableList<EnrichedField> fieldsNeedingBuilderStage, ImmutableList<EnrichedField> otherFields) {
    List<TypeSpec.Builder> interfaces = new ArrayList<>();
    PeekingIterator<EnrichedField> fieldPeekingIterator = Iterators.peekingIterator(sortedEnrichedFields(fieldsNeedingBuilderStage).iterator());
    while (fieldPeekingIterator.hasNext()) {
        EnrichedField field = fieldPeekingIterator.next();
        String nextBuilderStageName = fieldPeekingIterator.hasNext() ? JavaNameSanitizer.sanitize(fieldPeekingIterator.peek().fieldName()) : "completed_";
        ClassName nextStageClassName = stageBuilderInterfaceName(objectClass, nextBuilderStageName);
        interfaces.add(TypeSpec.interfaceBuilder(stageBuilderInterfaceName(objectClass, JavaNameSanitizer.sanitize(field.fieldName()))).addModifiers(Modifier.PUBLIC).addMethod(MethodSpec.methodBuilder(JavaNameSanitizer.sanitize(field.fieldName())).addParameter(ParameterSpec.builder(field.poetSpec().type, JavaNameSanitizer.sanitize(field.fieldName())).addAnnotation(Nonnull.class).build()).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(nextStageClassName.box()).build()));
    }
    ClassName completedStageClass = stageBuilderInterfaceName(objectClass, "completed_");
    TypeSpec.Builder completedStage = TypeSpec.interfaceBuilder(completedStageClass).addModifiers(Modifier.PUBLIC).addMethod(MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(objectClass.box()).build());
    completedStage.addMethods(otherFields.stream().map(field -> generateMethodsForFinalStageField(field, typeMapper, completedStageClass)).flatMap(List::stream).collect(Collectors.toList()));
    interfaces.add(completedStage);
    interfaces.get(0).addMethod(MethodSpec.methodBuilder("from").addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(builderClass).addParameter(objectClass, "other").build());
    return interfaces.stream().map(TypeSpec.Builder::build).collect(Collectors.toList());
}
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) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) ClassName(com.squareup.javapoet.ClassName) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 3 with FieldName

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

the class MethodSpecs method toStringConcatenation.

private static CodeBlock toStringConcatenation(String thisClassName, List<FieldName> fieldNames) {
    checkState(!fieldNames.isEmpty(), "String concatenation is only necessary if there are fields");
    CodeBlock.Builder builder = CodeBlock.builder().add("$S\n", thisClassName + '{' + fieldNames.get(0).get() + ": ");
    for (int i = 0; i < fieldNames.size(); i++) {
        FieldName fieldName = fieldNames.get(i);
        // The name of the first field is included with the class name
        if (i != 0) {
            builder.add(" + $S", ", " + fieldName.get() + ": ");
        }
        builder.add(" + $N", JavaNameSanitizer.sanitize(fieldName));
    }
    return builder.add(" + '}'").build();
}
Also used : CodeBlock(com.squareup.javapoet.CodeBlock) FieldName(com.palantir.conjure.spec.FieldName)

Example 4 with FieldName

use of com.palantir.conjure.spec.FieldName in project conjure by palantir.

the class ConjureParserUtils method parseFieldName.

private static FieldName parseFieldName(com.palantir.conjure.parser.types.names.FieldName parserFieldName) {
    FieldName fieldName = FieldName.of(parserFieldName.name());
    FieldNameValidator.validate(fieldName);
    return fieldName;
}
Also used : FieldName(com.palantir.conjure.spec.FieldName)

Example 5 with FieldName

use of com.palantir.conjure.spec.FieldName in project conjure by palantir.

the class UniqueFieldNamesValidator method validate.

@Override
public void validate(Set<FieldName> args) {
    Map<FieldName, FieldName> seenNormalizedToOriginal = new HashMap<>();
    for (FieldName argName : args) {
        FieldName normalizedName = FieldNameValidator.toCase(argName, CaseConverter.Case.LOWER_CAMEL_CASE);
        FieldName seenName = seenNormalizedToOriginal.get(normalizedName);
        Preconditions.checkArgument(seenName == null, "%s must not contain duplicate field names (modulo case normalization): %s vs %s", classSimpleName, argName.get(), seenName == null ? "" : seenName.get());
        seenNormalizedToOriginal.put(normalizedName, argName);
    }
}
Also used : HashMap(java.util.HashMap) FieldName(com.palantir.conjure.spec.FieldName)

Aggregations

FieldName (com.palantir.conjure.spec.FieldName)7 FieldDefinition (com.palantir.conjure.spec.FieldDefinition)4 ListType (com.palantir.conjure.spec.ListType)4 MapType (com.palantir.conjure.spec.MapType)4 ObjectDefinition (com.palantir.conjure.spec.ObjectDefinition)4 OptionalType (com.palantir.conjure.spec.OptionalType)4 PrimitiveType (com.palantir.conjure.spec.PrimitiveType)4 SetType (com.palantir.conjure.spec.SetType)4 Type (com.palantir.conjure.spec.Type)4 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)4 CodeBlock (com.squareup.javapoet.CodeBlock)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)3 Collections2 (com.google.common.collect.Collections2)3 ImmutableList (com.google.common.collect.ImmutableList)3 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)3 Options (com.palantir.conjure.java.Options)3 JavaNameSanitizer (com.palantir.conjure.java.util.JavaNameSanitizer)3 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)3 MoreVisitors (com.palantir.conjure.java.visitor.MoreVisitors)3 TypeVisitor (com.palantir.conjure.visitor.TypeVisitor)3