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();
}
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());
}
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();
}
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;
}
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);
}
}
Aggregations